Overriding in Java

What is Overriding in Java?

The act of redefining a method from a parent class in a subclass is known as method overriding in java language.

Method overriding is the act of redefining a superclass method in a subclass. This is also called run-time polymorphism or dynamic binding, because the compiler is unaware of the types of objects provided at compile time.

Overriding in Java

Overriding in Brief

  • In Java, there is a link between parent and child classes that governs inheritance. If two classes have methods with the same name, arguments, or parameters, one of the methods will always execute before the other during execution.The object determines which method will be used.
  • A subclass method overrides a superclass method when the subclass object calls the method. Otherwise, the superclass method is executed when the superclass object calls the method.
  • You cannot override static methods because static methods are bound to classes and instance methods are bound to objects.
  • Static belongs to class space, instance belongs to heap space.
overriding in java

Example:

Run
class Parent {
    public void walk() {
        System.out.println("I walk slowly");
    }
}

class Child extends Parent {
    @Override
    public void walk() {
    System.out.println("I walk faster");
    }
} 

 
public class Main {
    public static void main(String args[])
    {
        Parent pObj = new Parent(); 
        pObj.walk(); 
        Parent cObj = new Child(); 
        cObj.walk(); 
    }
}

Rules for Overriding in Java Language are:

  1. The method name ought to be familiar and identical to that of the parent class.
  2. The method’s parameter list and return type must match those in the parent class exactly.
  3. Between classes, there must be an inheritance relationship.
  4. The child class should implement all of the parent class abstract methods.
  5. The methods cannot be overridden if they were defined as static or final.

Super keyword in Java:

In Java, the super keyword enables access to members of the superclass by subclasses. It is, in essence, a keyword that is used to access objects from parent classes. This keyword is typically used in Java to implement the inheritance notion.

Why it is used?

When the names of a superclass and a subclass are identical, the data members and member functions (or methods). Then, the Java Virtual Machine has a high likelihood of producing ambiguity. We utilise the super keyword to directly access members of superclasses in order to eliminate this misunderstanding caused by members with the same name in two classes.

Example:

Run
class Parent {
    public void walk() {
        System.out.println("I walk slowly");
    }
}

class Child extends Parent {

    public Child(){
	    super.walk();  // super 
    }

    @Override
    public void walk() {
    System.out.println("I walk faster");
    }
} 

 
public class Main {
    public static void main(String args[])
    {
        Parent pObj = new Parent(); 
        pObj.walk(); 
        Parent cObj = new Child(); 
        cObj.walk(); 
    }
}

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription