An exercise in inheritance/abstract classes/polymorphism

  1. Service class A acts as superclass to class B. A is an abstract class because it has the abstract method m. The method m returns a String and takes as argument an int data type. Remember thatthis method is abstract and therefore it has no code implementing it now.

Class A has one private attribute: int x and another public attribute: double y.

Default constructor initializes the attributes to 10, and 100.00 respectively.

Non default constructor takes as argument the two attributes.

Write the accessor/mutator and toStrinhg methods of the class.

  1. Service class B is a subclass of A. It has one public attribute: Boolean z.

The default constructor calls the super class’ constructor and also sets the value of z to false.

The non default constructor takes 3 arguments (the first two for the super class). It also calls the super class ‘constructor and also initializes the variable z.

Write the code for the accessor/mutator methods of Bas well as the toString method which also utilizes the toString method of A.

The method m is implemented so that :

If the value of z is true and the argument is 20: It returns the String “This is the B version with True and the argument of m is 20”

Else it returns the String “This is the B version with False and the argument of m is not equal to 20”

  1. Create a class C that is also a subclass of A. It has only one public attribute double s.

Repeat the constructors, accessor, mutator toString methods followingteh same instructions as for B. The value of s is initialized to 200.00 by the default constructor.

Implement the method m this time with the code:

If the value of of the argument is =30 :

It calculates the sumof x+s (where x is the private attribute of A) and returns the String “the value of x+y+s =”+calculate the sum of the 3 attributes; be careful because x is private so it has to be accessed with getX() while the other identifiers can be used directly.

Else the String “No sum is calculated” is returned.

  1. Write a client class for the above service classes called Client
  • In order to use polymorphism to call the various implementations of method m do the following” i.e A a

Create a reference of A (just a declaration with initial value of null)

Create an object ob B i.e B b=new B(50, 300.00, true);

Let a=b;

Now use reference a to invoke the method m with argument 20 (the version of B class will be called). Capture the returned String and display it.

Repeat for C. Create an object of C: C c=new C(100, 500.0, 1000.0);

Let a=c;

Now use a to invoke m again with argument 30.

This time the C’s version of m will be called. The above technique of using a reference (nor an object) of the super class to invoke the abstract method is called polymorphism. Capture the returned String and display it.

Compile and run the program.

------Java Interpreter ------

This is the B version with true and argument for m=20

This value of x+y+s= 1600.0

Output completed (0 sec consumed) - Normal Termination