The Problem of Multiple Inheritance of Implementing Classes in Java

Implementation of multiple inheritance problems in Java

Classes in Java can only carry out single inheritance, not multiple inheritance.

Cause Analysis

Reason: If a class C inherits both class A and class B, and both A and B contain method d with the same name, when the method d in the parent class is called in class C, the computer does not know what you want to call is the class Method d in A is still method d in class B. (This is the explanation in most Java textbooks)
But the author feels that it can be distinguished by the form of "class name. Attribute name" similar to the properties and methods of calling the parent interface in the interface, such as:
Insert picture description here
so the author is more I tend to think that Java classes do not use the form of multiple inheritance, more because the Java team wants developers to use the super keyword to call the properties and methods of the parent class in the development process, so as to increase the frequency of use of the super keyword. Make the procedure more rigorous.


Java indirect realization of multiple inheritance method

Method 1: Use the interface

An interface can inherit multiple interfaces at the same time

E.g:

interface FightAgainstLandlords{
    
    
    int a = 0;
    void fightAgainstLandlords();
    void test();
}

interface HoeTheEarth{
    
    
    int a=0;
    void hoeTheEarth();
    void test();
}

interface JiCheng extends FightAgainstLandlords,HoeTheEarth{
    
    

    @Override
    void test();
}

class DuoJiCheng2 implements JiCheng{
    
    

    @Override
    public void fightAgainstLandlords() {
    
    
        System.out.println("Let's play the Fight Against Landlords.");
        System.out.println(FightAgainstLandlords.a);
    }

    @Override
    public void hoeTheEarth() {
    
    
        System.out.println("Let's play the hoe the earth.");
    }

    @Override
    public void test() {
    
    
        System.out.println("Let's take a test.");
    }



}

public class Duojicheng {
    
    
    JiCheng jiCheng = new DuoJiCheng2();
    DuoJiCheng2 duoJiCheng = (DuoJiCheng2) jiCheng;

    @Test
    public void Test() {
    
    
        duoJiCheng.fightAgainstLandlords();
    }
}


Note: When an interface inherits two interfaces at the same time, if you want to override the method of the parent interface, and multiple parent interfaces have this method, the parameter list of the method in the parent interface must be consistent. For example, in the test() method above, the parameter list of the method in the subinterface and the parent interface must be consistent (all voids in the above code).

The role of the interface rewriting the parent interface method: to achieve the polymorphism of the object.

Method two: use internal classes

E.g:

interface FightAgainstLandlords{
    
    
    int a = 0;
    void fightAgainstLandlords();
    void test();
}


 class Duojicheng2 {
    
    
    FightAgainstLandlords fightAgainstLandlords = new FightAgainstLandlords() {
    
    
        int b=50;
        @Override
        public void fightAgainstLandlords() {
    
    
            System.out.println("Let's play the fight against landlords.");
        }

        @Override
        public void test() {
    
    
            System.out.println("Let's take a test.");
        }
    };
    FightAgainstLandlords fightAgainstLandlords1 = new FightAgainstLandlords() {
    
    
        int a=100;
        @Override
        public void fightAgainstLandlords() {
    
    
            System.out.println("Let's play the hoe the earth.");
        }

        @Override
        public void test() {
    
    
            System.out.println("There're too much test.");
        }
    };
}
public class Duojicheng extends Duojicheng2{
    
    
    @Test
    public void test() {
    
    
        fightAgainstLandlords.fightAgainstLandlords();
        fightAgainstLandlords1.fightAgainstLandlords();
    }
}

The role of the inner class:

1. Make the code more concise, increase the readability and maintainability of the code, such as directly using the "new class name (){}" form in the class.
2. As shown above: indirect realization of multiple inheritance.

Guess you like

Origin blog.csdn.net/m0_50654102/article/details/114165670