This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract Methods and Classes" Watch "Abstract Methods and Classes" New topic
Author

Abstract Methods and Classes

Martin Vladimiruk
Greenhorn

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
Bartender

Joined: Mar 17, 2011
Posts: 7690
    
  19

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.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract Methods and Classes