• 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 extending concrete classes

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recently saw a statment that abstract class can extend a concrete class!
What good can this be? No polymorphism is present here isn't it?
Thanks!
Sricharan
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can derive from a concrete class an designate it abstract:

I'm not really sure what you meant by your "polymorphism" comment. Polymorphism is always present in Java, since all calls are routed to the correct object when methods are invoked.
 
Sricharan Modali
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What i meant by polymorphism was Abstract classes are extended by concrete class to override the methods present in them. So overriding holds the key here. The classes that extend the abstract class can override the methods depending on its needs. But when u extend a concrete class by a astract class what possible use it can be of?
Thanks!
Sricharan
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes you don't always start from the abstract and work toward concrete. Sometimes, you start with something concrete, but some subset of those can have something "abstracted out" to a middle layer. Let me try to come up with an example - forgive me if this is remarkably contrived.

In this example, it is quite possible to have an instance of Employee. Perhaps that instance represents you or me - we each have a first name and last name, we each have a social security number, and we each have an employee ID.
Now, let's imagine that, for some reason, we want to have some instances that represent baseball players. Every baseball player has a number and a team, but not all players can do the same things. In this case, only a Pitcher can throw a pitch and only a Batter can swing a bat.
What I've done is "abstracted out" the things that are common to all players, such as the number and team. Those things are put into an abstract class that Pitcher and Batter will inhherit from. But, as that isn't a "full" representation of a baseball player, I've made that class abstract - it doesn't truly represent a baseball player because, even though it identifies some things that all baseball players have, it doesn't include what baseball players can do.
Therefore, I've created some concrete classes that extend BaseballPlayer that include the unique capabilities of each type of player.
As you can see from this example, although it is rather contrived, there is some benefit from starting with a concrete class, extending it was an abstract class, and then extending that with more concrete classes.
I hope that helps,
Corey
[ March 30, 2004: Message edited by: Corey McGlone ]
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you can never instantiate an abstract class, it will be of no use until a concrete class is extended from it. So, when you finally are able to instantiate an object of some class type, it will have every method defined.
What's the use? Well in some cases you will want to define commonly used functions as part of an abstract class that you never want to have instantiated. Then, as child classes are created, they can call their abstract parent's methods instead of having to redefine them all over again.
As for the polymorphism, as I said, since ultimately any concrete class will have to define every abstract function, polymorphic method calls will get routed to the correct object's definition. Polymorphism isn't affected by abstract classes. You can think of the "abstract" keyword as insurance that nobody will try to instantiate one.
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since all classes ultimately descend from Object, it would seem that there must be some possible use.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since all classes descend from Object, all abstract classes descend from a concrete class.
reply
    Bookmark Topic Watch Topic
  • New Topic