Java's three major object-oriented features and Java polymorphic learning-Le byte

Hello everyone, this article will bring you Java polymorphism.

The above are the six major tasks of this study. Let's look at them in turn.

1 Object class

The Object class is the root base class of all Java classes.

If the extends keyword is not used in the class declaration to indicate its base class, the default base class is the Object class.

class Person{
    
    
	
}

Equivalent to

class Person extends Object{
    
    
}

VX lezijie007 (encryption 33)

1. The instantiation process of the object

Instantiating a class is instantiated from the top-level superclass, which is a layer-by-layer wrapping structure. "First parent class and then child class, static first and then members".

⑴toString method

toString: the string representation of the object

The public String toString() method is defined in the Object class, and its return value is of type String, which is used to describe the relevant information of the current object.

When connecting String and other types of data (such as: System.out.println("hello" + person)), the toString() method of the object class will be automatically called

You can override the toString() method in the user-defined type as needed.

⑵equals method

equals: compare equal, the default address comparison ("the comparison of the first box"), to compare the second box, you need to rewrite this method

Defined in the Object class: public boolean equals (Object obj) method

Provide logic to define whether objects are "equal"

The equals method of Object is defined as: x.equals(y) returns true when x and y are references to the same object, otherwise it returns false

Some of the classes provided by the JDK, such as String, Integer, Date, etc., have rewritten the equals method of Object, calling the equals method of these classes, x.equals(y), when the objects referenced by x and y are objects of the same type And when the attribute content is equal (not necessarily the same object), return true otherwise return false.

You can override the equals method in user-defined types as needed

Exercise: Rewrite the toString and equals methods of the previously defined "Person" and "Student" classes, and test them.

2 Object transformation (casting)

A reference type variable of a base class can "point to" an object of its subclass.

A reference to a base class cannot access the new members (including properties and methods) of its subclass objects.

You can use "reference variable instanceof class name" to determine whether the object "pointed to" by the reference type variable belongs to this class or a subclass of this class.

Subclass objects can be used as base class objects, called upcasting, and vice versa, downcasting

  • Example 1:

There are Animal class (parent class), Cat class (subclass), Dog class (subclass).

  • test:

Parent class reference = parent class object

Subclass reference = subclass object

Parent class reference = subclass object

The parent class invokes the properties and methods inherited from the parent class.

The parent class references the unique properties and methods of the subclass (downcast).

The subclass object instanceof the parent class.

After coerced type conversion, the unique members (attributes and methods) of the subclass can be called.

  • Example 2:

The test uses the parent parameter and the subclass object as the actual parameter

1. Overload

2. Call the unique members of the subclass through forced type conversion

3 polymorphism

Static binding (static binding) : It is completed during compilation, which can improve code execution speed. Methods of static binding include:

  1. Static method

  2. Constructor

  3. private method

  4. Method called with keyword super

Dynamic binding (dynamic binding) : Refers to judging the actual type of the referenced object "during execution (not during compilation)" and calling its corresponding method according to its actual type. Although this gives us flexibility in programming, it reduces the execution speed of the code. This is also one of the main reasons why JAVA is slower than C/C++.

Polymorphism, polymorphism is a variety of forms, fuzzy strategy, in order to keep changing, using polymorphism can write more general code.

The concept of polymorphism was developed based on encapsulation and inheritance. The subclass appears as the parent, but it is still implemented in its own way when doing things.

When the same thing is called with the same method and the same parameters, the behavior is different.

There are three necessary conditions for polymorphism to occur: there must be inheritance, there must be rewriting, and the parent class reference points to the child class object.

Polymorphic example:

  • Example 1 (must master and see through):

Animal Cat Dog

Example 2 (Improve understanding of polymorphism): Think first, then run to see the results

  • Four principles of problem solving:

1. Inheritance chain, did not find a father;

2. Compile to see the type + determine the method table, run to find the object

3. The principle of nearest best: I did not find my father

4. Polymorphism occurs, and the base class is not visible to the new method of the subclass

4 abstract class

When the abstract keyword is used to modify a class, the class is called an abstract class; when the abstract keyword is used to modify a method, the method is called an abstract method.

Classes containing abstract methods must be declared as abstract classes, abstract classes must be inherited, and abstract methods must be rewritten

Abstract class cannot be instantiated

Abstract methods only need to be declared but not implemented

Example: Animals will scream when they are happy, but for different types of animals, their respective calls are different, and the call/calls of animals (the parent category) can never meet the needs of subcategories.

5 interface

Interface is a collection of abstract methods and constant value definitions.

In essence, an interface is a special abstract class, which contains only the definition of constants and methods, but no implementation of variables and methods.

It can be understood from the semantics that for a certain kind of abstraction of actions, behaviors, and functions, we define it as an interface, which is just a standard and complete specification, and it is not suitable to be defined as a class. For example: the function of flying, airplanes can fly (with the function of flying), birds can fly (with the function of flying), and insects can also fly. Generally, we don't define a class Fly, which is semantically impractical, so this is just one A function, a specification, we can define it as a kind of interface for other classes to implement

Interface characteristics

  1. Multiple unrelated classes can implement the same interface
  2. A class can implement multiple unrelated interfaces
  3. Similar to inheritance, there is polymorphism between interfaces and implementation classes

Define the syntax format of the Java class:

<modifier> class <name>[extends<superclass>] [implements<interface>[,<interface>]…]{…}

  1. The properties declared in the interface are public static final by default, and can only be public static final;

  2. Only abstract methods can be defined in the interface, and these methods are public by default, and can only be public

  3. Interface can inherit other interfaces and add new attributes and abstract methods

  4. Interface cannot implement another interface, but can inherit multiple other interfaces

  5. Interface example: Sing this ability Paint this ability.

Exercise: Design the interface to implement the following structure

6 Understanding of three object-oriented characteristics

When writing code, we pursue "high cohesion and low coupling" to achieve reuse and specification, we need to use three object-oriented features to achieve:

Encapsulation : encapsulation hide information

Inheritance : inheritance continuation + extended parent class information

Polymorphism : polymorphism fuzzy strategy in order to keep changing

Encapsulation function:

a) Realize professional division of labor, sub-module and sub-function development in the work.

b) Hide information and implementation details. Make changes to the code safer and easier

Inheritance : to achieve code reuse, continuation + extension of parent class information

Polymorphic effect : keep changing (such as USB interface, as long as you achieve my standard, you can plug in the computer)

note:

Although the three major characteristics of java are simple, they can truly understand the meaning of them. Without a year and a half of learning, they can’t understand them.

Guess you like

Origin blog.csdn.net/dirft_lez/article/details/108062383