Skip to content

Override Java | Method Overriding Java | Examples

  • by

If any class (subclass) has the same methods as its parent class then it’s called a Method Overriding Java. In subclass(child class) override methods are shown with @override Java annotation.

Methods overriding is also called runtime polymorphism in Java. It’s achieved by an Inheritance a one of Object-Oriented Programming (OOps) concept.

Override Java Equals Method Overriding Java Examples

In this tutorial, you will learn about What is Methods Overriding and How to use it with some basic examples in Java.

Overriding Syntax

A Simple syntax of Java Overriding.

class ParentClass {
    //method
    .....
}

class SubClass extends ParentClass{
    @Override
    //method
    .....
}
class SubClass extends ParentClass{
    @Override
    //method
    .....
}

Complete Java overriding example

How to do methods overriding in java? Here is below an example of Overriding a salary() Method in a subclass.

//base class
class Employee {
    //method
    void salary() {
        System.out.println("All Employee Salary Range is 10-50k");
    }
}

//subclass

class Manager extends Employee{
    @Override
    void salary(){
        System.out.println("Manager Salary  is 40k");
    }
}

public class MethodOverriding {

    public static void main(String arg[]){

        // creating base (parent) class object
        Employee employee = new Employee();
        employee.salary();

        // creating sub (child) class object
        Employee employee1 = new Manager();
        employee1.salary();

    }
}

Output: All Employee Salary Range is 10-50k
Manager Salary is 40k

When to apply Override Java in methods?

Here is a complete and real-time example of “Override Java”.

In every company, there is a different salary according to the position. So we are defining salary() methods that are overridden. A class Manager and team lead extend (inherit) in the Employee class.

complete and real time example of Override Java

Code:

//base class
class Employee {
    //method
    void salary() {
        System.out.println("All Employee Salary Range is 10-50k");
    }
}

//subclass

class Manager extends Employee {
    @Override
    void salary() {
        System.out.println("Manager Salary is 40k");
    }
}
//subclass

class TeamLead extends Employee {
    @Override
    void salary() {
        System.out.println("Team Lead Salary is 30k");
    }
}

public class MethodOverriding {

    public static void main(String arg[]) {

        // creating base (parent) class object
        Employee employee = new Employee();
        employee.salary();

        // creating sub (child) class object
        Employee employee1 = new Manager();
        employee1.salary();

        // creating sub (child) class object
        Employee employee2 = new TeamLead();
        employee2.salary();

    }
}

Output: All Employee Salary Range is 10-50k
Manager Salary is 40k
Team Lead Salary is 30k

Usage of Java Method Overriding

  • A specific statement of superclass methods in the subclass. This means in a subclass you can write your own statement (code) to do the operation.
  • To achieve a Runtime polymorphism

Rules for Java Method Overriding

  • The subclass method must have the same name as in the parent class.
  • Overridden methods parameters(argument) must be the same as parent class methods.
  • Required an IS-A relationship (inheritance).
  • The return type should be the same as the parent class.
  • An access level of methods can’t be more restrictive. Example – If the superclass method is declared public then the overriding method in the subclass can’t be either private or protected.
  • Final methods can’t be overridden.
  • Static methods can’t be overridden but can be re-declared.
  • Constructors can’t be overridden.

Why and When super Keyword is used in java overriding?

Answers: When you want a use superclass method in the subclass and that same methods already overridden in a subclass. Here is an example of how to use super keyword in java overriding.

//super class
class Office {
    //method
    void workHour() {
        System.out.println("All Employee Work Hour is 5 to 8 Hour");
    }
}

//sub class
class Manager extends Office {
    @Override
    void workHour() {
        super.workHour();
        System.out.println("Manager working for 20 hours a day");
    }
}

public class MethodOverriding {

    public static void main(String arg[]) {

        // creating base (parent) class object
        Manager manager = new Manager();
        manager.workHour();

    }
}

Output: All Employee Work Hour is 5 to 8 Hour
Manager working for 20 hours a day

Question: When do you use Java’s @Override annotation and why?

Answers: Override java annotation Use it every time when you override a method. here is we are mentioning 2 reasons or benefits of it.

  1. By doing you can take advantage of compiler checking your methods actually are overridden. You can prevent a common mistake of misspelling a method name or not correctly matching the parameters. IDE will show you an error that does not actually a problem in method override.
  2. Secondly, it makes your code easier to understand, easily can find this method is overridden.

Question: Does constructor Overriding in Java possible?

Answers: No, constructor Overriding is not possible because JVM will call the superclass constructor while creating the child class instance.

Whenever a subclass instance created the base class constructor invoked. Then continue with the subclass constructor statements.

Java Constructors are not methods that can be overridden.

Question: Can you override the java main method?

Answers: No, you can’t override the main method in Java because it’s a static method.

Question: Why Java is not allowed to Override Static methods?

Answers: Java Static methods are treated as global by the JVM, there are not bound to an object instance at all.

Question: Can you override a static method?

Answers: No, a static method can’t be overridden in Java. Because Overriding is an example of run-time polymorphism.

Do comment if you have any doubts and suggestions below.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Method Overriding Java Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading