Inheritance by Inheritance

With Java encapsulation and abstraction out of the way, let’s move on to inheritance. If you’re familiar with object-oriented programming languages like Java and C++, you’re probably familiar with this feature. Inheritance is used to indicate an IS-A relationship between classes, such as a cat being a mammal.

In terms of inheritance relationship, inheritance can be divided into two modes, single inheritance and multiple inheritance. Single inheritance means that a subclass inherits only one parent, while multiple inheritance means that a subclass can inherit multiple parent classes, such as cats that are both mammals and reptiles.

To implement this feature, programming languages need to provide special syntactic mechanisms to support it, such as Java’s use of the extends keyword for inheritance and C++’s use of colons (class B: Public A), Paraentheses () for Python, < for Ruby.

However, some programming languages support single inheritance but not multiple inheritance, such as Java, PHP, C#, Ruby, etc., while some programming languages support both single inheritance and multiple inheritance, such as C++, Python, Perl, etc.

public class Animal { 
    private String name;   
    private int id; 
    public Animal(String myName, String myid) { 
        // Initializes the property value
    } 
    public void eat(a) {  // An implementation of the eating method}
    public void sleep(a) { // An implementation of the sleeping method}
} 
// Penguins inherit from their animal parent
public class Penguin  extends  Animal{
   public Penguin(String myName, int myid) { 
        super(myName, myid); }}// The mouse descends from the animal parent
public class Mouse extends Animal { 
    public Mouse(String myName, int myid) { 
        super(myName, myid); }}Copy the code

Meaning of inheritance

One of the biggest benefits of inheritance is code reuse. If two classes have the same properties and methods, we can extract those same parts into the parent class, and let the two subclasses inherit from the parent class. In this way, both subclasses can reuse the code in the parent class, avoiding the code being written more than once. However, this is not unique to inheritance, and there are other ways to address this code reuse problem, such as using composite relationships rather than inheritance.

If we take this to the next level of thinking and think about inheriting this property, we can think of it this way: we have a cat class in our code, we have a mammal class. Cats are mammals, and from a human cognitive point of view, they have an IS-A relationship. We connect the two classes through inheritance, reflecting this relationship in the real world, which is very consistent with human cognition, and also, from a design point of view, has a structural aesthetic.

The concept of inheritance is well understood and easy to use. However, excessive use of inheritance, which is too deep and complex, can lead to poor readability and maintainability of code. To understand the functionality of a class, we need to not only look at the code of the class, but also look up the inheritance relationship layer by layer “parent, parent’s parent…” The code. Also, subclasses and superclasses are highly coupled, and modifying the code in the parent class directly affects the subclass.

Therefore, inheriting this feature is also a very controversial feature. Many people think of inheritance as an anti-pattern. We should use it as little as possible, or not at all.

More original reading: javawu.com