Inheritance Flashcards

1
Q

Inheritance: Motivation

A
  • 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…
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Inheritance: Definition

A
  • 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…
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Inheritance: Terminology

A

 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Inheritance in Java

A

 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Inheritance in Java 2

A

 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…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Inheritance in Java 3

A

 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Inheritance in Java 4

A

 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Inheritance in Java 5

A

 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Inheritance Case Study

A

 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?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Inheritance Hierarchy

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Inheritance Hierarchy 2

A

 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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

super in Java

A
  • 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()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

super in Java

A
  • 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()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Constructor Chaining example

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Method Overriding

A

 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:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Method Overloading

A

 Not to be confused with Method Overriding!
- Java methods are uniquely identified by their…
- Class…
- Name…
- … and parameter list

 This means we can have more than one method with the same, in the
same class… as long as they have different parameter lists
- The name and parameter list is often referred to as a method’s signature.
- Defining multiple methods with the same name in the same class is known as method overloading.
- All flavours of the overloaded methods typically achieve the same goal…
- n.b. method overloading can be applied to constructors!