

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An exercise on java inheritance, where you will create a new class (subclass) that extends a previously defined class (superclass) and inherits its data and methods. The example uses classes person and employee, and you will create a new class student that extends person. Pay attention to the constructor calls, method overriding, and access modifiers.
Typology: Lab Reports
1 / 2
This page cannot be seen from the preview
Don't miss anything!


In this exercise you will:
Inheritance allows us to create a new class (called the subclass or child class) that extends a previously defined class (called the super, parent, or base class). This new class inherits all of the data and methods of the super class, and then adds additional data or methods. Inheritance allows us to reuse Java code that has been previously developed thus decreasing the costs needed to develop a new application.
For example: class Person contains data and methods for information common to all persons (name, date of birth and address). Class Employee extends class Person and therefore inherits all of the data and methods of the Person class. Class Employee then adds data and methods for the employee’s department, title and salary.
Closely examine files Person.java and Employee.java. Pay particular attention to the following:
Step 1. Compile and run test program InheritanceTest.java and see how the Employee objects are able to execute methods in the Person class.
Step 2. Now create class Student which will extend Person and contain major, hours, and GPA data.
Step 3. Uncomment the code in InheritanceTest.java and compile and execute it to test your solution.