Java Beginner 4

Source: Internet
Author: User

1. Interface interface

(1) [modifier] Interface interface Name extends parent interface 1, parent interface 2.... No constructor method, cannot be instantiated;       • Interfaces can only inherit interfaces, cannot inherit classes and interfaces there is no ordinary method, the method is all abstract; • The method default modifier in the interface isPublic Abstract• The fields in the interface are all global constants, and the default modifier isPublic static Final; (2) The members in the interface include (mainly the first two): • Global constants • Common abstract methods • Internal classes (including internal classes, internal interfaces, internal enumeration classes);using Interfaces format: Public class Subimpl extends Super implements Ia,ib• The implementation of the interface must be after extends;• The method that implements the interface must be public type • Interfaces cannot create instances, but can declare variables of reference types.       • At this point, a variable of the reference type must point to its implementation class object. istudent s = new String ();//istudent s = new Studentimpl ();//2. Interface and abstract class similarities and differences(1) The same point: • are located at the top of the inheritance and are used for other implementations or inheritance;       • Neither can be instantiated;       • Both contain abstract methods whose subclasses must overwrite these abstract methods; (2) Differences: • Abstract classes provide implementations for partial methods, avoiding the repetition of subclasses to implement these methods, and providing code reuse; interfaces can contain only abstract methods; • A class can inherit only one direct parent class (possibly an abstract class), but may implement multiple interfaces; (interface compensates for Java's single Inheritance) (3)the choice of the two: • Preferred interface, as little as possible abstract class; • You need to define the behavior of subclasses, but also to provide generic functionality for subclasses to select abstract classes;3. Inner class(1) There are classes and inner classes in the class: nested classes • External classes: The Host Class (2) hides the inner class from the outer class, does not allow access to the class with other classes of the package, and (3) the inner class can access the private data of the outer class, the external class cannot access the implementation details of the inner class, such as fields, and (4) observe the compiled bytecode file )non-static inner class;static inner class,local inner class; (6)Anonymous Inner classSuitable for classes that use only one use at a time;A, non-static inner classØ If the external class field, inner class field, inner class method variable has the same name, the access method is: • Access the field of the outer class: the External class class name. This field • Accesses the Inner class field: this. field • Access local variables for inner class methods: Fieldexamples of non-static internal classes:Printing results:Add a local variable to the tail adds an inner class attribute to the tail adds the outer class attribute to the tailaccessing non-static inner classes outside the outer class(1) The inner class cannot be private, otherwise it cannot be accessed (2) Define internal class variables outside the outer class Outerclass.innerclass varName; (3) Non-static inner class objects are stored in objects of the outer class, Therefore, before creating a non-static inner class object, you must first create an object of its outer class outerinstance.new innerclass ([parameter list]);
 Public classInnerdemo { Public Static voidMain (string[] args) {//Outer.Inner in = new Outer (). New Inner ();Outer.Inner in; Outer out=NewOuter (); Inch= out.NewInner (); //in.inshow (); //Outer out = new Outer (); //out.outshow ();    }}classOuter {classInner { Public voidinshow () {System.out.println ("Inshow"); }    }     Public voidoutshow () {//System.out.println ("Outshow");Inner in =NewInner ();    In.inshow (); }}
accessing non-static inner class instances outside of the external class B, static inner classC, local inner classD, anonymous inner class

Java Beginner 4

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.