August 1999
JP219 : JAVA PROGRAMMING

QUESTION 5

Total Marks: 20 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 5

(a) Explain what is meant by the term single inheritance. [1]
Single inheritance is when a class can extend only one other class.

 

(b) What mechanism does Java provide to allow multiple inheritance. [1]
Interfaces

 

(c) Identify:

(i) The components of a parent class which are inherited in the sub class.

(ii) The components of a parent class which are not inherited in the base class.

 

[2]

[1]

(i)
  • The parent class methods;
  • The parent class member variables;

 

(ii)
The constructor function.

 

(d) (i) Variables can be polymorphic. Explain what is meant by the term polymorphism.

(ii) Explain how polymorphism contrasts to overloading.

[1]

[2]

(i)
A variable can refer to the objects of different forms (types)

 

  • (ii)
    Polymorphism is a run time issue;
  • Whilst overloading is a complete time issue.

 

(e) (i) Define a class Animal, which has the private attributes birthday of type Date, and number_legs, of type int. Include in this class a constructor which takes values that is used to initialize the members.

(ii) Derive a subclass of your Animal class, called Dog, which has a public name of type String. Include a constructor, which should have parameters for the members in this class and base class, and initialize the members of this class and those of the base class accordingly.

(iii) Consider the following Chihuahua, which is a special type of Dog, but also has characteristics associated with the class Rat:

public class Chihuahua extends Dog implements Rat {
     private num_ribbons;

     public Chihuahua(int R, String N, Date D, int l) {
          super(N, D, l);
   }

     public Boolean Is_Diseased () {
          return diseased;
   }

}

Give a definition of the interface class Rat, which has the interface function Is_Diseased() and the member diseased, neither of which should be allowed to be overloaded by any class which derives from Rat.

 

[4]

 

[4]

 

 

 

 

 

 

 

 

 

[4]

(i) A sample definition of Animal follows:

public class Animal {
    private Date birthday;
    private int number_legs;

    public Animal(Date D, int legs) {
        birthday= Date;
        number_legs= legs;
    }
}

 

(ii) A sample definition of dog follows:

public class Dog  extends Animal {
    String name;

    public Dog(String N, Date D, int l) {
        super(D, l);
        name= N;
    }
}

 

(iii) A sample definition of Rat follows:

public interface Rat {
    public final Boolean diseased;
    public final Boolean Is_Diseased ( );
}