Java – Method Overriding
If a method from subclass and a method from its superclass have same names and syntax, then it is known as method overriding in Java.
We all know that if the members of subclass and superclass have same name and syntax, then the members of subclass always dominate over superclass’ members in a subclass.
Similarly in method overriding when we try to execute overridden method in the subclass using subclass’ object reference then it always executes method defined by the subclass.
Example
class X { public void display() { System.out.println("In X's class display method"); } } class Y extends X { public void display() // overridden method { System.out.println("In Y's class display method"); } } class OverriddenDemo { public static void main(String args[]) { Y y = new Y(); y.display(); // call to Y’s class display method } }
Output
In Y's class display method
So what if we want to execute display method of superclass X?
Simple, just make the use of keyword super with method display() as super.display() in subclass’ display() method.
Example
class A { public void display() { System.out.println("In A's class display method"); } } class B extends A { public void display() { super.display(); // call to A’s class display method System.out.println("In B's class display method"); } } class OverridenDemo { public static void main(String args[]) { B b = new B(); b.display(); } }
Output
In A's class display method In B's class display method
Using Superclass’ Reference to call overridden methods
Yes, it is possible to use superclass’ reference to call overridden methods.
For that we need to assign superclass’ reference with its subclass’ reference or object.
Call to the overridden method is decided at run time.
Example
class Vehicle { public void wheels() { System.out.println("Depends upon type of vehicle"); } } class Car extends Vehicle { public void wheels() { System.out.println("Car has 4 wheels"); } } class SuperclassRef { public static void main(String args[]) { Vehicle v = new Vehicle(); Car c = new Car(); v.wheels(); v = c; // reference of Car assigned to reference of Vehicle v.wheels(); } }
Output
Depends upon type of vehicle Car has 4 wheels
In the above program, we have assigned subclass’ object to superclass’ reference like
v = c;
This process is also known as upcasting.
Note
To use superclass’ reference to call subclass’ method, a method of the subclass must override superclass method otherwise reference of superclass will not be able to call subclass method.
Up-casting and Down-casting
When an object of a subclass is referred by reference of its superclass, it is known as upcasting.
When the reference of superclass pointing to the subclass object is cast to subclass type it is known as downcasting.
Example, up-casting and down-casting
class Vehicle { public void start() { System.out.println("Vehicle Started"); } public void use() { System.out.println("For transportation"); } } class Car extends Vehicle { public void start() { System.out.println("Car started"); } public void name() { System.out.println("Ferrari"); } } class UpDownCastDemo { public static void main(String args[]) { Car c1 = new Car(); Vehicle v1 = new Vehicle(); c1.start(); c1.name(); System.out.println("---------------------"); v1.start(); v1.use(); System.out.println("---------------------"); // Up-casting Vehicle v2 = c1; v2.start(); //v2.name(); -- name() is undefined for the type vehicle -- v2.use(); System.out.println("---------------------"); //Down-casting Vehicle v3 = new Car(); Car c2 = (Car)v3; c2.start(); c2.name(); c2.use(); //-- Runtime error -- //Car c3 = (Car)new Vehicle(); //c3.start(); //c3.name(); //c3.use();*/ } }
Output
Car started Ferrari --------------------- Vehicle Started For transportation --------------------- Car started For transportation --------------------- Car started Ferrari For transportation
Leave a reply