Java OOP Concepts: Inheritance and Polymorphism Examples
Single Inheritance in Java
Single inheritance involves one class inheriting properties and methods from exactly one parent class. Below is a demonstration using the Animal and Dog classes.
class Animal {
Animal() {
System.out.println("Animal constructor called");
}
void eat() {
System.out.println("I can eat");
}
}
class Dog extends Animal {
Dog() {
System.out.println("Dog constructor called");
}
void bark() {
System.out.println("I can bark");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat();
myDog.bark();
}
}Implementing Multiple Inheritance using Interfaces
Java does not support multiple inheritance... Continue reading "Java OOP Concepts: Inheritance and Polymorphism Examples" »
English with a size of 3.1 KB