Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

Assignments- Inheritance

1. Create an inheritance hierarchy of Rodent: Mouse, Gerbil,


Hamster, etc. In the base class, provide methods that are
common to all Rodents, and override these in the derived classes
to perform different behaviors depending on the specific type of
Rodent. Create an array of Rodent, fill it with different specific
types of Rodents, and call your base-class methods to see what
happens.

2. Modify Exercise 1 so that Rodent is an abstract class. Make the


methods of Rodent abstract whenever possible.

3. Modify Exercise 1 so that it demonstrates the order of


initialization of the base classes and derived classes. Now add
member objects to both the base and derived classes, and show the
order in which their initialization occurs during construction..

4. Create a class as abstract without including any abstract


methods, and verify that you cannot create any instances of that
class.

5. Create a base class with an abstract print( ) method that is


overridden in a derived class. The overridden version of the
method prints the value of an int variable defined in the derived
class. At the point of definition of this variable, give it a nonzero
value. In the base-class constructor, call this method. In main( ),
create an object of the derived type, and then call its print( )
method. Explain the results.

6. Add a new method in the base class of Shapes.java that prints a


message, but don’t override it in the derived classes. Explain what
happens. Now override it in one of the derived classes but not the
others, and see what happens. Finally, override it in all the derived
classes.

7. Add a new type of Shape to Shapes.java and verify in main( )


that polymorphism works for your new type as it does in the old
types

Concept: Packages, Inheritance and Polymorphism


Objective: At the end of the assignments, participants will be able to
 Create abstract classes
 Create a new class by extending a class
 Write code to exhibit polymorphic behavior of a method call.

1) Create an abstract class Compartment to represent a rail coach. Provide a abstract function
notice in this class. Derive FirstClass, Ladies, General, Luggage classes from the compartment
class. Override the notice function in each of them to print notice suitable to the type of the
compartment.

All these classes should be created in a separate package named javass.intro3.prg1

Create a class TestCompartment in a different directory. Write main function to do the following:
 Declare an array of Compartment pointers of size 10.
 Create a compartment of a type as decided by a randomly generated integer in the range 1 to
4.
 Check the polymorphic behavior of the notice method.

2) Create an class Medicine to represent a drug manufactured by a pharmaceutical company.


Provide a function label in this class to print Name and address of the company. Derive Tablet,
Syrup and Ointment classes from the Medicine class. Override the label function in each of these
classes to print additional information suitable to the type of medicine. For example, in case of
tablets, it could be “store in a cool dry place”, in case of ointment it could be “for external use
only” etc.

All these classes should be created in a separate package named javass.intro3.prg2.

Create a class TestMedicine in a different directory. Write main function to do the following:
 Declare an array of Medicine references of size 10
 Create a medicine object of the type as decided by a randomly generated integer in the range
1 to 3.
 Check the polymorphic behavior of the label method.

You might also like