Detailed explanation of java encapsulation inheritance polymorphism

  • 2021-07-10 19:56:01
  • OfStack

Object-oriented programming (Object Oriented Programming) has three characteristics: encapsulation, inheritance and polymorphism. Here, I will deepen my understanding of the three with you.

Encapsulation

Encapsulation can be disassembled, understood and installed, which puts data and methods into classes; Seal, adding access rights to the loaded data and member methods. To the outside, the internal details are transparent, and what is exposed to the outside is its access method.

Inheritance

Inheritance is to reuse the parent class code. If two classes have the relationship of is a, they can use extends. In addition, inheritance paves the way for polymorphism.

Polymorphism

The specific type pointed to by the reference variable defined in the program (java has two major data types, internal data type and reference data type) and the method call issued through the reference variable are uncertain at compile time. It is determined during the running of the program, that is, the instance object of which class a reference variable points to, and the method call issued by the reference variable is the method implemented in which class, which must be determined during the running of the program.
When understanding polymorphism, we can't escape "upward transformation".

The core of the problem is that, in general, after a subclass inherits the parent class, the subclass has all the members and methods of the parent class. However, after the upward transition, some unique members and methods in the subclass are "invisible" to the reference variables of the parent class type.

Instances


package binaryheap.test;

import java.awt.image.SinglePixelPackedSampleModel;

// Create 1 A Animal Class 
class Animal {
	public void run() {
		System.out.println("Animal Running! ! ! ");
		sing();
	}

	public void sing() {
		System.out.println("Animal Singing ~~~");
	}

}

// Create 1 Subclass inherits parent class 
class Lion extends Animal {

	//  Heavy load run() Method , And in the parent class run() The method is not 1 A 
	//  This method will be lost when you transition up 
	public void run(String anotherAnimal) {

		System.out.println(("Lion In pursuit 1 Only " + anotherAnimal));;
		sing();
	}

	//  Rewrite 
	public void sing() {
		System.out.println("Lion Singing ~~~");
	}

}

public class BH {

	public static void main(String[] args) {
		//  Create 1 Parent class reference, but points to Lion Object 
		Animal animal = new Lion();
		animal.run();
	}
}

Results:

Animal is running! ! !
Lion is singing ~ ~ ~

Animal animal = new Lion (); The reference to the Animal type transformed upwards, missing the overloaded method run (), that is, run () in the subclass, points to run () in the parent class, so it outputs "Animal is running" instead of "Lion is running". The sing () method is called in the run () method, because the sing () method is overridden and the reference can find the sing () method, so the reference calls the sing () method (dynamic call) and outputs "Lion is singing".

Necessary conditions for polymorphism are inheritance, overriding, and upward transformation (or parent class references point to subclass objects). The implementation mechanism is that the method called is determined by the type of the referenced object, not the type of the referenced variable, and the called method must be overridden by a subclass.

Summary:

Use the reference of the parent class to point to the subclass object This reference can only call methods and variables defined in the parent class If a subclass overrides a method of the parent class (only methods can be overridden, not variables, otherwise an error will be reported at compile time), this method of the subclass will be called when calling.

In addition, the construction method of superclass may be involved in polymorphic reference. The order of calling superclass constructors is to call the constructor of the farthest superclass first.


Related articles: