Inheritance and Abstraction
-
Define the term inheritance
- Inheritance is where a class uses the interface of another class (its attributes and methods), and optionally (re-)defines the behaviour of those methods. It may also add attributes or methods of its own.
-
Draw a diagram to show inheritance relationships between at least three things.
Computer / | \ Desktop Phone Laptop / \ Smartphone Flip-phone
-
Use the pseudocode below to answer the questions that follow:
class Guitar private noOfStrings = 6 public procedure holdFret() ... endprocedure public procedure strum() ... endprocedure endclass class ElectricGuitar inherits Guitar public procedure adjustVolume() ... endprocedure endclass
-
Identify the parent class and the child class.
- Parent:
Guitar
- Child:
ElectricGuitar
- Parent:
-
Identify the attributes and methods that
ElectricGuitar
inherits fromGuitar
noOfStrings
holdFret()
strum()
-
-
Describe what happens when a class calls a super method.
- When a class calls a 'super method', it calls the original method on its superclass (ignoring any overriden methods that the subclass may have), performing the original routine on the subclass object.
-
Explain the issue caused by allowing multiple inheritance.
- The method resolution order is not immediately clear to inexperienced programmers.
- Two classes with conflicting interfaces could be inherited from
-
Define the term abstract method, and explain when you might use one.
- An abstract method is one for which the signature, but no implementation, is defined. Classes containing abstract methods may not be instantiated.
- Abstract methods may be used when defining a base class that other classes will inherit from, but may not be instantiated, or when defining an interface for use by other classes