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) {
// Creating an object of Dog class
Dog myDog = new Dog();
// Calling inherited method from Animal class
myDog.eat();
// Calling method from Dog class
myDog.bark();
}
}Multiple Inheritance Using Interfaces
This Multiple Inheritance program shows how Java uses interfaces to implement multiple inheritance, as classes cannot extend more than one parent.
// First interface
interface Animal {
void eat();
}
// Second interface
interface Mammal {
void sleep();
}
// Class implementing multiple interfaces
class Dog implements Animal, Mammal {
public void eat() {
System.out.println("Dog is eating.");
}
public void sleep() {
System.out.println("Dog is sleeping.");
}
}
// Main class to test multiple inheritance
public class Main {
public static void main(String[] args) {
Dog obj = new Dog();
obj.eat();
obj.sleep();
}
}Method Overloading in Java
The following Method Overloading program illustrates how to define multiple methods with the same name but different signatures within the same class.
class OverloadExample {
// Method with one parameter
void display(int a) {
System.out.println("Integer: " + a);
}
// Overloaded method with two parameters
void display(int a, int b) {
System.out.println("Two Integers: " + a + " and " + b);
}
// Overloaded method with different data type
void display(double a) {
System.out.println("Double: " + a);
}
public static void main(String[] args) {
OverloadExample obj = new OverloadExample();
obj.display(5); // Calls method with one int parameter
obj.display(10, 20); // Calls method with two int parameters
obj.display(3.14); // Calls method with double parameter
}
}Method Overriding in Java
This Method Overriding program demonstrates how a subclass provides a specific implementation of a method that is already provided by its parent class.
// Parent class
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
// Child class overriding the method of Parent class
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Dog barks");
}
}
// Main class to test method overriding
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal();
myAnimal.makeSound(); // Calls Parent class method
Animal myDog = new Dog();
myDog.makeSound(); // Calls overridden method in Child class
}
}
English with a size of 3.49 KB