In this chapter we discuss the inner class and the upward transformation.
Like normal classes, an inner class can implement an interface and then move up.
Why?
Because this can better hide the details of the implementation, basic other programmers use inheritance to extend the interface and methods are not able to access the relevant implementation.
Package Com.ray.ch03;public class Test {Private class person implements Canrun {@Overridepublic void run () {System.out.pri Ntln ("Run");}} Public Person Getperson () {return new person ();} public static void Main (string[] args) {Canrun person = new Test (). Getperson ();p erson.run ();} Interface Canrun {void run ();}
The above code passed the interface, completely isolating the implementation and interface dependencies, completely hiding the implementation of the details.
The code above even expands by inheriting test, but since all of the implementation methods of test are private, there is no way to rewrite them.
Package Com.ray.ch03;public class Test {Private class person implements Canrun {@Overridepublic void run () {System.out.pri Ntln ("Run");}} Public Person Getperson () {return new person ();} public static void Main (string[] args) {Canrun person = new Test (). Getperson ();p erson.run ();} Class MyTest extends Test {//does not have any method to override}interface Canrun {void run ();}
Let's look at one more example:
Package Com.ray.ch03;public class Test {Private class ship implements Destination {private String Destination = "abc"; @Ove Rridepublic String Read () {return destination;}} Public ship Destination () {Return to New Ship ();} public static void Main (string[] args) {Destination Destination = new Test (). Destination ();d estination.read ();}} Class MyTest extends Test {//does not have any method to override}interface Destination {String read ();}
Summary: This chapter mainly discusses the internal class and the upward transformation, through the interface to completely hide the implementation.
This chapter is here, thank you.
-----------------------------------
Directory
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Understanding java-8.4 inner class and upward transformation from the beginning