Certainly! Inheritance is one of the core concepts of OOP and Java handles inheritance through the extends keyword for classes.
Single Inheritance
Single inheritance is where a class extends one other class. This is a simple scenario where subclasses inherit features from one superclass.
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class SingleInheritanceDemo {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Inherits from Animal class
myDog.bark();
}
}
Multilevel Inheritance
This type of inheritance follows a parent-child-grandchild relationship where a class extends a class that extends another class.
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("The puppy weeps.");
}
}
public class MultilevelInheritanceDemo {
public static void main(String[] args) {
Puppy myPuppy = new Puppy();
myPuppy.eat(); // Inherits from Animal
myPuppy.bark(); // Inherits from Dog
myPuppy.weep();
}
}
Hierarchical Inheritance
In this type of inheritance, multiple subclasses extend from a single superclass.
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
class Cat extends Animal {
void meow() {
System.out.println("The cat meows.");
}
}
public class HierarchicalInheritanceDemo {
public static void main(String[] args) {
Dog myDog = new Dog();
Cat myCat = new Cat();
myDog.eat();
myDog.bark();
myCat.eat();
myCat.meow();
}
}
Multiple Inheritance (via Interfaces)
Java does not support multiple inheritances directly (i.e., a class cannot inherit from more than one class), but it can be achieved using interfaces.
interface FlyingAnimal {
void fly();
}
interface SwimmingAnimal {
void swim();
}
class Duck implements FlyingAnimal, SwimmingAnimal {
public void fly() {
System.out.println("The duck can fly.");
}
public void swim() {
System.out.println("The duck can also swim.");
}
}
public class MultipleInheritanceDemo {
public static void main(String[] args) {
Duck myDuck = new Duck();
myDuck.fly();
myDuck.swim();
}
}
Using super and Method Overriding
super is used to refer to the parent class methods and variables, and method overriding allows a subclass to provide a specific implementation of a method that is already present in its superclass.
class Animal {
void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
void move() {
super.move(); // Invokes the super class method
System.out.println("Dogs can walk and run");
}
}
public class MethodOverridingDemo {
public static void main(String[] args) {
Animal a = new Animal();
Animal b = new Dog(); // Dog object, but upcast to Animal
a.move(); // Outputs: Animals can move
b.move(); // Outputs: Animals can move
// Dogs can walk and run
}
}
Abstract Classes and Inheritance
You can also use abstract classes to define methods that must be implemented by all subclasses.
abstract class Animal {
abstract void move();
}
class Bird extends Animal {
void move() {
System.out.println("The bird flies.");
}
}
class Fish extends Animal {
void move() {
System.out.println("The fish swims.");
}
}
public class AbstractClassDemo {
public static void main(String[] args) {
Animal myBird = new Bird();
Animal myFish = new Fish();
myBird.move();
myFish.move();
}
}
Inheritance with Constructor Calls
All classes in Java inherit from the Object class. Constructor calls are chained from subclass to superclass and finally to Object.
class Animal {
Animal() {
System.out.println("A new animal has been created!");
}
void display() {
System.out.println("This is an animal.");
}
}
class Dog extends Animal {
Dog() {
super(); // Calls the parent constructor
System.out.println("A new dog has been created!");
}
void display() {
super.display(); // Calls the superclass' display method
System.out.println("This is a dog.");
}
}
public class ConstructorInheritanceDemo {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.display();
}
}
Advanced Scenario: Diamond Problem with Default Methods
The diamond problem is a case where a class implements two interfaces that have default methods with the same signature. Java resolves this by forcing the implementing class to override the default method.
interface FirstInterface {
default void show() {
System.out.println("Default method from FirstInterface");
}
}
interface SecondInterface {
default void show() {
System.out.println("Default method from SecondInterface");
}
}
class DemoClass implements FirstInterface, SecondInterface {
public void show() {
FirstInterface.super.show(); // Calls show from FirstInterface
SecondInterface.super.show(); // Calls show from SecondInterface
System.out.println("Override the method in DemoClass");
}
}
public class DiamondProblemDemo {
public static void main(String[] args) {
DemoClass obj = new DemoClass();
obj.show();
}
}
These examples cover various scenarios of inheritance in Java, from basic to more advanced ones. Mastery of inheritance requires understanding these patterns and recognizing when and how to apply them in your code.