You are on page 1of 6

Inheritance

Q1)

class Body
{
private String name;
private double mass;
public Body(String n, double m)
{
//initialize the data members of the current object using the parameters.
}
public void display()
{
//Details of a body are displayed in the following format.
//Name: Body Name
//Mass: ... Kg
}
public double getMass()
{
//Mass of the current body is returned.
}
}

class MovingBody extends Body


{
private double velocity;
public MovingBody(String n, double m, double v)
{
//initialize the data members of the current object using the parameters.
//Note: super class constructor should be used to initialze inherited data members.
}
public double momentum()
{
//calculate and return the momentum of the current moving body.
// momentum of a moving body is equal to the product of its mass and velocity.
}
public void display() //method to display the details of a moving body.
{
//Details of a moving body are displayed in the following format.
//Name: Body Name
//Mass: ... Kg
//Velocity: ... m/s
//Momentum: ... Kg m/s
//Note: super class display() method should be used to display the name and mass of the body.
}
}

Q2:

abstract class Number


{
abstract public void display();
abstract public Number add(Number n);
public static void displaySum(Number n1, Number n2)
{
//Values of the given number and their sum are displayed as follows.
//First No. is: .......
//Second No. is: .......
//Their sum is: .......
}
}

class Rational extends Number


{
// data member to store the values of a rational number.
public Rational(int n, int d)
{
//initialize the current Rational object using the given parameters.
}
public void display()
{
//display the value of a Rational object in p/q format.
}
public Number add(Number n)
{
//Add the current and parameter Rational objects and return the result as a Rational object.
}
}

class Complex extends Number


{
// data member to store the values of a complex number.
public Complex(int r, int i)
{
//initialize the current Complex object using the given parameters.
}
public void display()
{
//display the value of a Complex object in a+bi format.
}
public Number add(Number n)
{
//Add the current and parameter Complex objects and return the result as a Complex object.
}
}

Q3:

abstract class Cargo


{
private String to, from;
private double weight;
public Cargo(String to, String from, int weight)
{
//initializer statements
}
public void display()
{
//Details of a Cargo are displayed in the following format
//Source: ..........
//Destination: ..........
//Weight: ..........
}
abstract public double getRate();
}

class Normal extends Cargo


{
public Normal(String to, String from, int weight)
{
//initializer statements
}
public double getRate()
{
//freight rate per kg of the cargo weight is returned.
}

public void display()


{
//Details of a Cargo are displayed in the following format
//Source: ..........
//Destination: ..........
//Weight: ..........
//Cargo Type: .............. //Name of the sub classes is the cargo type.
//Freight Charge: ......... //Calculated using the rate defined by the sub classes.
}
}

class Express extends Cargo


{
public Express(String to, String from, int weight)
{
//initializer statements
}
public double getRate()
{
//freight rate per kg of the cargo weight is returned.
}

public void display()


{
//Details of a Cargo are displayed in the following format
//Source: ..........
//Destination: ..........
//Weight: ..........
//Cargo Type: .............. //Name of the sub classes is the cargo type.
//Freight Charge: ......... //Calculated using the rate defined by the sub classes.
}
}
Q4:

Q5:
Q6:

You might also like