• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Usefulness of Abstract Classes?

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
As of right now I do not like abstract classes...I don't see the point of making a class that has to have the methods implemented by the user.
How is the programmer supposed to know how to implement the methods?
Can someone please correct me and tell me why abstract classes are such a wonderful feature of Java?
Thanks,
Nick Ueda
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let's say you're building an application for an airport (just an example). The client tells you that you have an airplane, but an airplan can be started using different techniques. One airplane can have 2 main engines, but another airplane can have 4.
So, how would you go with this example?
2o) you can have an abstract class for an airplane. you know that even if the airplane has 2 or 4 engines, it's got wheels, it's got windows, it's got a cockpit, so on. it's the same. So does it make sense to create 2 classes that have the same attributes? The only thing is that the way to start an engine in a 2-engine plane is different to a 4-engine plane.
So that's the reason this abstract class will have some methods that are not implemented. This is to give you the flexibility to implement these methods in a subclass (more specific) in any way you like.
the abstract class, for instance, can have a method openDoor() which is common to all the planes. So it is implemented in this abstract class, and when you create your particular class extending from this abstract class, you automatically inherit all these methods. You will only need to implement the ones that are different for the plane you are creating.
Now, if a client tells you six months later that a new plane came with 1 engine, what would you do? simple, you know that this new plane is indeed, a plane, so you only need to extend from this class again and implement the method to start the engine. If the way to open a door is different, you simply override the method from the abstract class.
So, instead of duplicating information, you create an abstract class, knowing that all the classes that extend from this class will inherit all the properties that this abstract class has.
my lack of english fluency makes things harder to explain , but I hope this helps a bit
[ August 13, 2003: Message edited by: Andres Gonzalez ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Andres,
Good explanation buddy
regards
subramaniam o
 
Nick Ueda
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much, Andres that actually helps me alot!
 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good explanation. Can you pls explain me when should we go for interface and when to use abstract class?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anres,
that was too good dude!
bye
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Choosing interfaces and abstract classes is not an either/or proposition. If you need to change your design, make it an interface.Abstract classes are an excellent way to implement planned inheritance hierarchy and an example for the usage is over to Andres
Cheers,
Gaya3

---------------------------------------------------------------------------
Learniing by Doing and Sharing..
 
What's a year in metric? Do you know this metric stuff tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic