-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab6Q2.java
More file actions
71 lines (57 loc) · 2 KB
/
Copy pathlab6Q2.java
File metadata and controls
71 lines (57 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Person {
protected String name;
protected int age;
protected String address;
public Person(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public void introduceYourself() {
System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
}
public void displayContactInfo() {
System.out.println("Address: " + address);
}
}
class Teacher extends Person {
private String subject;
private int yearsOfExperience;
public Teacher(String name, int age, String address, String subject, int yearsOfExperience) {
super(name, age, address);
this.subject = subject;
this.yearsOfExperience = yearsOfExperience;
}
public void teachClass() {
System.out.println("I am a teacher of " + subject + " with " + yearsOfExperience + " years of experience.");
}
}
class Student extends Person {
private int studentID;
private String major;
public Student(String name, int age, String address, int studentID, String major) {
super(name, age, address);
this.studentID = studentID;
this.major = major;
}
public void study() {
System.out.println("I am a student with ID " + studentID + " majoring in " + major + ".");
}
}
public class lab6Q2 {
public static void main(String[] args) {
Person person = new Person("John Doe", 30, "123 Main St");
person.introduceYourself();
person.displayContactInfo();
System.out.println();
Teacher teacher = new Teacher("Prof. Smith", 45, "456 Oak St", "Computer Science", 10);
teacher.introduceYourself();
teacher.displayContactInfo();
teacher.teachClass();
System.out.println();
Student student = new Student("Alice Johnson", 20, "789 Pine St", 12345, "Biology");
student.introduceYourself();
student.displayContactInfo();
student.study();
}
}