Java Inheritance and Polymorphism Examples
Single Inheritance in Java
This Single Inheritance program demonstrates how a child class inherits properties and methods from a single parent class.
class Animal {
// Constructor of Parent class
Animal() {
System.out.println("Animal constructor called");
}
// Method of Parent class
void eat() {
System.out.println("I can eat");
}
}
// Child class inheriting from Animal
class Dog extends Animal {
// Constructor of Child class
Dog() {
System.out.println("Dog constructor called");
}
// Method of Child class
void bark() {
System.out.println("I can bark");
}
}
// Main class to test inheritance
public class Main {
public static void main(String[] args) {
/...