Inheritance
Inheritance involves creating a class by extending another class. The extended class is called a child class and the original class is called the parent class. For creating a child class, we need the .class file of the parent class.
The child class is created by adding an extends clause in the child class header.
Once created, an object of the child class contains all the instance variables and constructors and instance methods of child, parent and parent's parent (ancestor) classes.
The object of the child class is created by using a child class constructor. Java requires that a child class constructor call parent class constructor as its first statement using reserved word super.
Write a program that will input the names, ages, weights and heights of three siblings and display the lightest followed by the youngest followed by the tallest of the siblings.
This program will be an enhancement of a previous exercise that did the same. However, this program will additionally provide for inputting height and displaying the tallest sibling.
Do this assignment using inheritance as described in implementation section below.
For this assignment create the following classes.
Sibling
Create a class Sibling with private instance variables for name, age and weight. Provide a constructor with parameters for initializing name, age and weight. Also provide accessor methods for getting name, age and weight.
The content of this class will be identical to the one in the previous exercise. You may copy the contents of this class from the previous exercise.
SiblingExt
Create a class SiblingExt that will extend the class Sibling above and will provide a private instance variable for keeping height. It will provide a constructor with parameters for initializing name, age, weight and height. This constructor will call the parent constructor for initializing name, age and weight and will initialize the height by itself. The class will also provide an accessor method for getting height. (Since this class extends the Sibling class, the accessor methods from the Sibling class for getting name, age and weight will be part of an object of this class).
TestSiblingExt
Write a class TestSiblingExt containing the main method. In the main method, input the name, age, weight and height of each sibling and create a corresponding SiblingExt object containing the data.
After all SiblingExt objects are created, the main method will display the data for the lightest sibling followed by the youngest sibling followed by the tallest sibling. The data will include the sibling name, age, weight and height of the sibling. Use the object accessor methods for getting object data.
This class can be created by modifying the TestSibling class in the previous exercise. You may copy the contents of TestSibling class from a previous exercise and then modify them to create this class.
Instruction
Do the above assignment without using an array of objects.
Input
For testing, use the following input data:
Jack 21 130 69
Judy 24 118 63
John 26 145 70
Note that in the above data Jack is 21 years old, weighs 130 lbs and is 69 inches tall.
Output
The Lightest Sibling: Judy 24 118 63
The Youngest Sibling: Jack 21 130 69
The Tallest Sibling: John 26 145 70
//Sample code for SiblingExt
public class SiblingExt extends Sibling
{
private int height;
public SiblingExt (String n, int a, int w, int h )
{
super (n,a,w );
height = h;
}
public int getHeight ( )
{
return height;
}
}
Creating a SiblingExt object in the main method
String name;
int age;
int weight;
int height;
//Create SiblingExt reference
SiblingExt sibe1, sibe2, sibe3;
// input name, age, weight, height values for first sibling object
//Create the first object of SiblingExt class
sibe1 = new SiblingExt (name, age, weight,height);
The SiblingExt object will contain instance variables and associated constructors and methods from all three blue prints (Object, Sibling, SiblingExt).
Java requires that the first statement in each constructor be a call to the parent constructor using the reserve word super. If the user does not include this call, the compiler adds such a call with no parameters. If such a constructor does not exist in the parent class, the compiler issues an error.
The above rule results in the following: