Computer Programming 2
Polymorphism and Inheritance 1
Week008- Polymorphism and Inheritance
Laboratory Exercise 007
Objective/s:
At the end of this activity, you should be able to:
· Utilize the knowledge gained in this module to create an application that relies on
polymorphism and inheritance
· Understand the difference between polymorphism and inheritance
· Use an abstract class and an interface
What to Prepare for the Activity:
NetBeans IDE 8.2
JDK8 (Java Development Kit 8)
Procedure:
Create a NetBeans project for this activity. The project name should be as follows:
Project Name: Lab007_<lastname_firstname>
Example: Lab007_Blanco_Maria
Compress the NetBeans project into .rar or .zip format and then upload to the link
provided in the LMS.
Only NetBeans project compressed in .rar or .zip format will be accepted.
1. StudentRecord
a. Class Names: Lab8Main (main class)
b. StudentRecord:
public class StudentRecord {
//these are the attributes
private String name;
private double mathGrade;
private double englishGrade;
Assessments
private double scienceGrade;
//these are the mutators and accessors
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public double getMathGrade() {
returnmathGrade;
}
public void setMathGrade(double mathGrade) {
[Link] = mathGrade;
}
public double getEnglishGrade() {
returnenglishGrade;
}
public void setEnglishGrade(double englishGrade) {
[Link] = englishGrade;
}
public double getScienceGrade() {
returnscienceGrade;
}
public void setScienceGrade(double scienceGrade) {
[Link] = scienceGrade;
}
//custom method
public double computeAverageGrade(){
return ([Link] + [Link] +
[Link])/3;
}
}
c. ComputerScienceStudentRecord
The StudentRecord class is given for your reference. The following are your tasks:
Computer Programming 2
Polymorphism and Inheritance 3
Test Stem / Question Choices
Assessments
1: Create a class that will inherit A: extends
the attributes and methods of the
StudentRecord class. What B: implements
keyword is used in order create a
parent-child relationship between
two classes? C: extension
D: overloads
A: public class ComputerScienceStudentRecord
2: How will you declare that class. extends StudentRecord {
Use the // body
ComputerScienceStudentRecord }
as the identifier of the class.
B: public class StudentRecord extends
ComputerScienceStudentRecord{
// body
}
C: public class ComputerScienceStudentRecord
implements StudentRecord {
// body
}
D: public class ComputerScienceStudentRecord
overloads StudentRecord {
// body
}
3: Overload the StudentRecord A: public ComputerScienceStudentRecord (final
constructor with one that accepts String name, final int mathGrade, final int
4 parameters namely the name, englishGrade, final int scienceGrade) {
mathGrade, englishGrade and // body
scienceGrade.
}
B: public ComputerScienceStudentRecord (final
name, final mathGrade, final englishGrade, final
scienceGrade) {
// body
}
C:
@overloads
public ComputerScienceStudentRecord (final
name, final mathGrade, final englishGrade, final
scienceGrade) {
// body
Computer Programming 2
Polymorphism and Inheritance 5
D:
@overloads
public ComputerScienceStudentRecord (final
String name, final int mathGrade, final int
englishGrade, final int scienceGrade) {
// body
}
A: this
4: Now set the values of the
parameters to the attributes of
the class. What keyword will you
use?
B: super
C: just use the setter. This is the suggested way to
do that.
D: no need for keyword, you can just use the = sign.
A: private int computerProgrammingGrade = 0;
5: Create an attribute named
computerProgrammingGrade
with primitive int data type. How
will you do that?
B: private Integer computerProgrammingGrade =
0;
C: private double computerProgrammingGrade =
0;
D: private computerProgrammingGrade = 0;
Assessments
A:
6: Now override the @override
computeAverageGrade. How will public double computeAverageGrade()
you do that? //body
}
B:
@overrides
public double computeAverageGrade()
//body
}
C:
@overrides
public double [Link]()
//body
}
D:
public double [Link]()
//body
}
A: Both Inheritance and Polymorphism
7: What pillar of Object Oriented
Programming is used when we
override a method from the super
type?
B: Polymorphism only since we are using the
method to morph into another with different
implementation
C: Inheritance only.
D: All of the concept applies.
A: No. Private classes can only be access in the
8: Now try to make the class that it belongs to.
computeAverageGrade private.
Can you still access it in your
subclass?
B: Yes. All are inherited by the subclass.
Computer Programming 2
Polymorphism and Inheritance 7
C: It depends on the additional modifier.
D: Yes. As long as you use the super keyword.
A: Returns an error because final method cannot
9: Now try to make the be overriden.
computeAverageGrade final. Run
and compile. What happens in
your subclass?
B: Returns an error because it is not possible to
declare a method final. It only applies to attributes.
C: It is no longer visible in the sub class.
D: Nothing. The code run perfectly even if there is
an overriden method in the subclass.
A: A only.
10: Create a test class with a main
method. Create an instance of
ComputerScienceStudentRecord.
Which of the following is the
possible way to do that? B: B only.
A. ComputerScienceStudentRecor
d studentRecord = C: Both A & B.
ComputerScienceStudentRecor
d(“List Marie”, 91, 92, 93)
B. StudentRecord studentRecord
= D: None of the choices.
ComputerScienceStudentRecord(“
List Marie”, 91, 92, 93)
Assessments