Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract classes

 
Michael Keisu
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
Flex Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract classes encapsulate shared behavior and define the protocol for all subclasses
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is already FAQ available which explains when to use which one.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic