- create new class for purpose to reuse / borrow a group of existing attribute / method in existing Abstact / Super Class.
- use extends to calling Super Class for borrow a method.
- example :
superclass:
public class car{
private String carName;
private String carModel;
public car(){
carName=" ";
carModel=" ";
}
public car(String name,String model){
carName=" ";
carModel=" ";
}
public void setCarName(String name){
carName=name;
}
public void setCarType(String model){
carModel=model;
}
public String getCarName(){return carName;}
public String getCarType(){return carModel;}
}
subclass:
public class sportCar extends car{
private String carColor;
public sportCar(String color,String name,String model){
super(color,model);
carColor=" ";
}
public void setCarColor(String color){
carColor=color;
}
public String getCarColor(){return carColor;}
}
POLYMORPHISM
- create a class / had a Subcalss Class for purpose to modify / add a feature that could borrow a attribute / method in existing Higher Class.
- use extends to modify a method in Higher Class.
- example
superclass:
public abstract class car{
private String carName;
private String carModel;
public car(){
carName=" ";
carModel=" ";
}
public car(String name,String model){
carName=" ";
carModel=" ";
}
public void setCarName(String name){
carName=name;
}
public void setCarType(String model){
carModel=model;
}
public String getCarName(){return carName;}
public String getCarType(){return carModel;}
public abstract void calcTotPrice();
}
subclass:
public class carType extends car{
private String carType;
public carType(String color,String name,String model){
super(color,model);
carType=" ";
}
public void setCarType(String type){
carType=type;
}
public String getCarColor(){return carType;}
public void calcTotPrice(){
//statment and calculation;
}
}
REFERENCE