0% found this document useful (0 votes)
28 views

Week008 LabEx

The document discusses polymorphism and inheritance in object-oriented programming. It provides examples of creating a subclass that inherits from a superclass and overrides one of its methods. Specifically, it creates a ComputerScienceStudentRecord class that inherits from StudentRecord and overrides the computeAverageGrade() method. It also discusses using keywords like extends, override, and super when working with inheritance and polymorphism.
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)
28 views

Week008 LabEx

The document discusses polymorphism and inheritance in object-oriented programming. It provides examples of creating a subclass that inherits from a superclass and overrides one of its methods. Specifically, it creates a ComputerScienceStudentRecord class that inherits from StudentRecord and overrides the computeAverageGrade() method. It also discusses using keywords like extends, override, and super when working with inheritance and polymorphism.
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/ 7

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) {


this.name = name;
}

public double getMathGrade() {


returnmathGrade;
}

public void setMathGrade(double mathGrade) {


this.mathGrade = mathGrade;
}

public double getEnglishGrade() {


returnenglishGrade;
}

public void setEnglishGrade(double englishGrade) {


this.englishGrade = englishGrade;
}

public double getScienceGrade() {


returnscienceGrade;
}

public void setScienceGrade(double scienceGrade) {


this.scienceGrade = scienceGrade;
}

//custom method
public double computeAverageGrade(){
return (this.mathGrade + this.englishGrade +
this.scienceGrade)/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 super.computeAverageGrade()
//body
}
D:
public double super.computeAverageGrade()
//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

You might also like