Java Method Overriding
Declaring a method in the subclass which already exists there in the parent class is known as method overriding. When a class is inheriting a method from a superclass of its own, then there is an option of overriding the method provided it is not declared as final. The advantage of using overriding is the ability to classify a behavior that’s specific to the child class and the child class can implement a parent class method based on its necessity.
There are certain rules that a programmer should follow in order to implement overriding. These are:
Java Program to Demonstrate Method Overriding
Example:
class college {
public void move() {
System.out.println("College is open");
}
}
class univ extends college {
public void move() {
System.out.println("University is open too");
}
}
public class stud {
public static void main(String args[]) {
college a = new college();
college b = new univ();
a.move();
b.move();
}}
Comments
Post a Comment