1903021102 - Zheng Ziling - Java Week 11 assignment - inheritance, polymorphism and application of abstract classes

project content
Course class blog link Level 19 information and accounting team (this)
This job requires a link Week 11 homework
Blog name 1903021102 - Zheng Ziling - Java Week 11 assignment - inheritance, polymorphism and application of abstract classes
requirement Each question should have a question and code (use the inserted code. If you can't insert the code, check the data and solve it by yourself. Don't directly screenshot the code!!), Screenshot (only the operation results)

Topic 1:

  • Class Person defines name, birth year, and its subclass Graduate defines gpa and graduation year on the basis of inheriting the parent class. Write the test class and output the name, age, graduation year and achievement of the graduates.
  • Tip: the parent class must have a constructor, and the child class calls the constructor of the parent class through super.

The code is as follows:

Parent class:
package zzl.week11;

public class Person {
	String name;
    int birthYear;
	public Person(String name, int birthYear) {
		super();
		this.name = name;
		this.birthYear = birthYear;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getBirthYear() {
		return birthYear;
	}
	public void setBirthYear(int birthYear) {
		this.birthYear = birthYear;
	}
	
}
Subclass:
package zzl.week11;

public class Graduate extends Person {
	int gap;
	int graduateYear; 
	public Graduate(String name, int birthYear, int gap, int graduateYear) {
		super(name, birthYear);
		this.gap = gap;
		this.graduateYear = graduateYear;
	}
	public int getGap() {
		return gap;
	}
	public void setGap(int gap) {
		this.gap = gap;
	}
	public int getGraduateYear() {
		return graduateYear;
	}
	public void setGraduateYear(int graduateYear) {
		this.graduateYear = graduateYear;
	}	
}
Test class:
package zzl.week11;

public class PersonTest {

	public static <Greduate> void main(String[] args) {
		// TODO Auto-generated method stub
		Graduate G = new Graduate("Tutu",2001,90,2022);
		System.out.println("full name:"+G.getName()+"\n Age:"+(G.getGraduateYear()-G.getBirthYear())+"\n Year of graduation:"+G.getGraduateYear()+"\n Achievements:"+G.getGap());
	}

}

Operation results:

Topic 2:

  • Define a basic class shape. There is a draw method. Define three classes: Circle, Triangle and Square. All of them inherit from shape. Define a method doStuff in the test class, pass in the basic class shape as the parameter, and call the draw method. Use the parent class shape to create three instance objects of Circle, Triangle and Square, which are respectively passed into the draw method as parameters.
  • Tip: polymorphism

The code is as follows:

Parent class:
package zzl.week11;

public class Shape {
	void draw() {
	}

}
Subclass Circle(Circle):
package zzl.week11;

public class Circle extends Shape {
	void draw(){
		System.out.println("Circle.draw()");
	}
}
Subclass Triangle(Triangle):
package zzl.week11;

public class Triangle extends Shape {
	void draw(){
		System.out.println("Triangle.draw()");
	}
}
Subclass Square(Square):
package zzl.week11;

public class Square extends Shape {
	void draw(){
		System.out.println("Square.draw()");
	}

}
Test class:
package zzl.week11;

public class ShapeTest {
	static void doStuff(Shape s){
		s.draw();
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Shape c=new Circle();
		Shape s=new Square();
		Shape t=new Triangle();
		doStuff(c);
		doStuff(s);
		doStuff(t);
	}

}

Operation results:

Topic 3:

  • All animals have a parent class Animal, and then define two subclasses Bird and Dog, which inherit from Animal and implement the bark method in the parent class.
  • Tip: Abstract

The code is as follows:

Parent class:
package zzl.week11;

public class Animal {
	public void bark(){
	}
}
Subclass Bird(Birds):
package zzl.week11;

public class Bird extends Animal {
	public void bark(){
		System.out.println("Birds chirp");
	}
}
Subclass Dog(Dog):
package zzl.week11;

public class Dog extends Animal {
	public void bark(){
		System.out.println("The barking of a dog is: woof, woof, woof");
	}
}
Test class:
package zzl.week11;
public class AnimalTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Bird B = new Bird();
		Dog D = new Dog();
		B.bark();
		D.bark();
	}

}

Operation results:

Topic 4:

  • The area calculation formulas of different geometric figures are different, but they have the same characteristics. They both have the two attributes of length and width, and also have the method of area calculation. Calculate the area of rectangle and triangle according to the abstract concept.
  • Tip: Abstract

The code is as follows:

Parent class:
package zzl.week11;

public abstract class Area {
	int width;
	int height;
	public Area(int width, int height) {
		super();
		this.width = width;
		this.height = height;
	}
	
}
Subclass Trigon(Triangle):
package zzl.week11;

public class Trigon extends Area {

	public Trigon(int width, int height) {
		super(width, height);
		// TODO Auto-generated constructor stub
	}
	void Area() {
		// TODO Auto-generated method stub
		System.out.println("The area of the triangle is:"+width*height/2);
	}
}
Subclass Rectangle(rectangle):
package zzl.week11;

public class Rectangle extends Area {

	public Rectangle(int width, int height) {
		super(width, height);
		// TODO Auto-generated constructor stub
	}
	void Area() {
		// TODO Auto-generated method stub
		System.out.println("The area of the rectangle is:"+width*height);
	}
}
Test class:
package zzl.week11;

public class AreaTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Rectangle R = new Rectangle(4,6);
		R.Area();
		Trigon T = new Trigon(4,6);
		T.Area();
	}

}

Operation results:

*Extended reading: Inheritance and polymorphism in Java, abstract classes in Java

experience:

1. java classes modified by abstract are abstract classes.

2. Subclasses of abstract classes are often of the same type. Abstract classes actually provide the public content of the same type of things, and the subclasses of abstract classes implement the public content provided by this abstract class according to their own actual situation. In this way, subclasses do not need to create this public content, but only need to inherit to rewrite it.

3. Elements in abstract classes: local variables cannot be accessed in other methods (no need to consider) classes: instance variables, static member variables, construction methods, instance methods, static methods; Abstract class: instance variable, static member variable, constructor, instance method, static method.

Misunderstanding: judging whether a class is an abstract class is not to see whether there are abstract methods. It depends on whether the current class has abstract modification

4. Specific methods of abstract classes:

(1). Abstract classes cannot be new, and instance elements in abstract classes can be accessed by means of subclasses.

(2). If an ordinary java class inherits an abstract class, you need to rewrite all the abstract methods in the abstract class, otherwise change the ordinary java class into an abstract class.

(3). Abstract classes can inherit other abstract classes without overriding abstract methods.

(4). Create an abstract class object by using the upper transformation object

(5). Abstract class objects can access instance variables, class variables, construction methods, instance methods, class methods and [abstract methods are actually methods after subclass rewriting].

(6). When the method parameter in an ordinary java class is an abstract class type, it can pass the upper transformation object or the subclass object of the abstract class.

Posted by kef on Sun, 15 May 2022 15:19:33 +0300