Think of a scenario where a child class has the same method as the parent class, but implemented differently, that is overriding. Many new...
Think of a scenario where a child class has the same method as the parent class, but implemented differently, that is overriding.
Many newbies in java confuse method overloading and overriding.
Many newbies in java confuse method overloading and overriding.
Uses of Java overriding methods.
1.Provide an implementation of a method that is contained in the superclass.
2.Used for runtime polymorphism.
Rules to follow when overriding a method
1.The method should have the same name as contained in the superclass.
2.The method should take the same parameters as in superclass
3.Must be in IS-A inheritance.
Overriding Java method example code
Super class
public class Animal { void eat() { System.out.println("All Animals eat"); } }
Child class
public class Cat extends Animal { void eat()//overriding the super class method eat. { System.out.println("Cat eats meat"); } public static void main(String args[]) { Cat c= new Cat(); c.eat(); }}