Method Overriding


Method Overriding :
  
Explanation:  
         Method overriding allows a subclass to provide a specific implementation for a method defined in its superclass.

Example:

class Animal {

    void makeSound() {

        System.out.println("Animal makes a sound.");

    }

}

 

class Cat extends Animal {

    @Override

    void makeSound() {

        System.out.println("Cat meows.");

    }

}

 

public class Main {

    public static void main(String[] args) {

        Animal myCat = new Cat();

        myCat.makeSound(); // Output: Cat meows.

    }

}

Comments

Popular posts from this blog

Installing MySQL and MySQL Workbench

Java Program to Check Palindrome Number

Scenario : 1