Showing posts with label hl7. Show all posts
Showing posts with label hl7. Show all posts

Tuesday, 17 January 2012

Inheritance



Just as children can inherit characteristics from their parents, subclasses can be created that inherit from superclasses.  The diagram above would need 4 classes (3 sub classes and 1 super class).  The 3 different types of bike would all have the same variables and methods as in the superclass and would also have variables or methods which are unique to this subclass.  The methods and variables are only defined once in the super class.  They are not redefined in the subclass. 

The advantages of inheritance are:

-efficiency : don't need to redefine procedures each time we create a new type of bike

-reliability : increased program structure

-reusability : code can be reused more easily making the program smaller and allowing it to execute faster

TASK 1:

TASK 2:
Create van, and car subclasses of the superclass vehicle. 
All vehicles will have a colour, cost and make as well as set and get methods. 
Van's will have a payload variable. 
Car's will have a doors variable.  (3 or 5 doors?)

TASK 3:
Test your classes from task 2 with a program that asks a user to create 1 car and 1 van and then displays their details. 

TASK 4:
Now extend the car class to create subclasses familycar, sportscar and vintagecar. 
Vintagecars have a yearbuilt variable. 
Sportscars have a topspeed variable. 
Familycars must have 5 doors. 

Test your new classes. 

Extension:
Try saving and loading car details to a file.