• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract class

 
sam liya
Ranch Hand
Posts: 1240
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does abstract class has constructor?
-when we make the object of a concrete class what happen to the abstract class?
 
Eric Daly
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, an abstract class does not have a constructor because it is never instantiated (made into an object). There is nothing to "construct."
The abstract class just provides default methods and variables for any subclass to inherit. Nothing happens to the abstract class when a subclass is instantiated, you just automatically have all the methods* and variables* when you extend the abstract class. It also can be used to require a subclass to override (or re-define) certain methods.

*depending on their access modifiers like private or public
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid you are mistaken. Abstract classes can have constructors. And when you instantiate the subclass, part of that subclass object is "made from" the abstract superclass.
 
Eric Daly
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darn! Sorry for giving false information... It's been too long since I've studied Java in depth, I really need to get back into it. Thanks for the correction! I will try to remember it this time!
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



When a new BasketBall object is created the JVM calls its constructor, but doesn't complete. Before it does BasketBall's super type constructor is called and if that object has a super type then its constructor is called before that completes, otherwise BasketBall's super type constructor completes and then BasketBall's constructor completes. All objects in java are created this way.
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add one more thing to the question,

Can abstract class have parameterized constructor?

 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harshit Rastogi wrote:Just to add one more thing to the question,

Can abstract class have parameterized constructor?



Yep they can. Look at Campbell answer for an explanation. You could easily try it out by creating a abstract class with a parameterized constructor.

Only restriction on the abstract class is that you cannot create an instance of it. Other than that all the normal rules apply!!
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Mercs wrote:
Only restriction on the abstract class is that you can create an instance of it. Other than that all the normal rules apply!!


It guess its cannot, typing mistake...
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harshit Rastogi wrote:
Sam Mercs wrote:
Only restriction on the abstract class is that you can create an instance of it. Other than that all the normal rules apply!!


It guess its cannot, typing mistake...


Yep a typo ... You cannot instantiate an abstract class - of course... updated .. Thanks for pointing out
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic