Worksheet 2.4 Object-Oriented Paradigm Inheritance
Worksheet 2.4 Object-Oriented Paradigm Inheritance
INHERITANCE
Worksheet 2.1
Unscramble each word from the list below and then use them to complete their
meaning.
1. Ecntahierin:
2. Sintiher:
3. Dfee:
4. Aly sgeg:
5. Inps wbes:
6. Haebvirou:
7. Poh:
8. Lishterh:
9. Poen-ededah wsorra:
Worksheet 2.2
Object-Oriented Paradigm: Inheritance
Inheritance makes it possible to create a new child class that inherits the
attributes and methods of the original (parent) class. The term 'parent class'
is also referred to as superclass or base class, while 'child classes' can also be
referred to as subclasses or derived classes.
Inheritance diagrams
Imagine that you are going to develop a game for children that allows them
to play the role of a zookeeper. The zoo will have many different animals, and
you could make a long list of every different animal type and develop a class
definition for each. However, you would soon notice that every class has a
set of attributes and methods in common. For example, all of the animals
would have a name and would be able to feed and move.
You could use a single animal class and add an animal-type attribute that
would specify the type of animal. But different types of animals will need
different methods (for example, birds need to be able to lay eggs and spiders
to spin webs).
Even where the animals have a method in common, such as move, the
implementation would need lots of 'if' statements to ensure the correct
movement behaviour (for example, kangaroos hop, whereas snakes slither).
Inheritance can solve this problem.
you can add extra attributes and methods and change the behaviour of any
inherited methods as required.
This high-level diagram shows us the names of the classes and the
relationship between them. You can see that a penguin is a non-flying bird
and that a snake is a reptile. It is important to note that this is not a biological
taxonomy; the classes and relationships are those relevant for the zoo game
Worksheet 2.3
WRAP-UP