In the earlier episode, we conversed concerning superclasses and subclasses. If a class takes over a technique from its superclass, then there is a probability to dominate the technique if it is not spotted final.
The advantage of superseding is: the aptitude to describe a behavior that is precise to the subclass type, which signifies a subclass, can realize a parent class process based on its condition.
In object-oriented words, overriding denotes to supersede the functionality of an obtainable method.
Let us look at an example.
class Animal { public void move() { System.out.println("Animals can move"); } } class Dog extends Animal { public void move() { System.out.println("Dogs can walk and run"); } } public class TestDog { public static void main(String args[]) { Animal a = new Animal(); // Animal reference and object Animal b = new Dog(); // Animal reference but Dog object a.move(); // runs the method in Animal class b.move(); // runs the method in Dog class } }
This will produce the following result:
Animals can move Dogs can walk and run
In the over instance, you can perceive that still though b is a kind of Animal it jogs the shifting process in the Dog class. The motive for this is: In accumulated time, the confirmation is made on the position type. However, in the runtime, JVM figures out the thing type and would sprint the technique that belongs to that exacting object.
Consequently, in the over instance, the agenda will amass properly while Animal class has the technique shift. Then, at the runtime, it lopes the technique exact for that object.
Consider the following example:
class Animal { public void move() { System.out.println("Animals can move"); } } class Dog extends Animal { public void move() { System.out.println("Dogs can walk and run"); } public void bark() { System.out.println("Dogs can bark"); } } public class TestDog { public static void main(String args[]) { Animal a = new Animal(); // Animal reference and object Animal b = new Dog(); // Animal reference but Dog object a.move(); // runs the method in Animal class b.move(); // runs the method in Dog class b.bark(); } }
This will produce the following result
TestDog.java:26: error: cannot find symbol b.bark(); ^ symbol: method bark() location: variable b of type Animal 1 error
This plan will fling an accumulated time fault since b's position type Animal never have a technique by the first name of bark.
When invoking a superclass version of an overridden method the super keyword is used.
Learn how 2 solve the Rubiks Cube with the layer method, learning only six algorithms.
class Animal { public void move() { System.out.println("Animals can move"); } } class Dog extends Animal { public void move() { super.move(); // invokes the super class method System.out.println("Dogs can walk and run"); } } public class TestDog { public static void main(String args[]) { Animal b = new Dog(); // Animal reference but Dog object b.move(); // runs the method in Dog class } }
This will produce the following result:
Animals can move Dogs can walk and run
Here at Intellinuts, we have created a complete Java tutorial for Beginners to get started in Java.