201671010116.2016-2017-4 "Java Programming" understanding objects and classes and inheritance

Source: Internet
Author: User

1. Classes and objects are the core and foundation of the object-oriented programming idea. A class exists as an abstraction of an object, and all objects are generated according to the corresponding class, and in object-oriented terminology, the process of producing an object is called instantiation. A class is a classification or division of a kind of thing or a type of object. We can even say that "class" is just an adjective, and ' object ' is one of the "entity objects" described by this adjective. Of course there are exceptions, such as we all know that there is a keyword in Java ' static ', we all know that the static modified by the variable or method, when used is not necessary to instantiate the object. Just use the class name + variable name/method name. The static object/method is attributed to the current class, and the non-static object/method belongs to the current ' object ', and the object is a part of the class, of course, the static object can be used, and vice versa.

A constructor is a special data structure that is provided when an object is created, and it is necessary to understand that the constructor itself is not a function. It has exactly the same name as the class in which it resides. Once the good one constructor is defined, the object is automatically called when it is instantiated, and the constructor itself does not have any return type, and it cannot be mistaken to assume that the return type of the constructor is void, and that the type of the constructor return value is the class itself.

Constructors at the source level we can think of as a special method, and sometimes we call it a constructor of a class, which is characterized as follows:

[1] The method name of the constructor must be the same as the class name.

[2] The constructor does not have a return type and cannot use void as the return type, which cannot be preceded by a method name.

[3] The function of the constructor is to complete the initialization of the object, it can be defined when the object parameters are passed to the object.

[4] A class can have more than one constructor, the constructor also supports overloading, the overloaded law follows the overloaded law of the function.

[5] If no constructor is defined, the system defaults to a parameterless constructor, and this function does nothing.

2. Inheritance of classes: Inheritance is an integral part of all OOP languages, and the inheritance of Java classes is mainly accomplished by extends keywords. When a class is created, it is always inherited, and it is always implicitly inherited from the root class object if it is not explicitly pointed out to inherit the class. Once inherited, the subclass gets the member property of the parent class and the member method. Basic syntax: Class name extends the parent class name.

When a subclass inherits a class, the member variables in the parent class can be used, but not all member variables of the parent class are fully inherited. The specific principles are as follows:

1) The ability to inherit the public and protected member variables of the parent class, and the private member variables of the parent class cannot be inherited;

2) for the package access member variable of the parent class, the subclass can inherit if the child class and parent class are under the same package, otherwise the subclass cannot inherit;

3) for the parent class member variable that the subclass can inherit, if a member variable of the same name appears in the subclass, a shadowing occurs, that is, the member variable of the child class masks the member variable with the same name as the parent class. If you want to access a member variable of the same name in the parent class in a subclass, you need to use the Super keyword to refer to it.

Subclasses also do not fully inherit all the methods of the parent class, as follows:

1) The ability to inherit the public and protected member methods of the parent class, and the private member method of the parent class cannot be inherited;

2) for the parent class of the package access member method, if the child class and the parent class under the same package, the subclass can inherit, otherwise, the subclass can not inherit;

3) for the parent class member method that the subclass can inherit, if a member method of the same name appears in the subclass, then it is called overwrite, that is, the member method of the subclass overrides the member method with the same name as the parent class. If you want to access a member method of the same name in the parent class in a subclass, you need to use the Super keyword to refer to it.

Note: Hiding and overwriting are different. Shadowing is for member variables and static methods, and overrides are for common methods.

Subclasses are constructors that cannot inherit the parent class, but note that if the constructor of the parent class is parameterized, the constructor of the parent class must be called with the Super keyword in the constructor of the child class and be accompanied by the appropriate argument list. If the parent class has a parameterless constructor, it is not necessary to call the parent class constructor with the Super keyword in the subclass's constructor, and if the Super keyword is not used, the system automatically calls the parent class's parameterless constructor.

201671010116.2016-2017-4 "Java Programming" understanding objects and classes and inheritance

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.