In this post, I’ll explain what method overriding is, the rules of method overriding, and some examples. So let’s get started and learn about method overriding in Java.
- We learned about inheritance in the previous tutorial. Inheritance is an OOP property that allows us to create a new class (subclass) by deriving from an existing class (superclass). The superclass’s attributes and methods are passed down to the subclass.
Contents
What is Method Overriding and how does it work?
When a method in a child class (subclass) has the same name, parameters, and return type (or covariant return type) as a method in its parent class (or super-class), the child class method has overridden the parent class method.
- If the same method is defined in both the superclass and the subclass, the method defined in the subclass takes precedence over the method defined in the superclass. This is referred to as method overriding.
Usage of Java Method Overriding
- Method overriding is used to provide a specific implementation of a method that its superclass already provides.
- For runtime polymorphism, method overriding is used.
Rules for Java Method Overriding
- The method’s name must be the same as it is in the parent class.
- The method’s parameters must be the same as those in the parent class.
- We cannot override the method declared as final and static.
- There must be an IS-A connection (inheritance).
Method Overriding Example
class Teacher { public void displayInfo() { System.out.println("I am a Teacher."); } } class Student extends Teacher { @Override public void displayInfo() { System.out.println("I am a Student."); } } class Main { public static void main(String[] args) { Student S = new Student(); S.displayInfo(); } }
Output:
I am a Student |
- The displayInfo() method is present in both the Teacher superclass and the Student subclass in the preceding program.
- When we use the S object (subclass object) to call displayInfo(), the method inside the subclass Student is called. The subclass’s displayInfo() method overrides the superclass’s displayInfo() method.
In our example, the @Override annotation is used. Annotations are metadata in Java that we use to provide information to the compiler. The @Override annotation tells the compiler that the method following this annotation overrides the superclass method.
It is not required to use @Override. However, when we use this, the method must adhere to all of the overriding rules. Otherwise, an error will be generated by the compiler.
Access Specifiers in Method Overriding
- Different access specifiers can be assigned to the same method declared in the superclass and its subclasses. However, there is a catch.
- We can only use access specifiers in subclasses that provide greater access than the superclass access specifier. As an example:
- Assume a method in the superclass, Sample(), is declared protected. Then, in the subclass, the same method Sample() can be public or protected, but not private.
Example: Access Specifier in Overriding
class Teacher { protected void displayInfo() { System.out.println("I am a Teacher."); } } class Student extends Teacher { @Override public void displayInfo() { System.out.println("I am a Student."); } } class Main { public static void main(String[] args) { Student S = new Student(); S.displayInfo(); } }
Output:
I am a Student |
- In the preceding example, the subclass Student overrides the superclass Teacher method displayInfo().
- When we use the S (subclass object) to call displayInfo(), the method inside the subclass is called.
- It’s worth noting that the displayInfo() method in the Teacher superclass is marked as protected. In the Student subclass, the same method has the public access specifier. This is possible because the general public has greater access than the protected.
Example: Private Methods can not be Overridden
class Parent { //Access modifier of parent method is private // This method can not override the child class method private void display() { System.out.println("parent method is execute"); } } //Subclass class Child extends Parent { //Below method can not overrides the Parent display() method //This method is unique to the child class private void display() { System.out.println("child method is executed"); } } //Driver class public class Main { public static void main(String args[]) { Parent P = new Parent(); P.display(); // this line when execute will throw compiler error Parent P1 = new Child(); P1.display(); // this line when execute will throw compiler error } }
Output:
Another restriction with access modifiers is that you cannot override private methods in the parent class. If a subclass attempts to override a parent class’s private method, the program will throw a compile-time error.
Example: Final Methods can not be Overridden in Java
class Parent { // Can't be overridden final void display() { } } //Child class or Subclass class Base extends Parent { // This would produce compile time error void display() { } }
Output:
/Main.java:13: error: display() in Base cannot override display() in Parent void display() { } ^ overridden method is final |
method overriding in java, method overloading and method overriding in java, method overriding in java example, can we override static method in java, method overloading and overriding in java, which of these is supported by method overriding in java, can we override the main method in java, the difference between method overloading and method overriding in java, override equals method in java.
Example: Static Methods can not be Overridden
//Base class or SuperClass class Parent { static void display() { System.out.println("parent static method is execute"); } void show() { System.out.println("parent non-static method is executed"); } } //Derived class or SubClass class Child extends Parent { //This method hides the Parent display() method static void display() { System.out.println("child static method is execute"); } // This method overrides the Parent show() method void show() { System.out.println("child non-static method is execute"); } } //Driver Class public class Main { public static void main(String args[]) { Parent P = new Child(); // static method can not be overridden, // so below line calls parent display() method P.display(); // Expected child method will run P.show(); } }
Output:
method overriding in java, method overloading and method overriding in java , method overriding in java example, can we override static method in java , method overloading and overriding in java, which of these is supported by method overriding in java, can we override main method in java , difference between method overloading and method overriding in java ,override equals method in java .
parent static method is execute child non-static method is execute |
A method marked static cannot be overridden. Method hiding occurs when a static method in a subclass (child class) has the same method signature as the super class (parent class).
Abstract method and Overriding
A class must be declared abstract if it has one or more abstract methods. An abstract class can have non-abstract methods (the usual type of method). A non-abstract child must define a method with the same signature and return type as each abstract method inherited from its parent.
- Only the first concrete class (classes without abstract methods) can override abstract methods, otherwise, a compiler error will be thrown.
//Abstract class abstract class Parent { public abstract void display( int x, String j); } //Subclass or Derived Class class Child extends Parent { @Override public void display( int x, String j ) { System.out.println("child method is execute"); } } //Driver class public class Main { public static void main(String args[]) { Parent P = new Child(); P.display(5, "DevelopersDome"); } }
Output:
child method is executed |
You may like:
Java final Keyword with Example
Another restriction with access modifiers is that you cannot override private methods in the parent class. If a subclass attempts to override a parent class’s private method, the program will throw a compile-time error.
Hope this article will guide you to recognize all about the Java Inheritance with Example and Types of inheritance that you needed and still if you have any problem or queries regarding this, post them in the comments section and we will be glad to assist you.
Pingback: Java super keyword with Example - Developers Dome
Pingback: Not Equal Example in Java - Developers Dome
Pingback: Java Polymorphism with Example - Developers Dome