As we know instance variable can v initialized by constructor while creating Object
Now we need to know:
Now we need to know:
How to initialize the instance variables of both parent class and child class using constructors ?
Constructor Invocation
- Base class constructor can not be inherited.
- To initialize the base class member variable which are to be used by derived class Base class constructor must be invoked,during the Object creation of derived class.
- Base class constructor can be Default/parameterised constructor
- If Base class has default constructor then it is invoked by creating the object of Base/Derived class.
- For parameterised constructor derived class has to invoke base class constructor explicitly from the Derived class constructor only
Super keyword
- Using super we can invoke base class constructor from derived class constructor
- It is used to prevent instance variable hiding
- It is used to refer member variable of base class
- Some of the point need to keep in mind while using super
- super can be only used in derived class constructor not in any other method
- super call must be the first line of derived class constructor.
- If the derived class does not explicitly call a base class constructor then Java automatically invoke the base class default constructor just befor the code inside the derived class constructor executes
class Customer{
private int customerId;
private String
private long contactNos[]=new long[3];
private static int counter=1000;
public Customer(String customerName, long teleNo1, long teleNo2, long teleNo3){
customerId=++counter;
this.customerName=customerName;
…….
}
public int getCustomerId(){
return customerId;
}
}
class RegularCustomer extends Customer{
private float discount;
public RegularCustomercustomerName, long teleNo1, long teleNo2, long teleNo3,float discount){
super(customerName,teleNo1,teleNo2,teleNo3);
this.discount=discount;
}
public float getDiscount(){
return discount;
}
}
class PrivilegedCustomer extends Customer{
private String memCardType;
public PrivilegedCustomer (String customerName, long teleNo1, long teleNo2, long teleNo3,String cardType){
super(customerName,teleNo1,teleNo2,teleNo3);
this.memCardType=cardType;
}
public String getMemCardType(){
return memCardType;
}
}
class Retail{
public static void main(String args[]){
RegularCustomer regObj=new RegularCustomer("John",9980788712L,9886124566L,9496781256L,20.0f);
PrivilegedCustomer prvObj=new PrivilegedCustomer("Jennifer",2544871,989789956,949629145,"Silver");
PrivilegedCustomer prvObj=new PrivilegedCustomer("Jennifer",2544871,989789956,949629145,"Silver");
System.out.println("Regular Customer Details");
System.out.println("Customer Id:" +regObj.getCustomerId());
System.out.println("Discount:"+regObj.getDiscount());
System.out.println("Privileged Customer Details");
System.out.println("Customer Id:"+prvObj.getCustomerId());
System.out.println("Membership Card Type:"+prvObj.getMemCardType());
}
}
Output:
Regular Customer Details
Customer Id:1001
Discount:20.0
Privileged Customer Details
Customer Id:1002
Membership Card Type:Silver
0 comments :
Post a Comment