Responsive Advertisement

Display information of students and teachers java

 Display information of students and teachers java

Define a class named The person that contains two instance variables of type String that stores the first name and last name of a person and appropriate accessor and mutator methods.

Also, create a method named display details that outputs the details of a person.

Next,  define a  class named  The student that is derived from  Person,  the constructor for which should receive the first name and last name from the class Student and also assigns values to student id, course, and teacher name. This class should redefine the display details method to personal details as well as details of a student. Include appropriate constructor(s).

Define a class named The teacher that is derived from Person. This class should contain an instance variables for the subject name and salary.

Include appropriate constructor(s). Finally, redefine the display details method to include all teacher information in the printout.

Create a main method that creates at least two student objects and two teacher objects with  different  values  and  calls  displayDetails for each.

public class Person {
String firstName;
String lastName;

String getFirstName() { return firstName; }
String getLastName() { return lastName; }

void setFirstName(String firstName) { this.firstName = firstName; }
void setLastName(String lastName) { this.lastName = lastName; }

void displayDetails() {
System.out.println("First name: " + this.firstName);
System.out.println("Last name: " + this.lastName);

public class Student extends Person {
String id;
String course;
String teacher;

Student(String firstName, String lastName, String id, String course, String teacher) {
this.firstName = firstName;
this.lastName = lastName; = id;
this.course = course;
this.teacher = teacher;

void displayDetails() {
System.out.println("First Name: " + this.firstName);
System.out.println("Last Name: " + this.lastName);
System.out.println("Student ID: " +;
System.out.println("Course: " + this.course);
System.out.println("Teacher: " + this.teacher);

public class Teacher extends Person {
String name;
int salary;

Teacher (String name, int salary) { = name;
this.salary = salary;

void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);

public class PersonTest {
public static void main(String args[]) {
Student student1 = new Student("Muhammad", "Tayyab", "023-2000", "BSCS-II", "Faheem Akhtar");
Student student2 = new Student("Amjad", "Umar", "023-2003", "BSCS-II", "Sheeraz Shah");

Teacher teacher1 = new Teacher("Faheem Akhtar", 200000);
Teacher teacher2 = new Teacher("Sher Muhammad", 35000);

System.out.println("\n\nPrinting data of students: \n");

System.out.println("\n\nPrinting data of teachers: \n");