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); } }
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.