• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract class also has constructor

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we cannot instantiate an abstract class, why does it have a constructor. We are obviously never going to create an object of an abstract class, but only of its sub classes. Correct? Then what is the purpose of having it there at all? Is it just to accommodate the Java language statute that since first statement in any constructor is a call to super() , it has to be there? Or is there something more to it?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors don't create objects. They put a newly created object into a valid initial state. Since abstract classes can have state (member variables), they need constructors to initialize that state.



How would we initialize the common base class state "name" without the constructor?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Constructors don't create objects. They put a newly created object into a valid initial state.


If constructors are not responsible for creating objects in Java, then what is?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:
Jeff Verdegan wrote:Constructors don't create objects. They put a newly created object into a valid initial state.


If constructors are not responsible for creating objects in Java, then what is?


The new operator, most of the time.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Mansukhdeep Thind wrote:
Jeff Verdegan wrote:Constructors don't create objects. They put a newly created object into a valid initial state.


If constructors are not responsible for creating objects in Java, then what is?


The new operator, most of the time.


OK Jeff. Thank you. I shall close this thread now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic