Professional Documents
Culture Documents
Mid Term OOp 1
Mid Term OOp 1
1. When you implement a method that is defined in a superclass, you _______ the original
method.
A. Overload C. Override
B. Copy D. Call
2. If we have two objects c1 and c2, the result of c1=c2 will be:
A. c1 and c2 have the same content C. The compile will print True.
and same reference. D. Both have the same content but
B. c1 and c2 have different content. different references.
Page 1 From 6
1
class Person {
public String getInfo() {
return "Person";
}
public void printPerson() {
System.out.println(getInfo());
}
}
QUESTION 4 [ 5 POINTS]:
Based on the following figure, write the header needed to declare each class.
EducationalMaterial Book
- author: String
- id: int
- title: String
+ EducationalMaterial()
+ Book(id: int, author: String,
+ EducationalMaterial(id: int)
title: String)
+ getID(): int
+ toString(): String
+ setID(id: int): void
+ toString(): String
Additional Specification:
An EducationalMaterial class:
In EducationalMaterial constructor with one argument: id
Use this keyword to initialise the the class attribute.
Page 7 From 6
1
A Book class:
In Book constructor:
- Use super keyword to explicitly invoke parents’ constructor and pass the id.
- Use this keyword to reference the data fields author and title, and assign the passed values to them
Override the toString() method, to
return "Book : EducationalMaterial (id = x), author = y, title = z",
Note: x is the output of the toString()from the superclass
y and z the author and title values of the instance.
package midterm1;
public EducationalMaterial () {
}
@ Override
public String toString() {
}
}
Page 7 From 6
1
Pckage midterm1;
@ Override
public String toString() {
Page 7 From 6