栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Inheritance in Java

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Inheritance in Java

Introduction

Inheritance is one of the three most important pillars of OOP (Objective Oriented Programming). Although the principle and concept of Inheritance are not hard to understand, in practice, the usage of inheritance may confuse new programmers. Therefore, this short blog aims to clarify some critical points of "Inheritence in Java".

Key terminology
  1. superclass and subclass: superclass, also known as the "parent class", is like a "father" to its subclass. A sub-class can "inherit" some features, fields, and methods from the class it extends.

  2. extends: the keyword indicates a sub-class forming an "inherit" relationship with its superclass. It is extremely important to remember that one class can only "extends" one superclass in Java, meaning a subclass can only have one superclass.

Example: public class PartTimeStaff extends Employee { }

  1. this and super: this and super keywords are core concepts of inheritance. To simplify it, "super" defines a superclass's object or methods, while "this" means fields and methods inside the subclass. Both this and super keywords are mainly used in a subclass.

Example code

Let's introduce the concept of inheritance by setting an easy-to-understand scenario. We assume that there is a chocolate company called "Sweet Joe's", which contains both full-time (receive constant monthly payment) and part-time(receive hourly fee) employees. We will create a program to store employees' information, including name, age, and pay and a calculate() method to calculate how much payment he could get. One way to do it is to create two independent classes of full-time and part-time classes; However, by doing this, we need to declare name and age fields separately in two classes. Both part-time and full-time employees belong to the "employee" concept in "Sweet Joe's" why not simply add an employee class as their superclass! Here is the code:

public class Employee {
    public String name;
    public int age;
​
    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

code1.0 since both full-time and part-time employees' names and ages are the same concept. So employee class should contain these two fields. Please remember that full-time and part-time staff have different payment methods, so creating a common calculate() method in full-time and part-time's common superclass is unreasonable.

public class PartTimeStaff extends Employee {
    public int workHour;
    public int hourlyPay;
​
    public PartTimeStaff(String name, int age,int workHour,int hourlyPay) {
        super(name, age);
        this.workHour = workHour;
        this.hourlyPay = hourlyPay;
    }
    public int calculate(){
        return this.hourlyPay*this.workHour;
    }
}

Part-time staff can inherit fields that all staff have in common (which "super" means employee class in this case). But as part-time staff receive different payments, it should have its unique pay and calculate method ( "this").

public class FullTimeStaff extends Employee{
    int salary;
​
    public FullTimeStaff(String name, int age,int salary) {
        super(name, age);
        this.salary = salary;
    }
    
    public int calculate(){
        return salary/12;
    }
}

Similar to part-time staff class, full-time class also inherit common fields like name and age from employee class. Full-time staff class also needs its unique salary fields and method.

Conclusion

Inheritance alongside encapsulation and polymorphism, are three core properties of OOP in Java. Inheritance allows one class(sub-class) to "inherit" some features like fields and methods from another class(superclass AKA. parent class). It is useful when subclass share some common features with its parentclass (some fields like with "final" keyword can not be inherited). "super" and "this" are two keywords to indicate fields and methods belong to superclass or subclass.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/868443.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号