Java---inheritance (bottom)---final + abstract class + inner class

Source: Internet
Author: User

Final

Keyword as a modifier

1. You can modify classes, functions, variables

2. A class that is final modified cannot be inherited

In order to avoid being inherited, quilt-like replication function.

3. Final modification of the method, can not be replicated

4. The final modified variable is a constant that can be assigned only once, either to modify the member variable or to modify the local variable. As constants: The writing specification for constants: All uppercase letters, if there are multiple words, connect with "_". Equivalent to const in C + +

5. When the inner class is defined at a local location in the class, only the local, final decorated local variables can be accessed




Final Class Aa{void Show () {System.out.println ("a.show ()");}} Class BB extends aa{////} class BB {final public int x = 10;final void Show () {System.out.println ("b.show ()");} void display () {//x = 20;//x = 10; System.out.println ("x==:" +x);}} Class Cc extends Bb{//public void Show () {}}public class Final {public static void main (string[] args) {AA a = new Aa (); B b b = new Bb (); CC C = new CC (); a.show (); B.show (); C.show (); C.display ();}}




Abstract class

Features of abstract classes:

1. Abstract methods must be in the abstract class

2. Abstract methods and abstract classes must be decorated with the abstract keyword

3. Abstract classes can not create objects with new. Because calling abstract methods doesn't make sense.

4. The method in the abstract class is to be used, the subclass object must be called after all its abstract methods have been replicated by the child class, and the subclass is an abstract class if the subclass covers only a subset of the abstract methods.

Abstractions can only modify classes and methods.

Abstract classes can also have non-abstract methods.

Abstract methods can also not be defined in an abstract class, which simply does not allow the class to create objects.

Abstract classes and general classes are not much different.

How do I use it?

Through the analysis of the actual problem, the same part of each class is extracted, and the same part is defined as abstract class.

For example:

Students and workers so two objects, their common behavior if said to be study, then will study this function extracts, defines a named person's abstract class.

The person class only provides methods, and the specific implementation is by subclasses.



Abstract class Person {abstract void study ();p ublic Void Show () {System.out.println ("person.show ()");} abstract void Dislay ();} Class Student extends person{public void study () {System.out.println ("student.study ()");} public void Show () {System.out.println ("student.show ()");}} Class Worker extends person{public void study () {System.out.println ("worker.study ()");} public void Show () {System.out.println ("worker.show ()");}} public class Abstract {public static void main (string[] args) {Student s = new Student (); Worker W = new Worker (); S.study (); W.study (); S.show (); W.show ();}}





Inner class

Define a class within another class, which is called an inner class (built-in class, nested Class)

Access rules for internal classes:

1. Inner classes can access external class members directly, including private.

A member in an external class can be accessed directly because the inner class holds a reference to an external class (the external class name. This)

2. External classes to access the inner class must establish an inner class object.

Simple inner-class creation method:




Class Out{int x = 10;class in {int y = 20;void Show () {System.out.println ("Out.In.show () + + +" +y);}} void display () {System.out.println ("out.display ()---" +x);}}



So how do you access your internal classes?




Import java.awt.displaymode;class Out{int x = 10;class in {void Show () {System.out.println ("Out.In.show () + + +" +x);// Inner classes can access members of external classes directly. Display ();}} void display () {System.out.println ("out.display ()---" +x); void set () {in ' = new in (); In.show ();}} Class Hello{public static void Main (string[] args) {out-out = new Out (); Out.display (); Out.set ();}}



Direct access to members in an inner class:



Import Java.awt.displaymode;class Out{int x = ten; class in {void Show () {System.out.println ("Out.In.show () + + +" +x);// Inner classes can access members of external classes directly. Display ();}} void display () {System.out.println ("out.display ()---" +x); void set () {in ' = new in (); In.show ();}} Class Hello{public static void Main (string[] args) {//out out = new Out ();//out.display ();//out.set ();/* Directly accesses members of the inner class. */new out (). New in (). Show (); Out.in in = new Out (). New in (); In.show ();}}



Access format:

When an inner class is defined on the member location of an external class and is not private, an inner class object can be created directly in the outer other class

Format:

The outer class name. Internal class Name Variable name = External Class object. Inner Class object

Out.in in = new out (). New In ();




Class Out{int x = Ten; class in {int x = 4;void Show () {int x = 6; System.out.println ("Out.In.show () + + +" +x)//6system.out.println ("Out.In.show () + + +" +this.x);// 4system.out.println ("Out.In.show () + + +" +out.this.x);//3display ();}}


A member in an external class can be accessed directly because the inner class holds a reference to an external class (the external class name. This)



When an inner class is on a member position, it can be decorated with modifiers.

For example: Private: Encapsulating inner classes in the outer class

Static: The inner class is statically characterized.

When an inner class is statically decorated, only static members in the outer class can be accessed directly. Access restrictions have occurred.

How do you directly access non-static members of static inner classes in other external classes?



Import java.awt.displaymode;class out{static int x = ten; static class in {void Show () {System.out.println ("Out.In.show () + + + "+x");//The inner class can access members of the external class directly. }}void display () {System.out.println ("out.display ()---" +x);} void set () {in ' = new in (); In.show ();}} Class Hello{public static void Main (string[] args) {new out.in (). Show ();}}



New out.in (). function;

When out is loaded and the inner class is loaded, because it is a static class, New Out.in () is equivalent to creating an inner class object.

How do you directly access static members of static inner classes in other external classes?

Out.In.function;

Attention:

When a static member is defined in an inner class, the inner class must be static.



Class in {static void show () {System.out.println ("Out.In.show () + + +" +x);//The inner class can access members of the external class directly. }}


Compiler hints:

The method show cannot be declared static; Static methods can only is declared in a static or top

Level type

When a static method in an external class accesses an inner class, the inner class must also be static.




Java---inheritance (bottom)---final + abstract class + inner class

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.