StudyLover
  • Home
  • Study Zone
  • Features
    • Workout Interval Timer
    • Typing Tutor
  • Profiles
  • Contact us
  • Sign in
StudyLover The Inheritance Chain
Download
  1. Java
  2. Programs
The 'this' & Memory Tracker : The Polymorphism Lab
Programs
Initiative: To achieve code reusability through multi-level class hierarchies and super calls.
class Person { // Base class representing a general human entity
    protected String name; // Protected attribute accessible by child classes
    
    public Person(String name) { // Constructor for the Person base class
        this.name = name; // Initialize the name attribute
        System.out.println("[PERSON] Constructor called."); // Log base class initialization
    } // End of Person constructor
} // End of Person class

class Employee extends Person { // Subclass extending Person to represent workers
    protected double salary; // Attribute representing the employee's pay
    
    public Employee(String name, double salary) { // Constructor for the Employee subclass
        super(name); // Pass the name to the Parent (Person) constructor using super
        this.salary = salary; // Initialize the salary attribute locally
        System.out.println("[EMPLOYEE] Constructor called."); // Log middle-tier initialization
    } // End of Employee constructor
    
    public void work() { // Method representing general work duties
        System.out.println(name + " is doing general office work."); // Print generic task
    } // End of work method
} // End of Employee class

class Manager extends Employee { // Leaf class extending Employee for specific roles
    private String dept; // Private attribute representing the manager's department
    
    public Manager(String name, double salary, String dept) { // Constructor for the Manager class
        super(name, salary); // Pass data to the Employee parent constructor using super
        this.dept = dept; // Initialize the department attribute locally
        System.out.println("[MANAGER] Constructor called."); // Log leaf class initialization
    } // End of Manager constructor
    
    @Override // Annotation indicating we are overriding a parent method
    public void work() { // Overridden work method with manager-specific logic
        super.work(); // Execute the original logic from the Employee class first
        System.out.println(name + " is also managing the " + dept + " department."); // Add extra logic
    } // End of overridden work method
} // End of Manager class

public class InheritanceChain { // Main class to demonstrate the inheritance hierarchy
    public static void main(String[] args) { // Entry point for the hierarchy test
        System.out.println("--- Employee Hierarchy Test ---"); // Print system header
        
        Manager m = new Manager("Riya Sharma", 85000, "Human Resources"); // Instantiate the manager
        System.out.println(); // Print a newline for cleaner output formatting
        
        m.work(); // Invoke the overridden work method which chains through the hierarchy
        System.out.println("--- End of Hierarchy Test ---"); // Final log for program end
    } // End of main method
} // End of InheritanceChain class
The 'this' & Memory Tracker The Polymorphism Lab
Our Products & Services
  • Home
Connect with us
  • Contact us
  • info.studylover@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website