Lab 9 Inheritance BESE-11
Lab 9 Inheritance BESE-11
Class: BESE-11
Lab 09: Inheritance
The learning objective of this lab is to understand and practice the concept of inheritance, a very
powerful feature of OOP which helps in code reusability.
Activity #1.
What is the output of running the class ActivityOne? Explain the output.
Hint. Remember only super-class data members (instance variables and instance methods) are
inherited by the sub-class, and constructors are not included.
class A
{
public A()
{
System.out.println( "A's no-arg constructor is invoked");
}
}
class B extends A {}
The following Java program compiles correctly. Show its output, and explain what is happening
when an object of Class B is created?
class A
{
public A()
{
System.out.println( "A's constructor is invoked");
}
}
class B extends A
{
Activity #3.
The program fails to compile. Identify the problem and propose a correction? Also explain the
reason.Hint. If a constructor does not explicitly call a super constructor as its first statement, a
call to super() is automatically added.
class A
{
public A( int x) {}
}
class B extends A
{
public B() {}
}
Activity #4.
class Circle
{
private double radius;
Task #1:
The following UML class diagram illustrates an inheritance relationship, wherein the classes
Circle and Rectangle have been extended from the class GeometricObject.
Write a test program that prompts the user to enter width and height of the rectangle, a color,
and a Boolean value to indicate whether the rectangle is filled. The program should create a
Rectangle object and set the color and filled properties using the input. The program should
display the area, perimeter, color, and true or false to indicate whether it is filled or not.
Hint:
In Java, getting the current date is as simple as instantiating the Date object from the Java package java.util:
java.util.Date date=new java.util.Date();
System.out.println(date);
// You may need to use Date class while implementing the getDateCreated() method of GeometricObject class.
Task #2:
Design a class named Person and its two subclasses named Student and Employee. Make
Faculty and Staff subclasses of Employee.
A person has a name, address, phone number, and email address. A student has a class status
(freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an
office, salary, and date hired. A faculty member has office hours and a rank. A staff member has
a title. Override the toString() method in each class to display the class name and the person’s
name.
Write a test program that creates a Person, Student, Employee, Faculty, and Staff, and
invokes their toString() methods.
Hand in the source code from this lab at the appropriate location on the LMS system. You should
hand in a single file named Lab_9_<Your CMS_ID. Your_NAME >.docx (without angle
brackets) that contains ONLY the following.
1) All completed java source code representing the work accomplished for this lab: The
code snippets should contain author information in the comments at the top.
To Receive Credit
1. Comment your program heavily. Intelligent comments and a clean, readable formatting
of your code account for 20% of your grade.
2. The lab time is not intended as free time for working on your programming/other
assignments.