Hello, this article will bring you Java polymorphism.


Those are the six tasks for this study. Let’s take it one by one.

1 Object class

The Object class is the base class for all Java classes.

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

class Person{

 

}

Copy the code

Is equivalent to

class Person extends Object{

}

Copy the code

1. Object instantiation process

Instantiating a class starts with the top-level superclass, a layer by layer structure: “parent class then subclass, static class then member”.

(1) the toString method

ToString: String representation of an object

The Object class defines the public String toString() method, which returns a String value describing information about the current Object.

The toString() method of the object class is automatically called when a String is connected to another type of data (e.g. System.out.println(” hello “+ person))

The toString() method can be overridden in user-defined types as needed.

2 equals method

Equals: compares the default addresses (” compares the first box “). To compare the second box, you need to override this method

Public Boolean equals(Object obj) ¶

Provides logic to define whether objects are “equal”

Equals (y) returns true if x and y are references to the same Object, false otherwise

The JDK overrides the equals(String, Integer, Date, etc.) method of objects, x.equals(y), when x and y refer to the same class of objects with the same attributes (not necessarily the same objects). Returns true otherwise returns false.

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

Exercise: Override toString and equals methods of the “Person” and “Student” classes defined earlier and test them.

2 Object Casting

A reference type variable of a base class can “point to” objects of its subclasses.

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

You can use the reference variable Instanceof class name to determine whether the object that the reference type variable “points to” belongs to that class or a subclass of that class.

Subclass objects can be used as base class objects and are called upcasting or downcasting.

  • Case 1:

There’s Animal, Cat, Dog.

  • Testing:

Superclass reference = superclass object

Subclass reference = subclass object

Superclass reference = subclass object

The parent class references properties and methods inherited from the calling parent class.

Superclass references invoke subclass-specific properties and methods (transition down).

The subclass object instanceof has a parent class.

Subclass-specific members (properties and methods) can be invoked after a cast.

  • Example 2:

The test uses a superclass parameter with a subclass object as an argument

1, overloading

2. Call subclass-specific members by casting

3 polymorphic

“Static binding” : Done at compile time to speed up code execution. Statically binding methods include:

  1. A static method

  2. The constructor

  3. Private methods

  4. Method called with the keyword super

“Dynamic binding” : Refers to calling methods based on the actual type of the referenced object “during execution (not compilation)”. This allows us to be flexible, but it slows down code execution. This is one of the main reasons why JAVA is slower than C/C++.

Polymorphism, which means a variety of forms, is a strategy of ambiguity, which allows for more general code to be written.

The concept of polymorphism was developed based on encapsulation and inheritance. A subclass appears as a parent class, but does things in its own way.

The same thing, the same method, the same parameters, but behave differently.

There are three requirements for polymorphism to occur: there is inheritance, there is overwriting, and a parent class reference refers to a subclass object.

Polymorphic examples:

  • E.g. 1(must be mastered to see through):

Animal Cat Dog

Example 2 (improve understanding of polymorphism) : Think first and then run and see the result

  • Four principles of doing the problem:

1, inheritance chain, he did not find his father;

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

The nearest optimal principle: he did not find his father

4, polymorphism occurs, the base class to the subclass of the new method is not visible

4 an abstract class

When the abstract keyword is used to modify a class, the class is called abstract. When we use abstract to describe a method, the method is called abstract.

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

Abstract classes cannot be instantiated

Abstract methods can only be declared, not implemented

E.g. Animals chirp when they are happy, but different kinds of animals have different calls, and the name/call of the animal (the parent class) will never satisfy the needs of the subclass.

5 interface

An interface is a collection of abstract methods and constant values.

In essence, an interface is a special kind of abstract class that contains only definitions of constants and methods, but no implementations of variables and methods

Semantically, it can be understood that the abstraction of an action, behavior or function is defined as an interface, which is only a standard and complete specification, and not suitable to be defined as a class. For example, “Fly” is a function, airplanes can Fly, birds can Fly, insects can Fly, generally we do not define a class “Fly”, it is not semantic, so it is just a function, a specification, we can define it as an interface for other classes to implement

Interface features

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

Define the syntax format of a Java class:

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

  1. Public static final; public static final;

  2. Only abstract methods can be defined in an interface, and those methods are, by default, public

  3. Interfaces can inherit from other interfaces and add new properties and abstract methods

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

  5. Interface example: Sing Paint.

Exercise: Design the interface to implement the following structure


Understanding the three characteristics of object oriented

When writing code, we pursue “high cohesion and low coupling” to achieve reuse and specification, which needs to use three object-oriented features to achieve:

Encapsulation: Encapsulates hidden information

Inheritance: Inheritance continues + extends parent information

“Polymorphism” : the strategy of polymorphism is ambiguous in response to changes

Encapsulation function:

A) Realize professional division of labor, and develop modules and functions in work.

B) Hiding information and implementation details. Making changes to code safer and easier

“Inheritance function” : implementation of code reuse, continuation + extend parent class information

“Polymorphisms” : The same as the same (like USB ports, as long as you meet my criteria, you can plug in your computer)

Note:

Java three features although simple, but really understand the meaning, not a year and a half of learning, is not to understand.