Java improvements-implement multi-inheritance in Java and inherit from java

Source: Internet
Author: User

Java improvements-implement multi-inheritance in Java and inherit from java

  Multi-inheritance means that a class can inherit behavior and features from more than one parent class at the same time.However, we know thatJava only allows single inheritance to ensure data security. Sometimes we think that multi-inheritance in the system is often a bad design. What we need to think about in this case is not how to use multi-inheritance, but whether your design has problems. but sometimes we do need to implement multi-inheritance, and in real life, there is such a situation, such as inheritance: we inherit the behaviors and characteristics of our father and the behavior and characteristics of our mother. Fortunately, Java is very kind and understands us. It provides two ways for us to achieve multi-inheritance:InterfaceAndInternal class.

 

I. Interface

When introducing interfaces and abstract classes, we learned that child classes can only inherit one parent class. That is to say, there can only be one inheritance, but multiple interfaces can be implemented, this paves the way for us to implement multi-inheritance.

For an interface, sometimes it is not just a pure abstract class, the interface has no specific implementation, that is, there is no storage related to the interface, therefore, the combination of multiple interfaces cannot be blocked.

 

1 interface CanFight {2 void fight (); 3} 4 5 interface CanSwim {6 void swim (); 7} 8 9 10 interface CanFly {11 void fly (); 12} 13 14 public class ActionCharacter {15 public void fight () {16 17} 18} 19 20 public class Hero extends ActionCharacter implements CanFight, CanFly, canSwim {21 22 public void fly () {23} 24 25 public void swim () {26} 27 28/** 29 * for the fight () method, inherit from the parent class, so no need to display Declaration 30 */31}

 

Ii. Internal class

The above method is feasible and universal to implement multi-inheritance Using Interfaces. When introducing the internal class, we will talk about the internal class to make the implementation of multi-inheritance more perfect, at the same time, it is also clear that if the parent class is an abstract class or a specific class, then I can only implement multiple inheritance through the internal class. To learn how to use internal classes to implement multiple inheritance, see the following example: how does a son use 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;    }}

 

Here, Son:

Public class Son {/*** internal class inherits the Father class */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 ();}}

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's kind:" + son. getKind () ;}-------------------------------------------- Output: Son's Strong: 10Son's kind: 6

The son inherited his father, became stronger than his father, and inherited his mother, but the gentle index declined. Two internal classes are defined here. They inherit the Father class and Mother class respectively, and can naturally obtain the behavior of their parent classes. This is an important feature of the internal class: an internal class can inherit a class unrelated to an external class, ensuring the independence of the internal class. Based on this, multi-inheritance will become possible.

Reference link: java (9)-implement multi-Inheritance

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.