Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views4 pages

Amanyyy Sheet7

The document provides solutions to exercises on Object Oriented Programming, including class definitions and method implementations. It features classes for Person, Student, StudentWithMajor, and Professor, each with a method to display their information. The main method demonstrates the creation of instances and displays their details, showcasing inheritance and method overriding.

Uploaded by

amanyhassan557
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Amanyyy Sheet7

The document provides solutions to exercises on Object Oriented Programming, including class definitions and method implementations. It features classes for Person, Student, StudentWithMajor, and Professor, each with a method to display their information. The main method demonstrates the creation of instances and displays their details, showcasing inheritance and method overriding.

Uploaded by

amanyhassan557
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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:

You might also like