Thursday , February 27 2020
Home / Java / JAVA #22 – Method Overriding in Java

JAVA #22 – Method Overriding in Java


Method-Overriding in Java

An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclassoverrides the superclass’s method.

Syntax

public class animal{// Super class
….
}
public class mammals extend animal{ //child class
public static void main(String arg[])
{
…
}
}
//File name Method_overloading.java
package method_overriding;
public class Method_overriding extends Animal{
    
    public void Details()
    {
        System.out.println("This is an Mammal Class");
        System.out.println("This is an Also be Animal Class");
    }
    public void Details(int x, int y) // Over Rided Method
    {
        System.out.println("The height of Mammal is: "+x+ " The weight of Mammal is:"+y);
    }
    
    public static void main(String[] args) {
        Method_overriding objMet = new Method_overriding();
        objMet.Details();
        objMet.Details(5, 80);
    }    
}
// File name Animal.java and must be store in same package of above file.
package method_overriding;
public class Animal {
    public void Details()
    {
        System.out.println("This is an Animal Class");
    }
    public void Details(int x, int y)
    {
        System.out.println("The height of Animal is: "+x+ " The weight of Animal is:"+y);
    }
}

 

About Faheem

Faheem
An adaptable and responsible Software Engineer, who enjoys working with computers and the opportunity to learn new skills and work with new technologies is particularly attractive to me.

Check Also

JAVA #32 – Java application using SQL server (Part: 01)

  In this part we made the GUI front end of a Application.

One comment

  1. Avatar

    Thank you, Thank you! I’ve always wanted to know how to do this and now I can! Can’t wait for more How-to’s from you.

Leave a Reply

Your email address will not be published.