wood burning stoves*
The moose likes Beginning Java and the fly likes abstract 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 classes" Watch "abstract classes" New topic
Author

abstract classes

Michael Keisu
Ranch Hand

Joined: Oct 08, 2009
Posts: 30
I'm reading about abstract classes, but I'm not sure if I understand them. Perhaps I'm just slow today, I feel I should totally get it.
I understand you don't instantiate them. Then is an abstracts class use only for subclasses to inherit stuff from.. Should it be used in a situation where it is uncessary to have a normal parent class, because the parent class is never going to be instantiated anyway - the abstract class just there to provide variables, methods and abstract methods for subclasses? And the abstract methods are declared because each of the subclasses methods are going to have different/unique implementation?

If I got it right they seem sort of uncessesary. But then there is also interfaces, which is not much different my textbook says. So I'm obviously missing something/some important facts.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
"Superclasses", please, not parent classes.

You want something which has a common component and parts different. You might have a Vehicle class with Car, Bus and Van subclasses. So as not to instantiate a Vehicle as such, you can label Vehicle as abstract. But you can call a Car, or a Bus, or a Van a Vehicle.
Michael Keisu
Ranch Hand

Joined: Oct 08, 2009
Posts: 30
Campbell Ritchie wrote:"Superclasses", please, not parent classes.

You want something which has a common component and parts different. You might have a Vehicle class with Car, Bus and Van subclasses. So as not to instantiate a Vehicle as such, you can label Vehicle as abstract. But you can call a Car, or a Bus, or a Van a Vehicle.


Okay, I think I got it now. And declaring a abstract method in the abstract class would also force you to define it in its subclasses, which a empty method within a normal superclass would never do, not so much as a reminder.

Thanks

Al Johnston
Ranch Hand

Joined: May 02, 2009
Posts: 99

Abstract classes are generally the only time I use inheritance. For instance, say I have a Store and each store has a type (General Store, Portal Store, Cart Store) that are all stores. I would use an abstract class to define Store such as.



Then, in all my stores, such as GeneralStore, I would extend Store



Sorry if the formatting is hard to read. I wrote the example here instead of an IDE. Hopefully this gives you some practical ideas on how to use abstract classes. Abstract classes save you a lot of time (unless you just like to type). If you tie an abstract class to an interface ("public abstract Class implements Interface"), you can get even more power through the abstraction of your code!

Cheers,
Al
Raj chiru
Ranch Hand

Joined: Aug 12, 2008
Posts: 141
Abstract classes encapsulate shared behavior and define the protocol for all subclasses
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

There is already FAQ available which explains when to use which one.


http://muhammadkhojaye.blogspot.com/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract classes