Object Oriented Programming - Sheet 7 Solution-
Amanyhassan/20225092
1) Trace q1
Output:
Some generic sound
Woof!
Some generic sound
2) Trace q
Vehicle Type: Vehicle, Car Type: Car
Vehicle Type: Vehicle
3) Q3
class Person
{ protected String
name; protected String
type;
public Person(String name, String type) {
this.name = name; this.type = type;
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Type: " + type);
}
class Student extends Person
{ protected int studentID;
public Student(String name, int studentID) {
super(name, "Student"); this.studentID =
studentID;
@Override public void
displayInfo()
{ super.displayInfo();
System.out.println("Student ID: " + studentID);
class StudentWithMajor extends Student
{ private String major;
public StudentWithMajor(String name, int studentID, String major) {
super(name, studentID); this.major = major;
}
@Override public void
displayInfo()
{ super.displayInfo();
System.out.println("Major: " + major);
class Professor extends Person
{ private String subject;
public Professor(String name, String subject) {
super(name, "Professor"); this.subject =
subject;
@Override public void
displayInfo()
{ super.displayInfo();
System.out.println("Subject: " + subject);
public class Main { public static void
main(String[] args) {
System.out.println("Student Information:");
Student s1 = new Student("Alice", 1001); s1.displayInfo();
System.out.println("\nStudent Information with Major:");
StudentWithMajor s2 = new StudentWithMajor("Alice", 1001, "Computer Science");
s2.displayInfo();
System.out.println("\nProfessor Information:");
Professor p1 = new Professor("Dr. Smith", "Physics");
p1.displayInfo();
Output: