• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

abstract classes

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 99
Postgres Database Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abstract classes encapsulate shared behavior and define the protocol for all subclasses
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is already FAQ available which explains when to use which one.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic