Inheritance Flashcards
Inheritance: Motivation
- In OO languages, inheritance provides a mechanism to promote reuse and extensibility.
- Code written by one programmer and delivered as a class can then be extended by another to suit the needs of the task at hand.
- The original programmer need not know anything about the application space in order to support it. Nor need he release his source code to allow his/her classes to be specialized…
Inheritance: Definition
- Classes are simply a grouping of variables and methods..
- In OO languages one class can inherit from, be derived from, or extend another to create new class.
n.b.These are different words for the same thing, used by different standards, groups and languages. - When a class is extended, the instance variables and methods defined
in that class implicitly become part of the new class. - Those instance variables and methods can be accessed/invoked on
the new class even though they are not explicitly defined in that
class…
Inheritance: Terminology
Inheritance Terminology
- The class being extended is known as the base class, super class or the parent class.
- The new class is known as a subclass of the other.
- A class can have many subclasses.
- In Multiple Inheritance Languages, a class can have many super classes too (C++ for example)
- In Single Inheritance Languages, a class can have only one.
Inheritance in Java
Java is a single inheritance language
- A class can optionally define a single super class by using the extends keyword when defining the class.
- All methods and instance variables defined in the super class implicitly become part of the new subclass…
- Any accessible methods and instance variables can be directly accessed by methods in the new subclass
Inheritance in Java 2
Inheritance is transitive
- If a class is a subclass of another, it is also a subclass of that classes
parent class.
- e.g. in the example below class Lecturer is a subclass of class
Mammal
- Class Lecturer therefore inherits instance variables and methods
from both class Person and class Mammal…
Inheritance in Java 3
As inheritance is transitive and we can create many subclasses…
- We therefore have an Inheritance Hierarchy.
- All the inheritance relationships can be mapped out using a tree…
- …a bit like a family tree.
- The variables and methods your class has depends on your position in that tree.
Inheritance in Java 4
Note that as we move:
- up the tree we become more generalist and abstract.
- down the tree we become more specialist and domain-specific.
- This is equally true for any OO programs we write.
Inheritance in Java 5
Inheritance is the primary mechanism to provide extensibility in
OO languages.
- You can use it to specialise an existing class to suit your needs
- Other programmers can specialise your classes in the future…
- Deciding where to implement a method, or place an instance variable is therefore a very important decision!
- To promote reuse, typically implement at as high a level as makes sense
Inheritance Case Study
Wizard Chess: A concrete example
- Visually model the correct behaviour of chess pieces in Swing
- Prevent illegal moves on the board.
- What classes can we identify? What names are sensible? Is there an inheritance relationship between any of these classes?
Inheritance Hierarchy
- Are ChessBoard and ChessSquare written from scratch or also specializations?
- Our own classes can extend any existing Java class - even the ones you didn’t write
Inheritance Hierarchy 2
I defined the following as part of my design… Which class do you think
should they be defined in and why?
Instance Variables:
- The chess square a piece is residing on.
(ChessPiece)
- A two dimensional array of 64 chess squares.
(ChessBoard)
- Image representing a piece.
(ChessPiece)
- The [x,y] location of a chess square
(ChessSquare)
Methods:
- ActionListener()
(ChessBoard)
- moveTo()
(ChessPiece)
- canMoveTo()
(All ChessPiece subclasses)
super in Java
- In java, we use super() to call the constructor method in our parent.
- Typically that uses super() to call the constructor on its parent before
initialising its own (extended) variables… - …in fact, java insists on it.
- super() must be the first statement in your constructor.
- If you omit it, then the java compiler will put it in anyway!
- Put any necessary parameters to the constructor in the brackets – exactly the same as you would using new()
super in Java
- In java, we use super() to call the constructor method in our parent.
- Typically that uses super() to call the constructor on its parent before
initialising its own (extended) variables… - …in fact, java insists on it.
- super() must be the first statement in your constructor.
- If you omit it, then the java compiler will put it in anyway!
- Put any necessary parameters to the constructor in the brackets – exactly the same as you would using new()
Constructor Chaining example
Method Overriding
Inheritance allows us to specialize the behaviour of an existing class by
adding stuff.
- new instance variables, methods
- n.b. It’s not permissible to remove methods and instance variables. You can choose to not use them, but they always implicitly remain.
Method overriding can be used to change the behaviour by replacing a
method in the superclass.
- Implementation is simple.
- Define a method in your class with the same signature as one in a superclass
- The functionality in this method replaces the functionality defined in the superclass: