Java improves the--java implementation of multiple inheritance

Source: Internet
Author: User

Read Catalogue


    • First, the interface
      Second, the internal class

  multiple inheritance refers to the fact that a class can inherit behavior and characteristics from more than one parent class at the same time , but we know that Java only allows single inheritance to ensure data security . Sometimes we think that if multiple inheritance is often a bad design in the system, we often need to think about not how to use multiple inheritance, It's about whether or not your design has problems. But sometimes we do need multiple inheritance, and in real life there are real situations, such as heredity: we inherit the behavior and characteristics of the father and inherit the behavior and characteristics of the mother. Fortunately Java is very kind and understanding of us, and it provides two ways for us to twist and implement multiple inheritance: interfaces and inner classes .

Back to top one, interface

In the introduction of interfaces and abstract classes to understand that subclasses can only inherit a parent class, that is, only a single inheritance, but can implement multiple interfaces, which for our implementation of multiple inheritance to do the foreshadowing.

For interfaces, sometimes it does not just represent a more purely abstract class, the interface is not implemented in any way, that is, there is no interface-related storage, so it is not possible to block the combination of multiple interfaces.

1 interface Canfight {2     void Fight (); 3} 4  5 interface Canswim {6     void swim (); 7} 8  9 interface Can Fly {one     void fly ();}13-public class Actioncharacter {     fight-         }19-     }18 Class Hero extends Actioncharacter implements CANFIGHT,CANFLY,CANSWIM{21 public     void Fly () {     }24     public void Swim () {     }27     /**29      * for the Fight () method, inherits the parent class, so there is no need to display the declaration of the      */31}

Back to top second, inner class

It is a more feasible and common way to implement multiple inheritance using interfaces above, and when it comes to internal classes, it is more perfect to implement multiple inheritance implementations of internal classes, and also to make it clear that if the parent class is an abstract class or a concrete class, then I can only implement multiple inheritance through the inner class. How to implement multiple inheritance using an inner class, see the following example: How the son uses multiple inheritance to inherit the good genes of his father and mother.

The first is father father and mother mother:

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

The play is here, son:

public class Son {        /**     * Inner class inherits 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's strong:" + Son.getstrong ());        System.out.println ("Son's kind:" + son.getkind ());}    } ----------------------------------------Output:son's Strong:10son kind:6.

The son inherited the father, became stronger than the father, but also inherited the mother, but the gentle index fell. This defines two inner classes, each inheriting the Father Father class, the mother class mother class, and all can be very natural to get the behavior of their parent class, which is an important feature of the inner class: The inner class can inherit a class independent of the outer class, guaranteeing the independence of the inner class, it is based on this point, Multiple inheritance will be possible.

Reference article Link: Java Improvement Chapter (IX)-Implementing multiple inheritance

From:http://www.cnblogs.com/qian123/p/5702798.html

Java improves the--java implementation of multiple inheritance

Related Article

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.