Abstract classes can define abstract methods as well as concrete methods. Abstract methods must be implemented by the subclasses who extend the abstract class. So using this mechanism you can define more generic methods in abstract classes which are applied to subclasses as well, and still other abstract methods which needed to be implemented by the subclasses.
In this way you can have a super class reference referred an subclass and they are guaranteed to implement all the abstract methods of the super(abstract) class. So you have the polymorphism.
Please see the program and comments.
Hope this helps
Hope following example helps you to understand the concept.
Kalyan Naveenan wrote:can you explain me why and when do i use the concept of interface implementation.
Interface and abstract class are abstract reference types. An abstract reference type is a type that may contain a contract that specifies how to derive a concrete reference type. This contract is in the form of one or more abstract methods. In the example provided by Vijay Tidake,
Vehicle is an abstract reference type which contains a contract that specifies how to derive a concrete reference type. The contract is public abstract void run(); This contract says "in order to derive a concrete reference type from Vehicle, the concrete reference type must implement a method called 'run'. This method must be public and must not return a value". The ThreeWheeler and FourWheeler classes are concrete reference types that are derived from Vehicle.
In the example below, Vehicle is an interface
Kalyan Naveenan wrote:... So can you explain me why and when do i use the concept of interface implementation.
In Java you can extend only one class at a time - while can implement as many interfaces as you want.
To handle scenarios where you need to define a class which display some other behavior too (other than extended class'), we require multiple inheritance - which is possible through implementing multiple interfaces.
Also when you need to define the contract ONLY, you will require interfaces - as abstract class may/may not contain a concrete method - but an interface can't contain any concrete method - it can contain method declarations only.
Hope this is helpful.
If a method is declared as abstract in super class and we are defining it in sub class. Will it be called as "overriding'? I got this doubt since in the examples given in this thread, its mentioned as overriding.
Thanks in advance.
Campbell Ritchie wrote:We have an FAQ which might be helpful.
I am still not clear. I am very new to Java and preparing for certification. It would be very helpful for me if you could explain. As per my understanding, it should be termed as implementation. Overriding is, when we have one method already defined in super class and subclass redefines it. Please correct if I am wrong. Thanks in advance.
neha singh tomar wrote:As per my understanding, it should be termed as implementation
A class can override an inherited abstract method by providing another abstract method declaration. This class must be declared abstract. For example
A class can override an inherited abstract method by providing a concrete implementation. For example
It is not easy to read.
I earlier wrote:You would have to read the Java™ Language Specification for the official definition of "overriding".
Thanks Campbell. The link is really difficult to understand. Perhaps, after reading it 2-3 times I will be able to understand it clearly.
So, I concluded that it can actually be termed as overriding.
I agree with you. Yes, filling in the body of an abstract method is called overriding.
you should always use the @Override annotation on any methods you think you are overriding, but beware: @Override also works (I think this behaviour is different in Java5 and Java6) when you are implementing an interface method, which isn't overriding according to that definition.