Why Internal classes are required for java-8.11 from the beginning?

Source: Internet
Author: User

Why Internal classes are required for java-8.11 from the beginning?

In this chapter, we will discuss why Internal classes are needed?

Answer: It facilitates multi-inheritance.

There are two methods to implement multi-inheritance: Internal classes and interfaces.

1. Interface

package com.ray.ch08;public class Sequence implements A, B {}interface A {}interface B {}


In fact, you don't need to mention the number of interfaces you can implement. Then, you can transform up based on the interface and call related methods.

 

2. Internal class

When we need to inherit and implement classes or abstract classes, according to the syntax rules, it is not allowed to inherit multiple classes, so now the internal classes play a role.

package com.ray.ch08;public class Sequence implements A, B {class C implements A, B {}}interface A {}interface B {}

 

package com.ray.ch08;public class Sequence extends A {class C extends A {}}abstract class A {}

 

From the code above, no matter what interfaces the external class implements or what classes it inherits, there is no impact on the internal class.

 

Note: Internal classes are considered only when multi-inheritance is required. Otherwise, this complex programming method is generally not used because it affects the readability of the Code.

 

When an internal class is used, the following features are obtained:

(1) Internal classes can have multiple instances, and the communication between instances and external classes is separated.

package com.ray.ch08;public class Sequence extends A {private int id = 0;class C extends A {public void setId(int id1) {System.out.println(id);id = id1;}}public int getId() {return id;}public void setId(int id) {this.id = id;}public static void main(String[] args) {Sequence sequence1 = new Sequence();C c1 = sequence1.new C();c1.setId(2);System.out.println(sequence1.getId());Sequence sequence2 = new Sequence();C c2 = sequence2.new C();c2.setId(3);System.out.println(sequence2.getId());}}abstract class A {}


Output:

0
2
0
3

From the output, we can see that although we changed the id at the first time new C, it is not changed at the second time new C, because they belong to different instances, they are independent.

 

(2) In a single external class, internal classes can implement the same interface or inherit the same class in different ways.

package com.ray.ch08;public class Sequence {class B implements A {@Overridepublic void run() {System.out.println(run1);}}class C implements A {@Overridepublic void run() {System.out.println(run2);}}public static void main(String[] args) {}}interface A {void run();}

 

Summary: This chapter mainly explains why Internal classes are required, that is, multi-inheritance. Of course, we generally cannot use them.

 

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.