Getting Started with Java--deep understanding of Java's three main features

Source: Internet
Author: User

Getting Started with Java--deep understanding of Java's three main features Content of this blog:
    1. Packaging
    2. Inherited
    3. Polymorphic
Package
    1. Encapsulation Getter,setter The properties of an object, and provides a way to provide some properties that can be accessed by the outside world, and if you do not want to be outside the method, we do not need to provide a way for outside access.
    2. encapsulation does allow us to easily modify the internal implementation of a class without modifying the client code that uses that class .
    3. You can have more precise control over member variables. (Validation of the actual meaning in the setter method)

Summary: Control attribute access, not if you want to change it. Easy to modify the property type. Precise control over the value range of the property.

Inherited
    1. Inheritance is the technique of building a new class using the definition of an existing class, and the definition of a new class can add new data or new functionality, or it can use the functionality of the parent class, but not selectively inherit the parent class.
    2. Subclasses have properties and methods that are not private to the parent class.
    3. Subclasses can have their own properties and methods, that is, subclasses can extend the parent class.
    4. Subclasses can implement methods of the parent class in their own way.

< constructors ><protected keywords >< up transformation > is three words to mention

    1. Constructors: In addition to private, there are some subclasses that cannot inherit. For example: a constructor can only be called, not inherited. Call the parent class as long as you use Super (). The subclass will call the default constructor of the parent class by default, and if there is no default constructor, you must explicitly indicate the constructor.
    2. Protected: For protected, it indicates that he is private for the class user, but is accessible to any subclass that inherits from this class or any other class that is in the same package.
    3. Upward transformation: Converts a subclass into a parent class, which is moved upward above the inheritance relationship, so it is generally called upward transformation. Since the upward transformation is converted from a private type to a more general type, it is always safe and the only change that can occur is the loss of properties and methods. This is why the compiler still allows for upward transformation in cases where "the transformation has not been explicitly expressed" and "no special tags have been specified".
    4. Ask yourself if you need to transform from subclass to parent class. If you have to move up, inheritance is necessary, but if you don't, you should consider whether you need to inherit.

Polymorphic
    1. Polymorphism refers to the specific type of reference variable defined in the program and the method call issued by the reference variable is not determined during programming, but is determined during the program's run, that is, a reference to which the variable bottom will point to which class the instance object, the reference variable emitted by the method call is exactly the method implemented in the class. Must be determined by the time the program is running . Because when the program is run to determine the specific class, so that, without modifying the source code, you can bind the reference variable to a variety of different class implementations, resulting in the invocation of the specific method of the reference change, that is, do not modify the program code can be changed when the program runs the specific code, so that the program can select multiple running state, This is polymorphism.

    2. A parent class reference to a subclass because it has been transformed upward, it can only access methods and properties that are owned in the parent class, and the reference is not available for methods that exist in the subclass and that do not exist in the parent class, although the method is overloaded. If subclasses override some of the methods in the parent class, they must be called using the methods defined in the subclass (dynamic joins, dynamic calls).

Java's three major features are the most basic and most important concepts in Java development, and the understanding of these three concepts can only be more and more deep, but there is absolutely no end. In particular, to pay attention to their own mentality, can not be impatient, to the most simple problem of resistance to live, thinking and thinking again, truly inward in the heart.

Getting Started with Java--deep understanding of Java's three main 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.