• 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

construtor in abstract class

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can there be a construtor in an abstract class, anyhow?
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried it? I guess not, otherwise you would have found out it is possible.

Of course this makes sense - an abstract class is still part of a class hierarchy, so constructor chaining will be the the same as with non-abstract classes.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This can be a bit confusing. You can't construct an abstract class directly. But it can have a constructor. This is so, as Rob points out, its constructor gets called either implicitly via constructor chaining or explicitly (via the super keyword) to allow for it to be constructed (and have values initialized or what have you). Take Rob's suggestion and try some examples.
 
Marshal
Posts: 79390
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course you need a constructor for an abstract class. I sometime quote an example like this:where the Vehicle class is abstract. But all vehicles have common characteristics.Now, the way I look on subclasses is that they incorporate a part from the superclass, which I am sure is not quite accurate.
A Car or a Bus is a Vehicle, and they have number plates (only they are called something different in different countries), so when you write the constructor for a Car it would include "super(numberPlate);" That means you are instantiating the "Vehicle" part. Then you can instantiate the "Car" part, in the rest of the constructor.

In fact every class has a constructor; if you don't write one the compiler will add a default constructor for you.
 
reply
    Bookmark Topic Watch Topic
  • New Topic