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

Abstract Methods and Classes

Martin Vladimiruk

Joined: Feb 19, 2013
Posts: 1

What are abstract methods and classes for? I can not see any necessity to use this. Can anyone give me any example of use? When is it necessary?
Manu Somasekhar
Ranch Hand

Joined: May 22, 2012
Posts: 44
When you want to have a common signature to your methods (like methods within an interface) and some common behaviors (like methods defined in a super class in an inheritance hierarchy), you can go for an abstract class.

Google is your friend. Some of the best examples are here
Winston Gutkowski

Joined: Mar 17, 2011
Posts: 8927

Martin Vladimiruk wrote:What are abstract methods and classes for? ... Can anyone give me any example of use?

Yes: AbstractList - In fact all of the 'Abstract...' classes in the Java Collections API.

When is it necessary?

When you have a class (or interface) based hierarchy that you can provide a partial implementation for.

I can not see any necessity to use this.

Maybe not, but some very clever people were involved with creating Java, so you might want to ask yourself why they spent the time to add it. It's also possible that you simply haven't yet encountered a situation where you need it.


Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
Articles by Winston can be found here
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

Martin Vladimiruk wrote:When is it necessary?
I would hazard a guess to say that it is not really necessary to have Abstract methods and classes when you have interfaces, but I know it is extremely convenient and useful to have them.

Winston's example of the AbstractList is a perfect example. The List interface defines all the common method signatures for any list implementation, so AbstractList is not necessary. But since much of what a list does will be the same, regardless of how the list is implemented, you can extract many methods out into a common parent - the AbstractList - so you don't have to have each List implementing class re-implement the same work for what contains() or toString() does (as examples). That means less code, and less code usually means fewer bugs. There are other ways to achieve it, but in this case an abstract parent class makes sense; it is the easiest to implement, easiest to use, doesn't interfere with a clean class-hierarchy.

Finally, since it is abstract it does not have to provide implementations for every method in the List interface; that is good because now you aren't forcing one implementation on child-classes and are forcing child-classes to actually implement the bits that are not common.

I agree. Here's the link:
subject: Abstract Methods and Classes
It's not a secret anymore!