Home Java javaTutorial Java Improvement (8) ----- Implementing Multiple Inheritance

Java Improvement (8) ----- Implementing Multiple Inheritance

Feb 09, 2017 pm 01:46 PM

Multiple inheritance means that a class can inherit behaviors and characteristics from more than one parent class at the same time. However, we know that Java only allows single inheritance in order to ensure data security. Sometimes we think that if multiple inheritance needs to be used in the system, it is often a bad design. At this time, what we often need to think about is not how to use multiple inheritance, but whether there are problems with your design. But sometimes we do need to implement multiple inheritance. Inheritance, and this situation really exists in real life, such as inheritance: we inherit both the behavior and characteristics of our father and the behavior and characteristics of our mother. Fortunately, Java is very kind and understanding of us. It provides two ways for us to implement multiple inheritance: interfaces and inner classes.

1. Interface

When introducing interfaces and abstract classes, we learned that subclasses can only inherit one parent class, which means that there can only be a single Inheritance, but can implement multiple interfaces, which paves the way for us to implement multiple inheritance.

For interfaces, sometimes it represents more than just a more pure abstract class. The interface does not have any specific implementation, that is to say, there is no storage related to the interface, so it cannot Combination of multiple interfaces is blocked.

interface CanFight {  
    void fight();  
}  
  
interface CanSwim {  
    void swim();  
}  
  
  
interface CanFly {  
    void fly();  
}  
  
public class ActionCharacter {  
    public void fight(){  
          
    }  
}  
  
public class Hero extends ActionCharacter implements CanFight,CanFly,CanSwim{  
  
    public void fly() {  
    }  
  
    public void swim() {  
    }  
  
    /** 
     * 对于fight()方法,继承父类的,所以不需要显示声明 
     */  
}
Copy after login

2. Internal classes

The above use of interfaces to implement multiple inheritance is a more feasible and common way. When introducing internal classes, we talked about the changes in the implementation of multiple inheritance made by internal classes. It is more perfect, and it also makes it clear that if the parent class is an abstract class or a concrete class, then I can only implement multiple inheritance through inner classes. How to use internal classes to achieve multiple inheritance, please see the following example: how a son uses multiple inheritance to inherit the excellent genes of his father and mother.

First, Father and Mother:

public class Father {  
    public int strong(){  
        return 9;  
    }  
}  
  
public class Mother {  
    public int kind(){  
        return 8;  
    }  
}
Copy after login

The highlight is here, Son:

public class Son {  
      
    /** 
     * 内部类继承Father类 
     */  
    class Father_1 extends Father{  
        public int strong(){  
            return super.strong() + 1;  
        }  
    }  
      
    class Mother_1 extends  Mother{  
        public int kind(){  
            return super.kind() - 2;  
        }  
    }  
      
    public int getStrong(){  
        return new Father_1().strong();  
    }  
      
    public int getKind(){  
        return new Mother_1().kind();  
    }  
}
Copy after login

Test procedure:

public class Test1 {  
  
    public static void main(String[] args) {  
        Son son = new Son();  
        System.out.println("Son 的Strong:" + son.getStrong());  
        System.out.println("Son 的kind:" + son.getKind());  
    }  
  
}  
----------------------------------------  
Output:  
Son 的Strong:10  
Son 的kind:6
Copy after login

The son inherited his father and became stronger than his father. He also inherited his mother, but his gentleness index dropped. Two inner classes are defined here. They inherit the Father class and the Mother class respectively, and both can obtain the behavior of their respective parent classes very naturally. This is an important feature of the inner class: the inner class can inherit one from the outer class. Irrelevant classes ensure the independence of internal classes. It is based on this that multiple inheritance becomes possible.


The above is the content of java improvement (eight)-----implementing multiple inheritance. For more related content, please pay attention to the PHP Chinese website (www.php.cn) !


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management? How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management? Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

See all articles