
Interface methods are implicitly abstract, and also require a concrete class implementation. Remember that an interface is a kind of contract that must be implemented by a concrete class. Next, let's look at a few of the things we can do with interfaces. Declaration and Initialization Public and Private Access Control self and this Inheritance and Polymorphism. If you notice that many classes belong to a parent class with the same method actions but different behavior, then it's a good idea to use an interface. So Serializable is still offering a guarantee, like List is, but it isn't about method signatures, it's about an extralinguistic feature of the language.In this case, the implementation methods for ArrayList, LinkedList, and Vector are all different, which is a great scenario for using an interface. It doesn't guarantee that methods are there, but instead guarantees that the creator of the class that implements serializable has thought about the many issues associated with serializing a class (overriding readObject/writeObject, compatiblity with other serialized forms, and other issues ). The most common use of polymorphism in Object Oriented Programming occurs when a parent class. Just a word of note, Serializable is a marker interface and a little odd because of that. This refers to the ability of an object to take on many forms. Being an object-oriented programming language, Java is much closer to the real world than most other languages, with concepts like Abstraction, encapsulation, inheritance, and polymorphism. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object. Any Java object that can pass more than one IS-A test is considered to be polymorphic. (you cannot see the difference.) Byungjoon Lee. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. If a class satisfies the contract, you can treat the object from the class in the same way that you use the object of the interface. What does it mean to "program to an interface"? Interface is a general contract which all the implementer class should satisfy.
#Assignment statement with interface and polymorphism java code#
If at some point you wanted to return a ArrayList instead of the LinkedList the caller would not have to change any code because they only care about the interface. The caller of your method isn't coupled to your specific implementation internal implementation which might use, and return, a LinkedList. Design patterns like Command, Observer, Decorator, Strategy, and.


It's much more flexible if you can return a List as opposed to something like a LinkedList. Polymorphism or the ability of an object to execute specialized actions based on its typeis what makes Java code flexible. It's called "programming to the interface". However, it can be achieved with interfaces, because the class can implement multiple interfaces. 2) Java does not support 'multiple inheritance' (a class can only inherit from one superclass). A reference variable declared as an interface type can refer to any object of the class that has implemented this interface. When Arrays.asList() returns a List you're not actually getting an interface, you're getting a concrete class that is guaranteed to implement the methods listed in the List interface.Īs to your "we should never really use a interface type variable" you're actually suppose to do that. 1) To achieve security - hide certain details and only show the important details of an object (interface). That is, the same entity (method or operator or object) can perform different operations in different scenarios. Any class that implements List will be guaranteed to have the methods of the interface. Java Polymorphism Polymorphism is an important concept of object-oriented programming.

Think of the List interface as a guarantee.
