JAVA TUTORIAL

Java Intro Java Features Java vs C++ Java Simple Program Java Applet vs Application JVM,JDK or JRE Java Keywords Java Variables Java Literals Java Separators Java Datatypes Java Operators Java Statements Java Array Java String

CONTROL STATEMENTS

Java If-else Java While Loop Java Do-While Loop Java For Loop Java For-each Loop Java Switch Java Break/Continue

JAVA METHODS

Java Method Java Overloading Java Overriding

JAVA CLASSES

Java OOPs Java Classes/Objects Java Inheritance Java Polymorphism Java Encapsulation Java Abstraction Java Modifiers Java Constructors Java Interface Java static keyword Java this keyword

Method Overriding in Java

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

Rules of Method Overriding in Java

1. Method name must be same for both parent and child classes.

2. The parameter of the base class should be the same as that of the parent class.

3. Access modifier of child method must not restrictive than parent class method.

4. Private, final and static methods cannot be overridden.

5. The relationship must be an IS-A relationship between the child class as well as the parent class.

Example:

//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

Example 2:

//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

Example 3:

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

Overriding and Access-Modifiers :

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.

Example:

// 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()

Difference between Overloading and Overriding

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