Understanding of inheritance and polymorphism in Java

Polymorphism means that when a method of a parent class is overridden by its subclasses, they can each produce their own functional behavior.

When a class there are more than a subclass, and these subclasses overrides a method in the parent class. So when we handle a reference class of objects created objects into a parent class, you get an object on a transformation of the object , that the transformation of the object may have a variety of form when calling this method state , because Different subclasses may have different behaviors when they override the methods of the parent class .

Polymorphism is another important feature of object-oriented programming. It means that after the attributes and methods defined in the parent class are inherited by the subclass, they can have different data types or exhibit different behaviors, which makes the same property or method It has different meanings in the parent class and its subclasses.

For object-oriented, polymorphism is divided into compile-time polymorphism and runtime polymorphism . Among them, polymorphism at compile time is static, which mainly refers to the overloading of methods, which distinguishes different methods according to different parameter lists. After compilation, it will become two different methods, and there is no talk of polymorphism at runtime. While runtime polymorphism is dynamic, it is achieved through dynamic binding, which is commonly referred to as polymorphism.  There are three necessary conditions for

Java to achieve polymorphism: inheritance, rewriting and up-casting . Only when these three conditions are met, can developers use unified logic to implement code to process different objects in the same inheritance structure, thereby performing different behaviors.

  • Inheritance: In polymorphism, there must be subclasses and parent classes that have inheritance relationships.
  • Rewrite: The subclass redefines some methods in the parent class, and when calling these methods, it will call the method of the subclass.
  • Up-casting: In polymorphism, the reference of the subclass needs to be assigned to the parent class object. Only in this way can the reference be able to call the method of the parent class and the method of the subclass.
class 动物
{
	void cry(){
	
	}
}

class 狗 extends 动物
{
	void cry(){
		System.out.println("这是狗叫的声音:***");
	}
}

class 猫 extends 动物
{
	void cry(){
		System.out.println("这是猫叫的声音:~~~");
	}
}

public class Example5_10 
{
	public static void main(String[] args) 
	{
		动物 animal = new 狗();  //狗的上转型对象
		animal.cry();
		animal = new 猫();  //狗的上转型对象
		animal.cry();
	}
}

Let's use an example to demonstrate how to rewrite polymorphism. The example uses class inheritance and runtime polymorphism. The specific steps are as follows.

1) Create the Figure class. In this class, first define the size of the storage two-dimensional object, then define the construction method with two parameters, and finally add the area() method, which calculates the area of ​​the object. code show as below:

public class Figure 
{
	double dim1;
	double dim2;

	Figure(double d1, double d2){
		//有参构造函数
		this.dim1 = d1;
		this.dim2 = d2;
	}

	double area(){
		//计算对象面积
		System.out.println("父类中计算对象的方法,没有实际意义,需要在子类中重写。");
		return 0;
	}
}

2) Create a Rectangle subclass inherited from the Figure class. This class calls the construction method of the parent class and overrides the area() method in the parent class. code show as below:

public class Rectangle extends Figure
{
	Rectangle(double d1, double d2){
		super(d1, d2);
	}

	double area(){
		System.out.println("长方形的面积:");
		return super.dim1 * super.dim2;
	}
}

3) Create a Triangle subclass inherited from the Figure class, which is similar to Rectangle. code show as below:

public class Triangle extends Figure
{
	Triangle(double d1, double d2){
		super(d1, d2);
	}

	double area(){
		System.out.println("三角形的面积:");
		return super.dim1 * super.dim2 / 2;
	}
}

4) Create a Test test class. In the main() method of this class, first declare the variable figure of the Figure class, and then specify different objects for the figure variables, and call the area() method of these objects. code show as below:

public class Test 
{
	public static void main(String[] args) 
	{
		Figure figure;   //声明Figure类的变量
		figure = new Rectangle(8,9);
		System.out.println(figure.area());
		System.out.println("=====================");
		figure = new Triangle(6,8);
		System.out.println(figure.area());
		System.out.println("=====================");
		figure = new Figure(10,10);
		System.out.println(figure.area());
	}
}

 

Guess you like

Origin blog.csdn.net/qq_43629083/article/details/108690947