WHAT IS INHERITANCE? Inheritance is a core concept in object-oriented programming (OOP). It allows one class (subclass or child class) to inherit properties and behaviors from another class (superclass or parent class). This concept encourages code reusability and helps create a structured and organized codebase.
W HY DO WE USE INHERITANCE IN JAVA? Code Reusability Extensibility Polymorphism Maintainability Simplifying Complex Systems
SYNTAX OF JAVA INHERITANCE The extends keyword is used to establish inheritance in Java. Subclass is the name of the new class that is inheriting from Superclass . The Subclass can access and inherit all the members (fields and methods) of the Superclass . class Subclass extends Superclass { // Subclass-specific members and methods }
EXAMPLE OF INHERITANCE: As shown in the figure, programmer is the subclass and the employee is the super class. Relation between two classes is the Programmer IS-A Employee. It means that the Programmer is a type of Employee.
EXAMPLE OF INHERITANCE: class Employee { float salary=40000; } class Programmer extends Employee { int bonus=10000; public static void main (String args[]) { Programmer p=new Programmer(); System.out.println ( "Programmer salary is:" +p.salary); System.out.println( "Bonus of Programmer is:" +p.bonus); } }
TYPES OF INHERITANCE: Single Inheritance. Multilevel Inheritance. Multiple Inheritance. Hierarchical Inheritance. Hybrid Inheritance.
MAJOR TYPES OF INHERITANCE:
EXAMPLE OF SINGLE INHERITANCE // Superclass class Animal { void eat () { System.out.println( "The animal eats." ); } } // Subclass inheriting from Animal class Dog extends Animal { void bark () { System.out.println( "The dog barks." ); } }
EXAMPLE OF SINGLE INHERITANCE public class SingleInheritanceExample { public static void main (String[] args) { // Create an instance of Dog Dog myDog = new Dog (); // Call methods from both Animal and Dog classes myDog.eat(); // Inherited from Animal myDog.bark(); // Specific to Dog } }
EXAMPLE OF MULTILEVEL INHERITANCE class Grandparent { void grandparentMethod () { System.out.println( "This is a method in the Grandparent class." ); } } class Parent extends Grandparent { void parentMethod () { System.out.println( "This is a method in the Parent class." ); } }
EXAMPLE OF MULTILEVEL INHERITANCE class Child extends Parent { void childMethod () { System.out.println( "This is a method in the Child class." ); } } public class MultilevelInheritanceExample { public static void main (String[] args) { Child child = new Child (); // Calling methods from each class child.grandparentMethod(); // Inherited from Grandparent child.parentMethod(); // Inherited from Parent child.childMethod (); // Method in Child class } }
EXAMPLE OF HIERARCHAL INHERITANCE 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." ); } }
EXAMPLE OF HIERARCHAL INHERITANCE public class HierarchicalInheritanceExample { public static void main (String[] args) { Dog dog = new Dog (); Cat cat = new Cat (); System.out.println( "Dog:" ); Dog.eat(); // Inherited from Animal dog.bark(); System.out.println( "\nCat:" ); cat.eat(); // Inherited from Animal cat.meow(); } }
WHY MULTIPLE INHERITANCE IS NOT SUPPORTED IN JAVA? Multiple inheritance is not supported in Java primarily to avoid the "diamond problem," which is a situation where ambiguity arises when a class inherits from multiple classes that have a common superclass. This problem can make code unpredictable and hard to maintain. Java uses interfaces to achieve some benefits of multiple inheritance while avoiding the complexities and ambiguities associated with inheriting from multiple classes directly.