Topics

Inheritance

Discussion

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.

Description

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.

Implementation

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.

Testing

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

//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);

System steps In Creating SiblingExt Object

  • Load the Class SiblingExt (SiblingExt.class file) in memory to use it as a blue print.
  • Load the Class Sibling (Sibling.class file) in memory to use it as a blue print.
  • Load the class Object (Object.class file) in memory to use it as a blue print.
  • Create instance variable using Object blue print.
  • Create additional instance variable using blue print Sibling.
  • Create additional instance variable using blue print SiblingExt.
  • Associate Object constructors with the created object.
  • Associate Sibling constructors with the created object
  • Associate SiblingExt constructors with the created object
  • Associate Object methods with the created object
  • Associate Object methods with the created object
  • Associate Object methods with the created object
  • Call the SiblingExt constructor
  • The SiblingExt constructor will call the Sibling constructor.
  • The Sibling constructor will call the Object constructor.
  • One SiblingExt Object creation and initialization is completed.
  • Return the reference of the created object.

The SiblingExt object will contain instance variables and associated constructors and methods from all three blue prints (Object, Sibling, SiblingExt).

Chain Of Constructor Calls

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:

  • A call to a constructor results in a chain of constructor calls.
  • The sequence of constructor calls is from bottom (child) to top (parent).
  • The sequence of constructor call completion is from top (parent) to bottom (child).
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.