Write a Java program to demonstrate method overriding with a subclass that overrides a method and narrows the access modifier from public to protected
Class hierarchy with a base class Animal and a subclass Dog. The makeSound method is declared in the Animal class and overridden in the Dog class. Here's a breakdown of what your code does:
When you run the code, it will create a Dog object and call the makeSound method. Since the makeSound method is overridden in the Dog class, it will print "Dog barks" to the console.
class Animal { protected void makeSound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { @Override protected void makeSound() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.makeSound(); } }
Dog barks
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions