Internal classes (Inner Class)

Source: Internet
Author: User

The function of the inner class:

1, hide the internal realization, high cohesion.

2, Java Multi-inheritance implementation.

What is the multiple inheritance of Java?

Java only supports single inheritance, so if multiple inheritance is required, then internal classes can be implemented.

How is it implemented?

1. Parent Class A

public class A {

public int Age () {

return 1;

}

}

2. Parent Class B

public class B {

Public String name () {

Return "Zhang San";

}

}

3, the inner class contains class C

public class C extends otherdefclass{

Private class EA extends a {///Inherits Class A, optionally overriding the parent class method.

public int Age () {

Return super () +1;

}

}

Private class EB extends B {//Inherit class B

Public String name () {

Return super () + "EB";

}

}

      

public int Age () {

return new EA (). age (); Add the extra processing you need

}

Public String name () {

return new EB (). Name ();

}

}

At this point, call the age method of Class C or the name method, and you can call the specific method in the inner class.

Thinking of using:

1, hide the internal implementation.

2, with the template method for use.

Internal classes (Inner Class)

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.