File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Abstract Class & Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract Class & Methods" Watch "Abstract Class & Methods" New topic
Author

Abstract Class & Methods

Kalyan Naveenan
Greenhorn

Joined: Jul 23, 2011
Posts: 12
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
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3837

Welcome to CodeRanch, Kalyan Naveenan!

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.



SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

Hi,

Please see the program and comments.


Hope this helps

Thanks.


The important thing is not to stop questioning.Curiosity has its own reason for existing.
Kalyan Naveenan
Greenhorn

Joined: Jul 23, 2011
Posts: 12
Thanks Vijay Tidake for helping me understand the concept with a simple eg. So can you explain me why and when do i use the concept of interface implementation.
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

Hi,

Hope following example helps you to understand the concept.




Thanks
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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



Rohit Mehta
Ranch Hand

Joined: Mar 11, 2005
Posts: 79

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.
neha singh tomar
Greenhorn

Joined: Apr 14, 2009
Posts: 5
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
Sheriff

Joined: Oct 13, 2005
Posts: 39380
    
  28
We have an FAQ which might be helpful.
neha singh tomar
Greenhorn

Joined: Apr 14, 2009
Posts: 5
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39380
    
  28
You would have to read the Java™ Language Specification for the official definition of "overriding".
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39380
    
  28
I earlier wrote:You would have to read the Java™ Language Specification for the official definition of "overriding".
It is not easy to read.
neha singh tomar
Greenhorn

Joined: Apr 14, 2009
Posts: 5
Thanks Ogeh. The example was really helpful.

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39380
    
  28
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.
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
There was a bug which disallowed usage of @Override on a method which overrides an inherited interface method. The bug was fixed here.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39380
    
  28
I didn't know about that bug. Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract Class & Methods