Understanding java-8.4 inner class and upward transformation from the beginning

Source: Internet
Author: User

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

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.