This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
Hi, Being new to java, i would need some help from the experts here. Can someone explain me the concepts of abstract class & methods and how would each help in polymorphism. Please give an explanation with a simple example. Also if possible, it would be of much help if explanation is given with reference to eg in ch8 of head forst java
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.
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.
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.
I just have a doubt here. Please help me with this.
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.
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.
Joined: Oct 13, 2005
I did say it isn't easy to understand.
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.
Joined: May 13, 2002
There was a bug which disallowed usage of @Override on a method which overrides an inherited interface method. The bug was fixed here.