Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Method Overriding in Java

    A feature known as overriding in any object-oriented programming language enables a subclass or child class to offer a particular implementation of a method that is already given by one of its super-classes or parent classes. Whenever a method in a subclass shares the same name, parameters, signature, and return type (or sub-type) as a method in its superclass, the subclass method is said to override the superclass method.

Table Of Content

  • Method Overriding in Java
  • Usage of Java Method Overriding
  • Why we use Method Overriding
  • Can we override the static method






Method overriding is one approach for Java to implement Run Time Polymorphism. The object that is used to initiate a method determines the version of the method that is executed.

When a method is called by an object of a parent class, the parent class's method definition is executed. But when a method is called from an object of a subclass, the version of the child class's (subclass') method defined is executed. In other words, which version of an overridden method gets performed is determined by the type of the object being referenced to (not the type of the reference variable).

Java refers to this as method overriding when a subclass (child class) has a method that is identical to one that is declared in the parent class.
In simple terms, method overriding refers to the process of defining own unique implementation of a method by subclass that was originally defined by its parent class.

Java method overridden example
 // Java method overridden example  
class Animal {
    void Move(){
        System.out.println("Animals is Moving");
    }
    void Eat(){
        System.out.println("Animals is Eating");
    }
}
  
class Dog extends Animal {
    @Override
    void Move(){ // here overriding take place
        System.out.println("Dog is Moving");
    }
    @Override
    void Eat(){
        System.out.println("Dog is Eating");
    }
}
class DockerTpoint {
    public static void main(String[] args)
    {
    	Animal animal = new Animal(); // If a Animal type reference refers to a Animal object, then Animal
    	animal.Move();        // move or eat is called
    	animal.Eat();
    	
    	Animal animal2 = new Dog(); // If a Animal type reference refers
        animal2.Move();// to a Dog (Child) object Dog's eat() & Move(), 
        animal2.Eat(); // then it is called. This is called RUN TIME POLYMORPHISM. 
    }
}




Output:

Animals is Moving 
Animals is Eating 
Dog is Moving 
Dog is Eating 

Usage of Java Method Overriding

  • Method overriding is used to give a customised implementation of a method that its superclass already provides.
  • Runtime polymorphism is achieved by method overriding.

Rules for Java Method Overriding

  • The method's name has to match that of the parent class.
  • The method's parameters must match those in the parent class.
  • There needs to be an IS-A connection (inheritance).

Why we use Method Overriding ?

  • Java is able to support run-time polymorphism thanks to overridden methods. The ability of a general class to specify methods that will be shared by all of its derivatives while allowing subclasses to define the specific implementation of some or all of those methods makes polymorphism a necessary component of object-oriented programming. Another way Java implements polymorphism's "one interface, multiple methods" principle is through overridden methods.



  • One of the most powerful tools that object-oriented design brings to bear on code reuse and resilience is Dynamic Method Dispatch. The ability to use code libraries to call methods on new class instances without recompiling while retaining a clean abstract interface is a really useful tool.
  • Overridden methods enable us to call methods from any derived class without knowing the type of derived class object.

Java program with Method Overriding
 // Java program with method Overriding  
class Employee {
    public static int Salary = 15000;
    void salary(){
        System.out.println("Salary : "+Salary);
    }
}
class Manager extends Employee { // salary metod get override bt salary() of Employee Class 
    void salary(){
        System.out.println("Manager Salary : "+(Salary+10000));
    }
}

class Clerk extends Employee {
    void salary(){    // salary metod get override bt salary() of Employee Class 
        System.out.println("Clerk Salary : "+(Salary+20000));
    }
}
  
class DockerTpoint {
    public static void main(String[] args){
        Employee employee = new Manager();
        employee.salary();
      
        Employee employee2 = new Clerk();
        employee2.salary();
    }
}



Output:

Manager Salary : 25000 
Clerk Salary : 35000 


Method Overriding MCQ

  • No, you cannot override a static method. Because static methods are connected using static binding at compile time. As a result, we can't override static methods in Java.
  • We do not override static methods since they are bound to a class, whereas instance methods are bound to an object. A static object belongs in the class area, while an instance belongs in the heap area.
  • No, because the main method in java is static method.



  • No, methods designated as final cannot be overridden or concealed. As a result, a method should be declared final only when we are certain that it is complete.
  • Because private methods are bonded during build time, they cannot be overridden. As a result, we are unable to override private methods in a subclass.
  • It is possible to have a distinct return type for an overriding method in a child class starting with Java 5.0, but the child's return type must be a sub-type of the parent's return type. This is referred to as covariant return type.
  • We can't override constructor since parent and child classes can't have constructors with the same name.



  • Method overriding in Java is also known as run time polymorphism because the methods are resolved while the programme is running rather than at compilation time. Only one class' method will be called when a programme is run, out of a large number of classes. During runtime, a decision is made.
Java Runtime polymorphism Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Difference between Early and Late Binding in Java
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.