What is meant by Method Overriding?

What is meant by Method Overriding?

Asked on January 18, 2019 in Java.
Add Comment


  • 3 Answer(s)

    Method overriding happens if the sub class method satisfies the below conditions with the Super class method:

    • Method name should be same
    • Argument should be same
    • Return type also should be same

    The key benefit of overriding is that the Sub class can provide some specific information about that sub class type than the super class.

    Example:

    public class Manipulation{ //Super class
    public void add(){
    ………………
    }
    }
    
    Public class Addition extends Manipulation(){
    Public void add(){
    ………..
    }
    Public static void main(String args[]){
    Manipulation addition = new Addition(); //Polimorphism is applied
    addition.add(); // It calls the Sub class add() method
    }
    }

    addition.add() method calls the add() method in the Sub class and not the parent class. So it overrides the Super class method and is known as Method Overriding.

    Answered on January 18, 2019.
    Add Comment

    Method overriding It means one method is available in supper class which is displaying some string but you want to extend this class and also you want to print your own method at that time you need to overriding this method into your local class.
    this is Basic introduction of overriding.

    eg.
    class Animal
    {
    public void displayMessage()
    {
    System.out.println(“Animal is eating”);
    }

    }
    class Dog extends Animal
    {
    public void displayMessage()
    {
    System.out.println(“Dog is eating”);
    }
    public static void main(String arg[])
    {
    Dog d=new Dpg();
    d.displayMessage();
    }
    }
    OUTPUT:

    Dog is earing

    Advantages of Method Overriding is that the class ca give its own specific implementation to a inherited method without any modifying the parent class method.

    Rules of Method overriding is.

    The argument list : the argument list must be same.
    Access Modifier : if you overriding the method you must to give the same modifier of super class method suppose super class have the public method you cannot give the protected or private….etc

    Answered on January 25, 2019.
    Add Comment

    Overriding in Java

    In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.

    overriding in java

    Method overriding is one of the way by which java achieve Run Time Polymorphism.The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed.In other words, it is the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed.

     

    // A Simple Java program to demonstrate 
    // method overriding in java
     
    // Base Class
    class Parent
    {
        void show() { System.out.println("Parent's show()"); }
    }
     
    // Inherited class
    class Child extends Parent
    {
        // This method overrides show() of Parent
        @Override
        void show() { System.out.println("Child's show()"); }
    }
     
    // Driver class
    class Main
    {
        public static void main(String[] args)
        {
            // If a Parent type reference refers
            // to a Parent object, then Parent's
            // show is called
            Parent obj1 = new Parent();
            obj1.show();
     
            // If a Parent type reference refers
            // to a Child object Child's show()
            // is called. This is called RUN TIME
            // POLYMORPHISM.
            Parent obj2 = new Child();
            obj2.show();
        }
    }

    Output:

    Parent's show()
    Child's show()
    

     

    Answered on February 3, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.