Wednesday, June 3, 2015

Super (Constructor Behaviour)2



// super() should be the first statement in this case with BB's constructor, otherwise we get compile error

// Any constructor inside a subClass will try to call the default constructor of the superClass by default,
// if there is no default constructor in the superClass, you must use the super() keyword
// in all subClass constructors to explicitly call some other constructor defined in the superClass


public class COnstructorBehaviourWithSuperDemo2
{
public static void main(String[] args)
{
BB b = new BB(22, "Bipen");
}
}


class AA
{
int phNo;

public AA(int phone)
{
phNo = phone;
System.out.println("SuperClass constructor called");
}
}


class BB extends AA
{
String addr;

public BB() {
super(0);  // upper statement
System.out.println("Default Constructor BB");
}
public BB(int phone , String address)
{
super(phone); // super class  variable initialization
addr = address;
System.out.println("Sub Class constructor is called");
System.out.println( "you got vars from super class and subclass constructor:   "+ addr+" "+ phNo);
}
}





No comments:

Post a Comment