Constructor is a block of code that allows you to create an object of class and has same name as class with no explicit return type.
Whenever a class (child class) extends another class (parent class), the sub class inherits state and behavior in the form of variables and methods from its super class but it does not inherit constructor of super class because of following reasons:
◉ Constructors are special and have same name as class name. So if constructors were inherited in child class then child class would contain a parent class constructor which is against the constraint that constructor should have same name as class name. For example see the below code:
class Parent {
public Parent()
{
}
public void print()
{
}
}
public class Child extends Parent {
public Parent()
{
}
public void print()
{
}
public static void main(String[] args)
{
Child c1 = new Child(); // allowed
Child c2 = new Parent(); // not allowed
}
}
0 comments:
Post a Comment