This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging

Today we’re going to start learning about Java inheritance.

Main contents of this article:

  1. Know what inheritance does in Java
  2. Advantages and disadvantages of inheritance
  3. How do you implement inheritance in your code

Java Inheritance Definition

Inheritance is one of the three characteristics of object-oriented, and encapsulation takes the first place. After encapsulation, independent body is formed. There may be inheritance relationship between independent body A and independent body B. In fact, the inspiration of inheritance in the program comes from real life. Inheritance can be seen everywhere in real life. For example, the son inherits his father’s property, and the son is rich without any effort.

Inheritance in life:

Figure 1: Inheritance in the real world

In inheritance, a child inherits the characteristics and behavior of its parent class, so that the object (instance) of the child class has the properties of the parent class, or a child class inherits methods from the parent class, so that the child class has the same behavior as the parent class. Rabbits and sheep belong to the class of herbivores, while lions and leopards belong to the class of carnivores. Herbivores and carnivores are animals. So inheritance needs to conform to the relationship: is-a (Bird is-a Animal), the parent class is more general, the subclass is more specific. Although herbivores and carnivores are both animals, there are differences in their properties and behaviors, so subclasses may have the general characteristics of their parent class as well as their own characteristics.

Why use inheritance? There may also be common features and actions in different classes, and these common features and actions can be placed in one class and shared by other classes. So you can define a generic class and then extend it to other specific classes that inherit the characteristics and actions from the generic class. Inheritance is an important way to realize software reuse in Java. It avoids duplication and is easy to maintain.

How Java inherits

The syntax format of inheritance in Java:

Extends extends parent class name {class body; }Copy the code

To illustrate why inheritance is needed, let’s use the following requirement:

Public class Account {private String actno; // Private double balance; // Set and get methods for account and balance public String getActno() {return actno; } public void setActno(String actno) { this.actno = actno; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; }}Copy the code
Public class CreditAccount {private String actno; public class CreditAccount {private String actno; // Private double balance; // Set and get methods for account and balance public String getActno() {return actno; } public void setActno(String actno) { this.actno = actno; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } private double credit; Public double getCredit() {return credit; } public void setCredit(double credit) { this.credit = credit; }}Copy the code

The above two classes describe “bank account class” and “credit account class” respectively. The credit account class has its own characteristics in addition to the features of the bank account class. According to the way the above code is written, the program will be very bloated.

Public class Account {private String actno; // Private double balance; // Set and get methods for account and balance public String getActno() {return actno; } public void setActno(String actno) { this.actno = actno; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; }}Copy the code
Public class CreditAccount extends Account{public class CreditAccount extends Account{public class CreditAccount extends Account{public class CreditAccount extends Account{public class CreditAccount extends Account{public class CreditAccount extends Account{private double credit; Public double getCredit() {return credit; } public void setCredit(double credit) { this.credit = credit; }}Copy the code
public class AccountTest { public static void main(String[] args) { CreditAccount act = new CreditAccount(); act.setActno("111111111"); Act. SetBalance (9000.0); System.out.println(act.getactno () + "credit account, balance" + act.getbalance () + "yuan "); }}Copy the code

The running result is as shown in the figure below:

Figure 2: Inheritance tests

From the above code, we can see that inheritance can solve the problem of code bloat. In other words, inheritance solves the problem of code reuse, but this is not the most important function of inheritance. The most important function of inheritance is that it enables method coverage and polymorphism (which will be explained in more detail later in the course).

What are the features of inheritance? Here are some things to remember:

● If class B inherits from class A, class A is called superclass, parent class, base class, and class B is called subclass, derived class, and extended class.

Java extends A class B extends A C{} class B extends A C{} class B extends A C{} class B extends A C{} class B extends A C{} class B extends A C{} class B extends A C{}

For example, class C extends B; class B extends A; class C extends B; class B extends A; class C extends B; class B extends A

In Java, a subclass inherits from its parent class, except for constructors and private data.

If a class in Java does not inherit from any class, it inherits from Object by default. Object is the root class (ancestor class) provided by the Java language. In other words, an Object is born with all the characteristics of the Object type.

There are also some disadvantages to inheritance. For example, the coupling between the CreditAccount class and the Account class is very high, and any change to the Account class will immediately affect the CreditAccount class.

Let’s test the method inherited from the Object class. Let’s look at some of the source code for the Object class:

Figure 3: Source code for the toString() method in the Object class

Let’s try calling this method. Look at the code and the result:

public class ExtendsTest{ public static void main(String[] args) { ExtendsTest et = new ExtendsTest(); String s = et.toString(); System.out.println(s); }}Copy the code

The running result is as shown in the figure below:

Figure 4: toString() method execution result

The output is unreadable, but at least there is an “@” sign in the string. The toString() method is inherited from ExtendsTest.