Contd...
Lets now use the above classes in main program:
Objective: Based on an input from a user, the program has to use code from either Car or BenzCar or BMWCar.
Requirment: If the entry from use is Car then use the code from Car class similary for Benz and BMW.
Solution:
public class InheritanceDemo{
public static void main(String[] args){
if(args[0].equals("Car")){
Car car = new Car();
car.drive(); //Prints This is default drive implementation
car.accelerate(); //Prints This is default accelerate implementation
car.applyBrakes(); //Prints This is default applyBrakes implementation
}
else if(args[0].equals("Benz"){
BenzCar benz = new BenzCar();
benz.drive(); //Prints This is default drive implementation
benz.accelerate(); //Prints Accelerate slowly and steadily.
benz.applyBrakes(); //Prints This is default applyBrakes implementation
}
else if(args[0).equals("BMW"){
BMWCar bmw = new BMWCar();
bmw.drive(); //Prints Drive or move at highest speed all the time
bmw.accelerate(); //Prints Accelerate at brisk speed but steadily.
bwm.applyBrakes(); //Prints This is default applyBrakes implementation
}
else
System.out.println("Invalid Entry");
}
}
Notation: Super-class referencing Sub class object: So far, the code looks simple but there is an issue with the code. It contains lot of repetition.
car.drive();
benz.drive();
bmw.drive();
Like-wise the other methods - accelerate() and applyBrakes().
This repetition has made the program lengthy with more number of lines of code than required. As a result the code's compilation time increases. But there is a facility provided by Java's inheritance to optimize this kind of situations.
Use of the notation: Super class referencing Sub class object
Yes. The Super-class's or Parent-class's object reference can refer to its sub-class's or child class's object.
Car car = new BenzCar();
Car car = new BMWCar();
Lets use this notation in the above program.
public class InheritanceDemo{
public static void main(String[] args){
Car car; //Creating a Super class's object reference.
if(args[0].equals("Car")){
car = new Car(); //Assign a Class's object to its reference.
}
else if(args[0].equals("Benz"){
car = new BenzCar(); //Assign Subclass's object to its SuperClass's reference.
}
else if(args[0).equals("BMW"){
car = new BMWCar(); //Assign Subclass's object to its SuperClass's reference.
}
else
System.out.println("Invalid Entry");
car.drive(); //Prints based on input
car.accelerate(); //Prints based on input
car.applyBrakes(); //Prints based on input
}
}
Now see, how the code has shaped up with fewer number of lines with the new notation.
A Super-Class's object can behave(or morph or act) like its sub-class's object based on the object that it refers. This is called Runtime polymorphism or Dynamic polymorphism. An important concept closely associated with efficient coding.
Notation: Super-class referencing Sub class object: So far, the code looks simple but there is an issue with the code. It contains lot of repetition.
car.drive();
benz.drive();
bmw.drive();
Like-wise the other methods - accelerate() and applyBrakes().
This repetition has made the program lengthy with more number of lines of code than required. As a result the code's compilation time increases. But there is a facility provided by Java's inheritance to optimize this kind of situations.
Use of the notation: Super class referencing Sub class object
Yes. The Super-class's or Parent-class's object reference can refer to its sub-class's or child class's object.
Car car = new BenzCar();
Car car = new BMWCar();
Lets use this notation in the above program.
public class InheritanceDemo{
public static void main(String[] args){
Car car; //Creating a Super class's object reference.
if(args[0].equals("Car")){
car = new Car(); //Assign a Class's object to its reference.
}
else if(args[0].equals("Benz"){
car = new BenzCar(); //Assign Subclass's object to its SuperClass's reference.
}
else if(args[0).equals("BMW"){
car = new BMWCar(); //Assign Subclass's object to its SuperClass's reference.
}
else
System.out.println("Invalid Entry");
car.drive(); //Prints based on input
car.accelerate(); //Prints based on input
car.applyBrakes(); //Prints based on input
}
}
Now see, how the code has shaped up with fewer number of lines with the new notation.
A Super-Class's object can behave(or morph or act) like its sub-class's object based on the object that it refers. This is called Runtime polymorphism or Dynamic polymorphism. An important concept closely associated with efficient coding.
No comments:
Post a Comment