Day 12 - 27 Sep - Override
Java Override
class Animal {
void makeSound() {
System.out.println("Some Animal sound");
}
}
class Dog extends Animal {
@Override void makeSound() {
System.out.println("Dog Sound Bark");
}
}
class Cat extends Animal {
@Override void makeSound() {
System.out.println("Cat Sound Meow");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
Animal commonAnimal = new Animal();
Animal myDog = new Dog();
Animal myCat = new Cat();
commonAnimal.makeSound(); // Calls Common Animals Sound
myDog.makeSound(); // Calls Dog's makeSound method
myCat.makeSound(); // Calls Cat's makeSound method
}
}Last updated