• Post Reply Bookmark Topic Watch Topic
  • New Topic

The constructors of an abstract class  RSS feed

 
Peng Fan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we know when we instantiate a class,via the constructors we get all of the instance variables of that class and those of superclass and those of superclass's superclass initialized,untill reach the root class Object. Since abstract class cannot be instantiated, in this case, constructors' functionalities is lost, so the only logical that seem to be reasonable for putting constructor in an abstract class is transmiting the informations of the concrete class to the superclass of the abstract class. Am i right? thx!
 
Rikko Verrijzer
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
An abstract class can not have an constructor, it's simply illegal. This is because it has methods without a body, and it wouldn't make sense if an object of this class existed in that way. Constructing is left to a non-abstract subclass, which has the abstract methods implemented.
Hope this helps?
Rikko
 
Stephan Erlank
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Actually an abstract class can have constructors. Remember that it is legal to create an abstract class with no abstract methods! For the same reason it is perfectly legal to create an abstract class with constructors.
A constructor of an abstract class' functionality isn't lost however. Because of either an implicit or explicit call to a super constructor, a constructor in your abstract class will always be called.
 
Rikko Verrijzer
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan,
You seem to be right. My apologies for any misunderstanding!
Fanny, go with Stephans explanation!
Rikko
 
Stephan Erlank
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem, you might have gotton confused with interfaces, which cannot define any sort of implementation, hence no constructors.
 
Peng Fan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would you please explain to me the real functionalities of the constructors of a class including abstract class, we know super() is always inserted to the first line of the constructor's body inplicitly or explicitly, it's seem that jvm always make sure the root class Object be loaded and initialized when any class be instantiated, in this sense, one of the constructors functionalities is just like a bridge connecting the superclass and subclass. Did i make myself clear? Am i right?
 
Stephan Erlank
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The functionality of a constructor is exactly that. To construct or initialize a new instance of a class.
I would not really call it a bridge between a subclass and its superclass, because none of the subclass's members are available to the superclass, it only works the other way round.
 
Gjorgi Var
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract class does have a constructor and that constructor has to be called in the subclasses, otherwise compiler would take care of that with a default constructor with which we won't be very happy... superclass and its subclasses are not there to be bridged like they have been disconnected; superclass is like a diety of its subclasses, it has its face in them... all subclasses are made according to specifications in the superclass...
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you instantiate an object with "new MyClass(i, j)", the "new" operator calls the MyClass constructor whose aguments match, such as "MyClass(int a, int b)".
That constructor has three choices. It can immediately call another MyClass constructor with different arguments using "this(...)". It can call a constructor of the next higher class using "super(...)". It can do neither, in which case the compiler inserts the statement "super()" at the beginning of the constructor.
Eventually, the constructor "Object()" gets called. At that point, we have a chain of partially executed constructors, starting with
"MyClass(int a, int b)" and ending with "Object()". Now, the initialization strings and initialization blocks, then the called constructors, are executed for the top (Object) level. This process repeats going down level by level. Eventually, control is returned to "MyClass(int a, int b)", which finally returns a reference to the newly created and initialized object.
You could do most initialization in your bottom constructor (except for initializing private members of higher classes). However, it is better OO practice to initialize the members of each class with constructors in that class so as to group behavior with state and so that your super class can be more easily shared among subclasses. Similarly, if you have values that can be either specified or defaults, you can keep most of the initialization in a constructor with arguments and provide a constructor with no arguments that calls the first constructor with the default values as arguments.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!