Bài tập kế thừa trong Java | Codelearn

  • TuanLQ7
  • Easy
  • 100 Points

Task

Create 3 classes Person, Student, Teacher with the following information:

Explanantion:

Person claas

  • name, age, address attributes describe name, age and address of the object.
  • Person(name: String, age: int, address: String) is parameter which is used for initializing values to attribues.
  • display() method displays object information on the screen. For example, if name = "Khanh", age = 23, address = "Ha Noi", when we call this method, the screen will display as below:
    Name: Khanh
    Age: 23
    Address: Ha Noi​
  • Other methods are setter and getter.

Student class

  • Student class inherits from Person class.
  • Student(name: String, age: int, address: String, gpa: double) is the parameter constructor which is used for initializing values to attribues.
  • setGpa() and getGpa() are setter and getter.
  • display() method overrides Person class, it is used for displaying the object information on the screen. For example, if name = "Khanh", age = 23, address = "Ha Noi", gpa = 9.0, when we call this method, the screen will display as below:
    Name: Khanh
    Age: 23
    Address: Ha Noi​
    GPA: 9.0​

Teacher class

  • Teacher class overrides Person class.
  • Teacher(name: String, age: int, address: String, salary: int) is the parameter constructor which is used for initializing values to attribues.
  • setSalary() and getSalary() are setter and getter.
  • display() method overrides Person class which is used for displaying object information on the screen. For example, if name = "Tung", age = 34, address = "Ha Noi", salary = 1700, when we call this method, the screen will display as below;
    Name: Tung
    Age: 34
    Address: Ha Noi​
    Salary: 1700

A program to test the above classes:

public class Entry {
	public static void main(String[] args) {
		Student s = new Student("Khanh", 23, "Ha Noi", 9.0);
		s.display();
		System.out.println();
		Teacher t = new Teacher("Tung", 34, "Ha Noi", 1700);
		t.display();
	}
}

When the above code is compiled and executed, it produces the following result:

Name: Khanh
Age: 23
Address: Ha Noi
GPA: 9.0

Name: Tung
Age: 34
Address: Ha Noi
Salary: 1700

Instruction

Code sample:

File Person.java:

public class Person {
	private String name;
	private int age;
	private String address;

	public Person(String name, int age, String address) {
		this.name = name;
		this.age = age;
		this.address = address;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void display() {
		System.out.println("Name: " + name);
		System.out.println("Age: " + age);
		System.out.println("Address: " + address);
	}
}

File Student.java:

public class Student extends Person {
	private double gpa;

	public Student(String name, int age, String address, double gpa) {
		super(name, age, address);
		this.gpa = gpa;
	}

	public double getGpa() {
		return gpa;
	}

	public void setGpa(double gpa) {
		this.gpa = gpa;
	}

	@Override
	public void display() {
		super.display();
		System.out.println("GPA: " + gpa);
	}
}

File Teacher.Java:

public class Teacher extends Person {
	private int salary;

	public Teacher(String name, int age, String address, int salary) {
		super(name, age, address);
		this.salary = salary;
	}

	public int getSalary() {
		return salary;
	}

	public void setSalary(int salary) {
		this.salary = salary;
	}
	@Override
	public void display() {
		super.display();
		System.out.println("Salary: " + salary);
	}
}
Reset
...