javascript
Brief description  about Online courses   join in Online courses
View Shristy  Bhati 's Profile

Please Solve this doubt

How we can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super?
Asked by Shristy Bhati | Jun 16, 2013 |  Reply now
Replies (2)
View shristy bhati 's Profile
thank you sir.....
Jun 19, 2013
View java teacher 's Profile

A constructor from a subclass can call constructors from the superclass, but they're not inherited as such.

To be clear, that means if you have something like:

public class Super
{
public Super(int x)
{
}
}

public class Sub extends Super
{
public Sub()
{
super(5);
}
}

then you can't write:

new Sub(10);
because there's no Sub(int) constructor.



It may be helpful to think of constructors as uninherited static methods with an implicit parameter of the object being initialized.


Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.


One of the main reasons is because you probably don't want to override the super classes constructor, which would be possible if they were inherited.

By giving the developer the ability to override a super classes constructor you would erode the encapsulation abilities of the language.

Ok.

Jun 18, 2013