Three major features of Java: Encapsulation inheritance polymorphism

Source: Internet
Author: User
Tags abstract definition constant definition modifier

Inner class: member inner class, Static inner class, method inner class, anonymous inner class.

Inner class: A class defined in another class, corresponding to an outer class that contains an inner class, is called an outer class.

The function of the Inner class: (1) The inner class provides a better encapsulation, the inner class can be hidden within the outer class, and the other classes in the same package are allowed to be questioned by the other class. (2) The method of the inner class can directly access all the data of the external class, including private data. (3) functions implemented by inner classes can be implemented using external classes, but sometimes it is more convenient to use internal classes.

member Inner class (Ordinary inner Class): When an inner class creates an object, it must be created using an external class object, rather than creating an inner object directly with new, that is, an inner class object name = External class object. The new inner Class (); The outer class cannot call the inner class method directly, and must access its properties and methods through the inner class object. If the outer class and the inner class have the same properties or methods, the inner class accesses its own properties and methods by default. If you need access to the member variables of an external class, you can use the This keyword, which is the external class. This property name/method.

Static inner class: is the inner class of static adornments. Features are: (1) static inner classes cannot access non-static members of external classes, but can be accessed through the new external class (). Member. (2) If the external class static member is the same as the inner class member name, you can access the external class static members through the class name. Static members can be accessed directly from the external class by the member name if the static members of the external classes are different from the internal class member names.

(3) When creating a static object, no external object can be created directly, that is, the inner class object name =new inner class ();

Method Inner class: The inner class is defined in the method of the outer class, and the inner class of the method is visible only inside the method, that is, it can be used only within the method. Because the method inner class cannot be used outside the outer class, the method inner class cannot use the access control and static adornments.


Subclasses have all the properties and methods of the parent class, except for the parent class private members.

Inheritance Initialization order: (1) initializes the parent class and initializes the subclass. (2) The properties in the initialization object are executed first, and then the initialization in the constructor method is performed.

Final keyword: Final can modify a class, method, property, variable. Final decorated class of painting, this class can not be inherited, the final decoration method, the method cannot be overridden, the final decorated property, the property is not implicitly initialized, the class initialization must have a value, either in the declaration of the value, or in the construction method to assign value, the final modified variable, The variable becomes constant and can only be assigned once.

Super Keyword: Represents the parent class object inside an object.

Super applications: (1) If the constructor method of a subclass does not explicitly call the parent class's constructor, the system defaults to calling the parent class without the parameter constructor method. (2) If you explicitly call the parent class's construction method, you must put it in the first row. (3) If the subclass construction method does not explicitly call the parent class construction method, and the parent class method does not have a parameterless constructor method, the compilation error occurs.


The parent class references a pointer to this class of objects (execution of this type of method), and the parent class refers to the child class object (either executes the subclass method or executes the inherited parent method).

Abstract modifier

Role: Restricts the methods that subclasses must implement and does not pay attention to how they are implemented.

Rules of Use: (1) abstract Definition Abstraction class, (2) Abstract modification abstraction method, only declaration, without implementation (abstract method does not execute body curly braces, and end with a semicolon); (3) A class containing abstract methods is an abstract class; (4) Abstract classes can contain abstract methods. There can also be no abstract method; (5) Abstract classes cannot be created directly, and reference variables can be defined.


Interfaces are composed of global variables and common abstract methods, which specify the implementation methods of some classes, define the interface with the keyword interface, i.e. [modifier] Interface Interface Name [extends parent interface 1, parent Interface 2 ...] {0--n a constant definition;

0--n an abstract method definition;}

Interfaces are often used to be inherited, implemented, modifiers are generally public, and interfaces cannot be modified with private and protected. The interface must have an abstract adornment, the interface can define multiple parent interfaces, because the interface is constant, so the system defaults to public static final modifier constants, even if there is no modifier decoration before the constant, the system will automatically add. Because the interface is full of abstract methods, the system defaults to the public abstact [modifier] modifier, even if there is no modifier before the method, the system will automatically add.

A class can implement one or more interfaces, implemented with implements, and if the parent class is to be inherited, the inherited parent class must precede the implementation of the interface. The use of an interface is a reference to an object that implements an interface through an interface. Interfaces are often used with anonymous internal classes, such as: Interface I=new interface () {public void Method () {}};(must have a semicolon)

The package name specification should be all lowercase letters.

The * in import can only represent classes and cannot represent packages. If you use a different class, you must import it by using import.

Java.lang.Object is the parent class for all classes.

object is the parent class for all classes. Here are two important ways to do it.

(1) toString ()

(2) equals (), compares whether the object points to the same memory address.

Class object: Describes the code information for a class, that is, the properties of the class.

Class object: Describes the class's data field, which is the property value of the class.

Three major features of Java: Encapsulation inheritance polymorphism

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: 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.