Object Oriented Programming: DMD 139
Teacher, ComputerTeacher, and EnglishTeacher Classes
public class Teacher {
// Complete the method public void teach() which prints "Is a
Teacher"
public void teach() {
[Link]("Is a Teacher");
}
}
public class ComputerTeacher extends Teacher {
// Override the method teach(), which prints "Is a Computer
Teacher"
@Override
public void teach() {
[Link]("Is a Computer Teacher");
}
}
public class EnglishTeacher extends Teacher {
// Override the method teach(), which prints "Is an English
Teacher"
@Override
public void teach() {
[Link]("Is an English Teacher");
}
}
Inheritance Hierarchy and Types
Part a: Inheritance Hierarchy
The inheritance hierarchy for the given classes is illustrated as follows:
Fruit
/ \
/ \
Apple Orange
/ \
/ \
GreenApple RedApple
Part b: Define the Static and Dynamic Type of the Objects (fob1, fob2, fob3)
Object Static Type Dynamic Type
fob1 Fruit Orange
fob2 Fruit Apple
fob3 Fruit GreenApple
Java Code for Class Diagram
// Class A
class A {
// Attributes or methods common to class A
}
// Class B extends A
class B extends A {
// Attributes or methods common to class B
}
// Class C extends A
class C extends A {
// Attributes or methods common to class C
}
// Class ChildA extends A
class ChildA extends A {
// Attributes or methods common to class ChildA
}
// Class ChildB extends B
class ChildB extends B {
// Attributes or methods common to class ChildB
}
// Class ChildC extends C
class ChildC extends C {
// Attributes or methods common to class ChildC
}
// Class D extends ChildB
class D extends ChildB {
// Attributes or methods common to class D
}
// Class MyClassA extends A
class MyClassA extends A {
// Attributes or methods common to class MyClassA
}
// Class MyClassB extends MyClassA
class MyClassB extends MyClassA {
// Attributes or methods common to class MyClassB
}
Java Code Implementation for University UML Diagram
import [Link];
// Class representing University
class University {
private List<College> colleges;
private List<Library> libraries;
private List<Person> people;
public University(List<College> colleges, List<Library> libraries,
List<Person> people) {
[Link] = colleges;
[Link] = libraries;
[Link] = people;
}
// Getters and Setters
public List<College> getColleges() {
return colleges;
}
public void setColleges(List<College> colleges) {
[Link] = colleges;
}
public List<Library> getLibraries() {
return libraries;
}
public void setLibraries(List<Library> libraries) {
[Link] = libraries;
}
public List<Person> getPeople() {
return people;
}
public void setPeople(List<Person> people) {
[Link] = people;
}
}
// Class representing College
class College {
private String name;
public College(String name) {
[Link] = name;
}
// Getter and Setter
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
}
// Class representing Library
class Library {
private String name;
public Library(String name) {
[Link] = name;
}
// Getter and Setter
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
}
// Base class representing Person
class Person {
private String name;
public Person(String name) {
[Link] = name;
}
// Getter and Setter
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
}
// Class representing Student (inherits Person)
class Student extends Person {
public Student(String name) {
super(name);
}
}
// Class representing Employee (inherits Person)
class Employee extends Person {
public Employee(String name) {
super(name);
}
}
// Class representing ComputerStudent (inherits Student)
class ComputerStudent extends Student {
public ComputerStudent(String name) {
super(name);
}
}
// Class representing MedicalStudent (inherits Student)
class MedicalStudent extends Student {
public MedicalStudent(String name) {
super(name);
}
}
// Class representing TeachingStaff (inherits Employee)
class TeachingStaff extends Employee {
public TeachingStaff(String name) {
super(name);
}
}
// Class representing OfficeStaff (inherits Employee)
class OfficeStaff extends Employee {
public OfficeStaff(String name) {
super(name);
}
}
public class Main {
public static void main(String[] args) {
// Example usage
// Create some colleges
College college1 = new College("Engineering College");
College college2 = new College("Medical College");
// Create some libraries
Library library1 = new Library("Central Library");
Library library2 = new Library("Science Library");
// Create some people
Person student1 = new ComputerStudent("Alice");
Person student2 = new MedicalStudent("Bob");
Person teacher1 = new TeachingStaff("Dr. Smith");
Person staff1 = new OfficeStaff("Mr. Johnson");
// Create a list of colleges, libraries, and people
List<College> colleges = [Link](college1, college2);
List<Library> libraries = [Link](library1, library2);
List<Person> people = [Link](student1, student2, teacher1,
staff1);
// Create a university
University university = new University(colleges, libraries,
people);
// Example output
[Link]("University has the following colleges:");
for (College c : [Link]()) {
[Link]("- " + [Link]());
}
[Link]("University has the following libraries:");
for (Library l : [Link]()) {
[Link]("- " + [Link]());
}
[Link]("University has the following people:");
for (Person p : [Link]()) {
[Link]("- " + [Link]());
}
}
}