eg 2.4.3
class Animal {
void eat() {
System.out.println("Animal eats");
}
}
class Dog extends Animal {
void eat() {
System.out.println("Dog eats");
}
void bark() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Dog();
myAnimal.eat();
}
}
eg 2.4.4
interface Animal {
void makeSound();
}
class Dog implements Animal {
@Override
public void makeSound() {
System.out.println("Woof!");
}
}
class Cat implements Animal {
@Override
public void makeSound() {
System.out.println("Meow!");
}
}
public class InterfaceExample {
public static void main(String[] args) {
Animal myPet = new Dog();
myPet.makeSound();
myPet = new Cat();
myPet.makeSound();
}
}