Inheritance in Java Programming ICSE 10th

Inheritance

  • The mechanism by which the attributes or methods of one class (derived or child class) is inherited by another class (base or parent class).
  • It establishes a hierarchical relationship between classes and promotes the reuse of code.
  • Inheritance adds or overrides the functionality as well as some specific behaviors of an existing class into a new class.

Purpose of Inheritance

  • Promoting code reuse by avoiding redundancy.
  • Establishing a hierarchical relationship between classes.
  • Making the code simpler and easier to maintain.
  • Saves time

Implementation

Extends keyword in Java is used.
All the non-private properties and methods of a parent class are inherited by its subclass.

Types of Inheritance in Java

  • Single Inheritance: In single Inheritance, one subclass inherits from one superclass.
  • Multilevel Inheritance: Here, properties of a class which is already a subclass are inherited by another class.
  • Hierarchical Inheritance: Hierarchical inheritance involves multiple classes inheriting from a single parent class.
    Note: To avoid ambiguity (diamond problem), Java does not support multiple inheritance with classes. This is achieved using interfaces.

Advantages of Inheritance

  • Code reuse is encouraged.
  • Code duplication is reduced.
  • Code extension and maintenance is made easier
  • Save the system’s storage
  • Saves time by not repeating the code again and again

Real-Life Example:

  • Let us consider a company with different types of employees:
    Parent Class:
    Attributes: Employee Name, Employee ID
    Methods: Work
    Child Classes: Manager, Engineer, Intern
class Employee
{
   String name;
   int employeeId;
   public Employee(String name, int employeeId)     // Constructor
   {
        this.name = name;
        this.employeeId = employeeId;
   }
   String work()     // Method
  {
        return name + " is working.";
  }
}
class Manager extends Employee     // Constructor
{
    Manager(String name, int employeeId)
    {
        super(name, employeeId);
    }
    @Override // Overridden method
    String work()
    {
        return name + " is managing the team.";
    }
}
 
class Engineer extends Employee     // Constructor
{
    Engineer(String name, int employeeId)
    {
        super(name, employeeId);
    }
    @Override  // Overridden method
    String work()
    {
        return name + " is writing code.";
    }
}
 
class Main // Main class to test the code
{
    void main()
    {
        Employee emp = new Employee("Mohan", 101);
        Manager mgr = new Manager("Sneha", 102);
        Engineer eng = new Engineer("Chitra", 103);
 
        System.out.println(emp.work());
        System.out.println(mgr.work());
        System.out.println(eng.work());
    }
}

 
Output:
Mohan is working.
Sneha is managing the team.
Charlie is writing code.
Here, the Engineer and Manager classes inherit the general structure of class Employee but their specific roles are reflected by overriding the work method.

Operator And Expression in Python

Python Fundamentals: 20 Operator and Expression in Python to Boost Your Coding Skills

Introduction to Operator and Expression in Python Operators and Expressions are basic and essential …

Inheritance in Java Programming ICSE 10th

Inheritance Purpose of Inheritance Implementation Extends keyword in Java is used.All the non-privat…

objects

Elementary Concepts of Objects and Classes in Java Programming

Elementary Concepts of Objects and Classes in Java Programming Java, known for its versatility and w…

Virtual Pet Game in Scratch

Creating a Virtual Pet Game in Scratch Programming

Creating a Virtual Pet Game in Scratch Programming: A Step-by-Step Guide Are you ready to create you…

Calculator using scratch

Crazy Scratch Project on Sprite Dance Party

Scratch Programing Project on Sprite Dance Party Scratch: The Basics Welcome to the second chapter o…

Mastering the concept of Loops in Java

A developer can control the flow of a program by executing a block of code repeatedly based on a con…

Review Your Cart
0
Add Coupon Code
Subtotal

 
Scroll to Top