Java three major features

Source: Internet
Author: User
Tags modifier

Inherited

Inheritance makes it easier to implement class extensions. Using extends

Note:

    • Classes in Java have only single inheritance, and interfaces have multiple inheritance.
    • Subclasses inherit the parent class, and you can get all the properties and methods of the parent class (except for the parent class's constructor), but not necessarily directly accessible (for example, properties and methods that are private to the parent class).
    • Parent class of all classes: Object.
    • The instanceof is a two-tuple operator, the object on the left, the class on the right, and true if the object is the object created by the class or subclass that is on the right; otherwise, false.
Override of Method override

Subclasses can replace the behavior of the parent class with their own behavior by overriding the methods of the parent class. The rewriting of a method is a necessary condition for implementing polymorphism.

The rewrite of the method needs to meet the following three points:

    1. "= =": Method name, formal parameter list is the same.
    2. "≤": Returns the value type and declares the exception type, and the subclass is less than or equal to the parent class.
    3. "≥": access rights, subclasses greater than or equal to the parent class.
= = and Equals method

the "= =" represents the comparison between the two sides. If it is the base type, the value is equal, and if it is a reference type, the address is equal

The Equals method of object, by default, compares the hashcode of two objects, returns true if the reference is the same object, or false otherwise. However, we can rewrite the Equals method according to our own requirements.

Super

Super is a reference to the immediate parent class object. You can access the methods or properties covered by the quilt class in the parent class through Super.

If the first line of code for the constructor method does not explicitly call super (...) Or this (...); Java, by default, calls Super (), meaning that it calls the parameterless constructor of the parent class.

To construct the method invocation order:

Construction method The first sentence is always: super (...) To invoke the corresponding constructor method of the parent class. So, the process is to go back up to object, then execute the initialization block and construction method of the class in turn, until the current subclass.

Note: Static initialization of block invocation order, as in the order of constructor call, is no longer duplicated.

Packaging

Needs to let the user know to expose, does not need to let the user know all hides, this is the encapsulation. Encapsulation is the combination of the properties and operations of an object as a separate whole, as much as possible to hide the internal implementation details of the object.

Implementation of encapsulation--access control character

    1. Private means that only their own classes can access

    2. Default means no modifier adornments, only classes of the same package can access

    3. Protected means that it can be accessed by the same package class and by subclasses in other packages

    4. Public means that it can be accessed by all classes in all packages of the project

The handling of the properties of the class:

    1. Private access is generally used.

    2. Provides an appropriate Get/set method to access related properties, which are usually public-decorated to provide assignment and read operations to the property (note: The Get method of the Boolean variable is the start of the is!).

    3. Some helper methods that are used only for this class can be decorated with private, and the methods that other classes invoke are intended to be decorated with public.

Polymorphic

Polymorphism refers to the same method call, which may behave differently due to different objects.

Note:

    1. Polymorphism is the polymorphism of the method, not the polymorphism of the attribute (polymorphism is independent of the attribute).
    2. There are 3 prerequisites for polymorphic existence: inheritance, method overrides, and parent-class references to child-class objects.
    3. When a parent class reference points to a subclass object, the parent class reference invokes the method overridden by the subclass, at which time the polymorphism appears.
Transformation of objects (casting)

The parent class reference points to the subclass object, which we call the process as an upward transformation, which belongs to the automatic type conversion.

An upward-transformed parent class reference variable can only call a method of its compiled type, and cannot invoke a method of its run-time type. At this point, we need to do a type of coercion, which we call downward transformation!

Final keyword
    1. Modifier variable: The variable that is modified by him cannot be changed. Once the initial value is assigned, it cannot be re-assigned.
    2. Modification method: The method cannot be overridden by a quilt class. But can be overloaded!
    3. Decorated class: A decorated class cannot be inherited. For example: Math, String, and so on.

Java three major features

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.