Method overriding is a process of overriding base class method by derived class method with more specific definition.
In overriding, method of both class must have same name and equal number of parameters.
Method overriding performs only if two classes have is-a relationship. It mean class must have inheritance. In other words, It is performed between two classes using inheritance relation.
In java method overriding is also referred to as runtime polymorphism because calling method is decided by JVM during runtime.
To override a method in a subclass, the method must be defined in the subclass using the same signature and same return type as in its superclass
//Java Program to illustrate the use of Java Method Overriding
//Creating a parent class.
class MyVehicle{
void run(){
System.out.println("My vehicle is running");
}
}
//Creating a child class
class MyCar2 extends MyVehicle{
void run(){
System.out.println("My car is running");
}
public static void main(String args[]){
MyCar2 obj = new MyCar2();//creating object
obj.run();//calling method
}
}
Output:
My car is running
//Java Program to illustrate the use of Java Method Overriding
//Creating a parent class.
class Vehicle{
//defining a method
void run()
{
System.out.println("Vehicle is running");
}
}
//Creating a child class
class Bike2 extends Vehicle{
//defining the same method as in the parent class
void run()
{
System.out.println("Bike is running");
}
public static void main(String args[]){
Bike2 bobj = new Bike2();//creating object
bobj.run();//calling method
}
}
Output:
Bike is running
class Sum
{
void sum(int a)
{
System.out.println("Area is: "+(a*a));
}
}
class Sum2 extends Sum
{
void sum(int a)
{
System.out.println("Area is: "+Math.sqrt(a));
}
}
class Simple
{
public static void main(String []args)
{
Sum2 obj = new Sum2();
obj.sum(9);
}
}
Output:
Area is: 3.0
The access modifier for an overriding method can allow more, but not less, access than the overridden method. For example, a protected instance method in the super-class can be made public, but not private, in the subclass. Doing so, will generate compile-time error.
// A Simple Java program to demonstrate
// Overriding and Access-Modifiers
class Parent {
// private methods are not overridden
private void method1()
{
System.out.println("From parent method1()");
}
protected void method2()
{
System.out.println("From parent method2()");
}
}
class Child extends Parent {
// new method1() method
// unique to Child class
private void method1()
{
System.out.println("From child method1()");
}
// overriding method
// with more accessibility
@Override
public void method2()
{
System.out.println("From child method2()");
}
}
// Driver class
class Simple {
public static void main(String[] args)
{
Parent obj1 = new Parent();
obj1.method2();
Parent obj2 = new Child();
obj2.method2();
}
}
Output:
From parent method2()
Form child method2()
Method Overloading | |
---|---|
Parameter must be different and name must be same. | Both name and parameter must be same. |
Compile time polymorphism. | Runtime polymorphism. |
Increase readability of code. | Increase reusability of code. |
It is performed within a class | It is performed between two classes using inheritance relation. |
It is performed between two classes using inheritance relation. | It requires always inheritance. |
It can not have the same return type. | It should always have the same return type. |
It can be performed using the static method | It can not be performed using the static method |
It uses static binding | It uses the dynamic binding. |
It is code refinement technique. | It is a code replacement technique. |
Private, static, final methods can be overloaded | Private, static, final methods can not be overloaded |
It is also known as Compile time polymorphism or static polymorphism or early binding | It is also known as Run time polymorphism or Dynamic polymorphism or Late binding |