Java object-Oriented learning notes--3 (inheritance, styling, rewriting)

Source: Internet
Author: User

1. Inheritance

1) extends keyword, the use of inheritance can be used to implement code reuse, in the Java language, you need to implement class inheritance through the extends keyword. When inheritance is complete, subclasses (sub Class) can inherit member variables and member methods of the parent class (Super Class), and subclasses can also define their own member variables and member methods. By then, the subclass will have members of the parent class and members of this class. It is important to note that the Java language does not support multiple inheritance, that is, a class can inherit only one parent class, but a parent class may have multiple subclasses.

2) Benefits of using inheritance:

① inheritance is the most effective means of constructing, building and expanding new classes on the basis of some more general classes.

② inheritance simplifies people's understanding and description of things, and can clearly reflect the hierarchical relationship among related classes.

③ inheritance provides the software reuse function, reduces the redundancy of code and data, and greatly increases the reusability of the program.

④ inheritance reduces the interface and interface between modules by enhancing consistency, greatly increasing the ease of maintenance of the program

3) The order in which constructors are called in succession

① the subclass constructor is bound to call the parent class constructor, which calls the parent class's parameterless constructor by default

The ② subclass parent class does not write the constructor, and there is also a call relationship.

The ③ call to the parent class constructor can only be written in the first row of the subclass constructor, not when it is written, and the first line by default.

④ when a constructor in the subclass constructor is specified that invokes the parent class, the default call to the parameterless constructor is no longer performed

4) Use of super

①super (): Used to call the parent class without a parameter constructor, must be used in the first row of the subclass constructor, can not be written.

②super. Properties, accessing the properties of the parent class object

③super. Methods, calling methods of the parent class


2. Upward styling

An object of a subclass can be styled as the type of the parent class. That is, a reference that defines a parent type can point to an object of a subclass, but a reference to the parent class only accesses the members defined by the parent class and cannot access the part of the child class that is expanded.

Parent type variable = new subclass ();

Upward styling is a manifestation of polymorphism, and can make the program simpler and clearer.

property is bound to the type of the variable, and the variable type determines which property is accessed;

method is dynamically bound to an object, and the type of the object determines which method to access.

Strong turn:

parent type Variable 1 = new subclass ();

Subtype Variable 2 = (subtype) variable 1;

A strong turn pair method dynamically binds to an object without affecting it, because a strong turn is a reference to the parent class, and the instance is unchanged, just as the instance is viewed as another state. But strong forwarding has an effect on the dynamic binding of attributes to variable types.

/**

      *  in Tetris Games, each falling block is randomly generated, but there is no guarantee of what type of block will be generated the next time .

* Therefore, when you get a block, you need to use the parent type as the return value type of the method.

*/

public static Tetromino Getonttetronino () {

int num = (int) (Math.random () * 7);

Switch (num) {

Case 0:return New T (); Break

Case 1:return New O (); Break

Case 2:return New L (); Break

... ...

}

}

And then receive

public static void Main (string[] args) {

Tetromino Tetro = Getonttetronino ();

}


3. Overriding the method

Override: occurs in two classes, and is a relationship of the child parent class, the subclass method is the same as the parent method signature, and the method body is different.

Attention:

1) When the override method is called, look at the object

2) Two the same two small one big principle

2.1) Two identical: Method name with, parameter list same as

2.2) Two small: Subclass return value type less than equals parent class

2.2.1) return value using void and base type must be the same

2.2.2) When the return value is a reference type, the child class is less than or equal to the parent class

2.3) One large: subclass access rights greater than or equal to parent class

Benefits of Rewriting:

Overrides are extensions to the functionality of the parent class, the ability to have a parent class, the ability to implement your own functionality, ease of use in the actual development process, and a clear code structure.

The difference between overrides and overloads:

1) overrides occur in 2 categories, and are 2 classes of character-class relationships, and overloads occur in a class.

2) The principle of write-through is run-time binding, overload is the compile-time binding

3) overrides must have the same method name, parameter list is the same, overloading is the same method name, parameter list must be different.

This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1654863

Java object-Oriented learning notes--3 (inheritance, styling, rewriting)

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.