Inheritance
PROGRAM 1:SIMPLE INHERITANCE
// Parent class
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
// Child class
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
// Child class
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
// Child class
class Cow extends Animal {
void sound() {
System.out.println("Cow moos");
}
}
// Main class
public class Geeks {
public static void main(String[] args) {
Animal a;
a = new Dog();
a.sound();
a = new Cat();
a.sound();
a = new Cow();
a.sound();
}
}
PROGRAM 2:
Types of Inheritance in Java
1) Single Inheritance
2) Multilevel Inheritance
3) Hierarchical Inheritance
4) Multiple Inheritance (Through Interfaces)
5) Hybrid Inheritance
Polymorphism
Method overriding in Java Inheritance