java inheritance and polymorphism

java inheritance and polymorphism

preface

Tip: the following is the main content of this article. The following cases can be used for reference

1, What is inheritance?

If class B has all the attributes and methods of class A, and has some special attributes and methods, class A is called a general class. Class B is called a special class. This general and special structure can be expressed and implemented by inheritance. Inheritance 1 makes one kind of object inherit the properties and methods of another kind of object.

2, Implementation of inheritance

explain:
For the declaration of the same kind of modifier, [public omitted], or [final], or none.
Subclass names must conform to naming rules
Extensions is the keyword
The parent class can be a user-defined class or a class in the system class library. If the parent class is omitted, its default parent class is the java.lang.Object class
Subclasses can add new member variables and member methods, hide the member variables of the parent class, or override the member methods in the parent class
Java only supports single inheritance
Inheritance between classes is transitive.
In Java, the class java.lang.Object is all the parent or root classes, and all classes are obtained by directly or indirectly inheriting java.lang.Object. Therefore, objects are often referred to as the source of ten thousand classes.
Java does not support multiple inheritance. A subclass can only inherit one parent class. A parent class includes all classes that inherit it directly or indirectly. Class inheritance can be passed.
Shape.java file

package first;

public class Shape {
		String type; //Define categories;
		public void setType(String type) {
			this.type=type; //The member method sets its drawing type
		}
		public String getType() {
			return type;
		}
}

Circle.java file

package first;

public class Circle extends  Shape {
			double radius; //Define member variables
		public double  getRadius() { //Define your own member methods
			return radius;
		}
		public void setRadius(double radius) {
			this.radius=radius;
		}
		public  double getArea() {  //Calculate the area of a circle
			double area=Math.PI*radius*radius;
			return area;
		}
		public double getPerimeter() {
			return 2*Math.PI*radius;
		}
		public static void main(String[] args) {
			Circle myshape= new Circle();
			myshape.setType("circular");
			myshape.setRadius(5.2);
			System.out.println("myshape The categories are:"+myshape.getType());
			double area=myshape.getArea();
			System.out.println("myshape The area is:"+area);
			System.out.println("myshape The perimeter of the is:"+myshape.getPerimeter());
					
		}
}

1. Rules of succession

In Java, when the inheritance relationship between the direct subclass and the direct parent class is declared, the direct subclass inherits the direct parent class. In addition to construction methods, a subclass can inherit all member method variables and member methods of the parent class, but whether it can be accessed directly depends on the control range of its access modifier.

2. Inheritance of attributes

  1. Property inheritance and extension
    According to inheritance rules, a subclass can inherit all the attributes of its parent class and add its own member variables.

3. Attribute hiding

Property hiding means that a subclass redefines a variable exactly the same as the variable inherited from the parent class. Hidden means that a subclass has two member variables with the same name. One is inherited from the parent class, and the other is a self-defined member variable.

The attribute code hiding test code is as follows (example):

Person.java file

package first;

public class Person {
		String id;
		String name;
		String address="111";
		public Person() {
			   
		}
		public void showName() {
			System.out.println(name); //The output is the name in the parent class
		}
	
		
}

Student.java file

package first;

public class Student extends Person{
	String address="Changsha";
	String school="Changsha Institute of Technology";
	public Student() {
		
	}
	public void showInfo() {
		showName();//Call the method of the parent class;
		System.out.println("Student:"+address+" "+"Student:"+school);
		//adress of subclass is used;
	}
	public static void main(String[] args) {
		Student stu= new Student();
		stu.showInfo();
	}
} 

3, Method inheritance

1. Inheritance and extension of methods

Subclasses can inherit the member methods of the parent class or add their own member methods. Subclass objects can use the methods inherited from the parent class.

2. Method rewriting

Method rewriting means that a subclass redefines the methods inherited from the parent class, so that the subclass has its own behavior to meet its own needs.
Method rewriting should pay attention to the following issues:
The method overridden in the subclass should have exactly the same method name as the overridden method in the parent class. Parameter list, return value type, but the executed method body is different
The rewritten method cannot have more strict access restrictions than the rewritten method (the access control right is private default protected public in strict order), that is, the principle of child analogy and parent class expansion.
You can partially rewrite the parent method and add a new function on the basis of the original method, that is, add a statement at the first statement position of the override method of the subclass, super() the original method name ().
The final () method of the parent class cannot be overridden. The purpose of the final method definition is to prevent it from being overridden.
For static modifier, the subclass and parent must be consistent, both or none
Method rewriting is generally used in the following cases
The subclass implements the same functions as the parent class, but the algorithm is different
In the method with the same name, the subclass has more operations than the parent class
Cancel the method inherited from the parent class in the child class. In this case, you only need to override the unnecessary parent class method and set the method body to null.

Think about the difference between overloading and rewriting?
When a subclass overrides a parent class method, because the method with the same name belongs to the parent class and subclass respectively, it is necessary to distinguish which method in the class is called. Generally, as long as the object of different class or different class name is used in front of the method, the super keyword can be used if the method of the parent class is called directly.

Keywords: Java OOP Polymorphism inheritance

Added by vijdev on Sat, 02 Oct 2021 20:58:20 +0300