Java Inheritance in Java
Inheritance in Java
Inheritance is a fundamental concept in object-oriented programming (OOP). In Java, inheritance allows one class to inherit the properties and behaviors (fields and methods) of another class. It helps in code reuse and establishing a relationship between different classes.
Key Concepts:
Super Class (Parent Class): The class whose properties and methods are inherited.
Sub Class (Child Class): The class that inherits the properties and methods of another class.
Syntax:
class SuperClass { // SuperClass fields and methods}class SubClass extends SuperClass { // SubClass fields and methods}Types of Inheritance in Java:
Single Inheritance: A class inherits from one superclass.
Multilevel Inheritance: A class inherits from another class which itself is inherited from another class.
Hierarchical Inheritance: Multiple classes inherit from a single superclass.
Multiple Inheritance (not supported directly): A class cannot directly inherit from more than one class. However, multiple inheritance can be achieved through interfaces.
Example of Inheritance (Single Inheritance)
// SuperClass (Parent Class)class Animal { void sound() { System.out.println("Animals make different sounds"); }}// SubClass (Child Class) inherits from Animal classclass Dog extends Animal { // Overriding the method of the superclass @Override void sound() { System.out.println("Dog barks"); }}public class InheritanceExample { public static void main(String[] args) { Dog dog = new Dog(); dog.sound(); // Calls the overridden method in Dog class }}Output:
Dog barksExplanation:
The
Animalclass is the superclass.The
Dogclass is the subclass that extends theAnimalclass.The
sound()method is overridden in theDogclass to provide a more specific implementation.When
dog.sound()is called, the method in theDogclass is executed.
Example of Multilevel Inheritance
// Grandparent classclass Animal { void eat() { System.out.println("Animals can eat"); }}// Parent class inherits from Animalclass Mammal extends Animal { void giveBirth() { System.out.println("Mammals give birth to live young"); }}// Child class inherits from Mammalclass Dog extends Mammal { void bark() { System.out.println("Dog barks"); }}public class MultilevelInheritance { public static void main(String[] args) { Dog dog = new Dog(); dog.eat(); // Inherited from Animal class dog.giveBirth(); // Inherited from Mammal class dog.bark(); // Defined in Dog class }}Output:
Animals can eatMammals give birth to live youngDog barksExplanation:
Animalis the grandparent class.Mammalis the parent class that inherits fromAnimal.Dogis the child class that inherits fromMammal.The
Dogclass has access to the methods from bothMammalandAnimalthrough multilevel inheritance.
Example of Hierarchical Inheritance
// Superclassclass Animal { void eat() { System.out.println("Animals can eat"); }}// Subclass 1class Dog extends Animal { void bark() { System.out.println("Dog barks"); }}// Subclass 2class Cat extends Animal { void meow() { System.out.println("Cat meows"); }}public class HierarchicalInheritance { public static void main(String[] args) { Dog dog = new Dog(); dog.eat(); // Inherited from Animal class dog.bark(); // Defined in Dog class Cat cat = new Cat(); cat.eat(); // Inherited from Animal class cat.meow(); // Defined in Cat class }}Output:
Animals can eatDog barksAnimals can eatCat meowsExplanation:
Both
DogandCatclasses inherit from the same superclassAnimal. This is hierarchical inheritance.Both subclasses have access to the
eat()method from theAnimalclass, but they also define their own specific behaviors.
Accessing Superclass Members (super Keyword)
In Java, you can use the super keyword to refer to the superclass members (methods and variables) from the subclass.
Example: Accessing a Superclass Method
class Animal { void sound() { System.out.println("Animal makes sound"); }}class Dog extends Animal { void sound() { super.sound(); // Call the superclass method System.out.println("Dog barks"); }}public class SuperKeywordExample { public static void main(String[] args) { Dog dog = new Dog(); dog.sound(); // Calls the Dog class method and also the Animal class method }}Output:
Animal makes soundDog barksExplanation:
The
super.sound()in theDogclass calls thesound()method from theAnimalclass.The subclass method (
Dog'ssound()) can call the superclass method using thesuperkeyword.
Constructor in Inheritance
When a subclass is created, the constructor of the superclass is called automatically before the subclass constructor, using the super() keyword. You can also explicitly call the superclass constructor.
Example: Constructor in Inheritance
class Animal { Animal() { System.out.println("Animal constructor"); }}class Dog extends Animal { Dog() { super(); // Calling the superclass constructor System.out.println("Dog constructor"); }}public class ConstructorInheritanceExample { public static void main(String[] args) { Dog dog = new Dog(); }}Output:
Animal constructorDog constructorExplanation:
The
super()call in theDogconstructor explicitly calls the constructor of theAnimalclass before executing theDogclass's constructor.
Method Overriding in Inheritance
When a subclass defines a method that is already defined in its superclass, it is called method overriding. The subclass's method overrides the method in the superclass.
Example: Method Overriding
class Animal { void sound() { System.out.println("Animal makes sound"); }}class Dog extends Animal { @Override void sound() { System.out.println("Dog barks"); }}public class MethodOverridingExample { public static void main(String[] args) { Animal animal = new Animal(); animal.sound(); // Calls Animal's sound Dog dog = new Dog(); dog.sound(); // Calls Dog's sound }}Output:
Animal makes soundDog barksExplanation:
The
sound()method is overridden in theDogclass. Whendog.sound()is called, the overridden method in theDogclass is executed instead of the one in theAnimalclass.
Conclusion
Inheritance allows a class to inherit the properties and behaviors of another class, enabling code reuse and establishing a relationship between classes.
You can override methods, use the
superkeyword to refer to the superclass, and extend functionality through subclassing.Java supports single, multilevel, and hierarchical inheritance. However, multiple inheritance is not directly supported through classes but can be achieved using interfaces.
Let me know if you'd like more detailed examples or have any questions! ?