• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the use of constructor inside abstract class  RSS feed

 
Athira Vineeth
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

In java it is not possible to instantiate an abstract class. Then why there need a constructor inside abstract class?

Thanks in Advance
Athira
 
Rajat Jindal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance of Abstract class cant be created but still there are few things that needs to be taken care:

1. Whenever you inherit a class to a parent class, at least one constructor of the parent class should be called by child class, other wise Exception will be thrown.Reason is given below, why it is so.

2. Basic use of constructor is to make sure that the class should be initialized properly or should be in proper state before you get your hands dirty on its instance/object. So same thing happens when you do inheritance, before the child instance is created, we need to make sure parent as well as child should be in proper state and since Abstract classes can provide partial implementation so that should also be in proper state before you create an instance of child class.
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajat Jindal wrote: . . .

1. Whenever you inherit a class to a parent class, at least one constructor of the parent class should be called by child class, other wise Exception will be thrown.Reason is given below, why it is so.
. . .
Don’t say child and parent: subclass and superclass please.

That was a good explanation, but you do not normally suffer an Exception, because such a class will usually not compile.
 
Rajat Jindal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie... Thanks for correcting me... yes class will not compile... :-)
 
Athira Vineeth
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!