Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

construtor in abstract class

 
Agarwal Priyanka
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can there be a construtor in an abstract class, anyhow?
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47280
52
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic