HR vs Employee: Java Inheritance with a Twist! ???

HR vs Employee: Java Inheritance with a Twist! ???

Dive into Java inheritance through a fun HR vs Employee showdown! ?? HR sets the rules, but employees have their own spin. Learn how classes and subclasses work together in this playful example!

1. Single Inheritance: The Basic Inheritance

In single inheritance, one class inherits from another class. It’s the simplest form of inheritance, where a subclass extends the functionality of a single parent class.

class HR {
    void salaryPolicy() {
        System.out.println("HR: Salary is paid on the 1st of every month. ??");
    }
}

class Employee extends HR {
    void calculateSalary() {
        System.out.println("Employee: My salary is calculated based on my performance and work hours. ????");
    }
}

public class SingleInheritanceExample {
    public static void main(String[] args) {
        HR hr = new HR();
        hr.salaryPolicy();  // HR announcing salary policy

        Employee emp = new Employee();
        emp.salaryPolicy(); // Employee inherits HR's salary policy
        emp.calculateSalary(); // Employee calculates their own salary
    }
}        

Output:

HR: Salary is paid on the 1st of every month. ??
HR: Salary is paid on the 1st of every month. ??
Employee: My salary is calculated based on my performance and work hours. ????        

2. Multilevel Inheritance: Building on Top

Multilevel inheritance happens when a class inherits from another class, which in turn inherits from another, forming a chain of inheritance.

class HR {
    void promoteWorkLifeBalance() {
        System.out.println("HR: Let's keep a healthy work-life balance! ??");
    }
}

class Manager extends HR {
    void manageTeam() {
        System.out.println("Manager: I manage a team and encourage breaks. ????");
    }
}

class Employee extends Manager {
    void work() {
        System.out.println("Employee: I follow the guidelines and enjoy my breaks. ?");
    }
}

public class MultilevelInheritanceExample {
    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.promoteWorkLifeBalance(); // HR's policy
        emp.manageTeam(); // Manager's responsibility
        emp.work(); // Employee's task
    }
}        

Output:

HR: Let's keep a healthy work-life balance! ??
Manager: I manage a team and encourage breaks. ????
Employee: I follow the guidelines and enjoy my breaks. ?        

3. Hierarchical Inheritance: Sharing the Same Root

Hierarchical inheritance occurs when multiple subclasses inherit from a single parent class, sharing the same base functionality.

class HR {
    void promoteWellness() {
        System.out.println("HR: Don't forget to stretch and stay healthy! ??♀?");
    }
}

class Employee extends HR {
    void work() {
        System.out.println("Employee: I follow wellness practices while working. ??");
    }
}

class Intern extends HR {
    void learn() {
        System.out.println("Intern: I’m here to learn and keep my health in check! ??");
    }
}

public class HierarchicalInheritanceExample {
    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.promoteWellness(); // Shared HR wellness policy
        emp.work(); // Employee's own work routine

        Intern intern = new Intern();
        intern.promoteWellness(); // Shared HR wellness policy
        intern.learn(); // Intern’s learning task
    }
}        

Output:

HR: Don't forget to stretch and stay healthy! ??♀?
Employee: I follow wellness practices while working. ??
HR: Don't forget to stretch and stay healthy! ??♀?
Intern: I’m here to learn and keep my health in check! ??        

4. Multiple Inheritance (Through Interfaces): Combining Behaviors

Java doesn't support multiple inheritance directly with classes, but we can achieve it through interfaces. An interface can be implemented by multiple classes, allowing them to inherit multiple behaviors.

interface Workable {
    void work();
}

interface Breakable {
    void takeBreak();
}

class Employee implements Workable, Breakable {
    public void work() {
        System.out.println("Employee: I work on tasks efficiently. ??");
    }

    public void takeBreak() {
        System.out.println("Employee: Time for a quick coffee break! ?");
    }
}

public class MultipleInheritanceExample {
    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.work(); // Employee works
        emp.takeBreak(); // Employee takes a break
    }
}        

Output:

Employee: I work on tasks efficiently. ??
Employee: Time for a quick coffee break! ?        

5. Hybrid Inheritance (Combination of Types)

Hybrid inheritance is a combination of multiple types of inheritance, which can be achieved by combining classes and interfaces. This allows the subclass to inherit both behavior and functionality from multiple sources.

interface Workable {
    void work();
}

class HR {
    void promoteWorkLifeBalance() {
        System.out.println("HR: Promote work-life balance! ??");
    }
}

class Employee extends HR implements Workable {
    public void work() {
        System.out.println("Employee: Work hard, but never skip a coffee break! ???");
    }
}

public class HybridInheritanceExample {
    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.promoteWorkLifeBalance(); // HR's policy
        emp.work(); // Employee’s task
    }
}        

Output:

HR: Promote work-life balance! ??
Employee: Work hard, but never skip a coffee break! ???        

Conclusion:

Java inheritance simplifies code structure, from single to multiple inheritance via interfaces. It’s key for building clean, efficient, and reusable code. ????

Keep coding! ???

Good Day!

#HR #LinkedIn #developer #code

要查看或添加评论,请登录

Satish Jassal的更多文章

社区洞察

其他会员也浏览了