Teach you how to correctly understand the three characteristics of java! ! ! !

  • 2021-10-16 01:31:47
  • OfStack

Directory Inheritance and Polymorphism 1. Package 2. Inheritance 3: Overload and Rewrite 3.1 Overload (Overload) 3.2 Rewrite 4: Polymorphism 4.1 Transformation of Polymorphism
4.2 instanceof Summary

Inheritance and polymorphism

This chapter explains the three characteristics of object-oriented: encapsulation, inheritance and polymorphism.

Step 1: Package

Java defines a namespace called a package: package. A class always belongs to a package, and the class name (such as Person) is only a shorthand. The real complete class name is the package name. Class name. For example, the apple class is in the package banana. The full class name is banana. apple.

Syntax for custom packages:


package<    Package name    >

Note: Declare that a package must be written in line 1 of the class.

1.2 Package Import

If you want to use a package that exists in java, you can use the import statement in your program to import the package.

The format is as follows: (Note that there is a small dot in the middle)


import<   Package name   >.<   Class name   >

If you want to import more than one class in a package, you can use "*" to indicate all the classes in the package. As in the previous example, the complete code is: import banana. *.

2. Succession

Inheritance means that the subclass inherits the characteristics and behaviors of the parent class, so that the subclass object (instance) has the instance domain and method of the parent class, or the subclass inherits the methods from the parent class, so that the subclass has the same behavior as the parent class.

The function of inheritance: Through inheritance, we can quickly create new classes, realize code reuse, improve the maintainability of programs, save a lot of time to create new classes, and improve development efficiency and quality.

The inheritance syntax format of java is as follows:


[ Modifier ]  class  Subclass name  [extends  Parent class name ]{
        // Class definition section 
}

For example, banana is a class, apple is a class, and banana is set as the parent class of apple


public class apple extends banana{
        // Content 
}

Attention! 1. Inheritance in 1. java is single inheritance, that is, a subclass can only have one direct parent class.

2. Subclasses cannot selectively inherit from parent classes;

3. Java does not support multiple inheritance, but one class can implement multiple interfaces, thus overcoming the shortcomings of single inheritance;

4. The constructor is not inherited by the subclass, but the constructor of the parent class can be called from the subclass.

3: Overloading and rewriting

3.1 Overload (Overload)

In the same class, a method with the same name has different parameter lists (different parameter types, different number of parameters, and even different parameter order), which is an overload. Overload has no requirement on return types, and can be the same or different, but it cannot be judged by whether the return types are the same or not.

Note the following:

(1) The method name is the same as that in Class 1, but the parameter list is different.

(2) The return value type cannot be used to distinguish overloads of methods.

Reminder: Constructors can also implement overloads of methods

3.2 Rewrite

Rewrite, that is, rewrite once. That is, the method of the parent class itself is rewritten once in the subclass. The subclass extends the parent class, and the subclass is a special parent class. The child class is based on the parent class, and new properties and methods are added.

Rules for rewriting:

1. Occurs between a class and a parent class

2. Private methods in the parent class cannot be overridden

3. When overriding the method of the parent class, the access authority cannot be lower than that of the parent class, and the exception can not be thrown wider than that of the parent class or a new exception can be thrown

4. When the parent class has a static method, the child class must be overridden by the static method

The difference between overloading and rewriting:

1. Overloading occurs in 1 class

2. Overrides occur between parent and child classes.

4: Polymorphism

Polymorphism means that the same operation acts on different objects, which can be interpreted differently, resulting in different execution results.

Polymorphism is embodied in the fact that the parent class reference variable can point to the subclass object

Prerequisite: There must be a child-parent relationship.

Note: When a method is called using a polymorphic parent class reference variable, the subclass overridden method is called.

4.1 Transition of polymorphism

Polymorphic transformation can be divided into upward transformation and downward transformation:

Upward transformation: Polymorphism itself is a process of upward transformation

Use format:


 Parent class type   Variable name =new  Subclass type ();

Applicable scenario: When you don't need to face subclass types, you can complete the corresponding operations by improving extensibility or using the functions of parent classes.

Downward conversion: A subclass object that has been converted upwards can be converted from a parent class reference type to a subclass reference type using a cast format

4.2 instanceof

The first operand of the instanceof operator is usually a reference type variable, and the last operand is usually a class, which is used to determine whether the reference variable before the operator is an instance of the type after the operator or its subtype.

Attention! When using the instanceof operator, it is important to note that when compiling the operand before the instancecof operator, the type is either the same as the following class, or it has a parent-child inheritance relationship with the following class, otherwise it will cause compilation errors.

Summarize

That's all for this article. I hope I can bring you some help, and I hope you can pay more attention to more content of this site!


Related articles: