Why do I need internal classes to know java-8.11 from the beginning?

Source: Internet
Author: User
Tags what interface

In this chapter, let's discuss why we need internal classes.

Answer: To facilitate multiple inheritance.

There are two ways to implement multiple inheritance: internal classes and interfaces.

1. Interface

Package Com.ray.ch08;public class Sequence implements A, B {}interface A {}interface b {}


Interface in fact, you do not have to say how many you can, and then only according to the interface can be transformed upward, call the relevant methods.

2. Inner class

When we need to inherit the implementation of a class or abstract class, according to the rules of grammar is not multiple inheritance, then the inner class now 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 {}


As you can see from the code above, no matter what interface the external class implements or what class it inherits from, it has no effect on the inner class.

Note: Internal classes are only considered when multiple inheritance is required, otherwise this complex programming method is not generally used because it affects the readability of the code.

When an inner class is used, the following attributes are obtained:

(1) An inner class can have more than one instance, and the instances are separated from the external classes of communication.

Package Com.ray.ch08;public class Sequence extends a {private int id = 0;class C extends A {public void setId (int id1) {Sy STEM.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 new C, it was not changed at the time of the second new C, because they belong to different instances, each of which has a separate state.

(2) in a single external class, inner classes can use different methods to implement the same interface or inherit the same class.

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 we need internal classes, that is, multiple inheritance, of course, we do not use the general.

This chapter is here, thank you.

-----------------------------------

Directory

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Why do I need internal classes to know java-8.11 from the beginning?

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.