Systems Analysis and Design
Last week we talked about how we use natural language
analysis to end up with the bare bones of a potential class
We missed out some parts of it because we haven’t yet covered the
In this lecture we’re going to look at the relationship between
These allow us to arrange classes in terms of how they
Has-A and Is-A
In our natural language analysis, the key phrases we look for
are variations of ‘has a’ and ‘is a’
Has a represents a composition or aggregation
Is a represents an inherited relationship.
A car is a vehicle.
A case has an engine.
We do this as one of our later passes over the document.
That way we can ignore relationships to classes that have already
One of the primary ways in which object orientation permits
effective structuring of code is through inheritance.
Different languages permit different kinds of inheritance.
Java and the .NET languages offer single inheritance.
Each class can inherit from only one parent.
C++ offers multiple inheritance.
Classes can inherit from more than one parent.
Multiple inheritance is more powerful.
But also very difficult to do right.
Single inheritance is more limited.
But design patterns (more on that later) let us deal with those
The concept of inheritance is borrowed from the natural world.
You get traits from your parents and you pass them on to your
In OO programming, ‘traits’ are behaviours and attributes.
In most OO languages any class can be the parent of any other
The child class gains all of the methods and attributes of the parent.
This can be modified, more on that in a later lecture.
Through this basic mechanism, code can be made available
through an OO program.
Inheritance is a powerful concept for several reasons.
Ensures consistency of code across multiple different objects.
Allows for code to be collated in one location with the concomitant
impact on maintainability.
Changes in the code rattle through all objects making use of it.
Supports for code re-use.
Difficult to get around the ‘Not Invented Here’ syndrome.
It is part of the static design of a system.
It’s decided at design time how the different parts of the system can
This can’t be changed at runtime, but design patterns permit us to work
In most OO languages the most general case of a code system
belongs at the highest place it is shared in the inheritance
You will have seen this in player and npc and living from the
It is successively specialised into new and more precise
Children specialise their parents
Parents generalise their children.
Functionality and attributes belong to the most general class in
which they are cohesive.
In .NET, the concept is simple.
You create an inheritance relationship by having one class extend
The process of inheriting from a class is often called extension as a
The newly extended class gains all of the attributes and
methods of the parent.
It can be used, wholesale, in place of the parent if needed.
We can also add and specialise attributes and behaviours.
This is the important feature of inheritance.
Inheritance in VB .NET
private balance as Integer
property Balance() as Integer
Set (ByValue Value as Integer)
balance = Value
public Function doubleBalance()
balance = balance * 2;
Inheritance in Java
private overdraft as Integer
Function adjustBalance (byVal val as Integer) as Boolean
if Me.Balance – val < 0 – overdraft then
Me.Balance = (getBalance() - val);
Constructors And Inheritance
When a specialized class is instantiated, it calls the
constructor on the specialized class.
The constructor is a special method that handles the initial setup of
If it doesn’t find a valid one it will error, even if one exists in the
Constructors can propagate invocations up the object hierarchy
through the use of the MyBase keyword.
MyBase always refers to the parent object in VB .NET.
Easy to do, because each child has only one parent.
In a constructor, must always be the first method call.
Everywhere else, it can be anywhere.
In VB .NET, a constructor is indicated by a subroutine called
Public sub New()
Me.Balance = 100
Public sub New (ByVal value as initialBalance)
Me.Balance = initialBalance
We can overload these methods too.
And all methods in Visual Basic .NET.
The biggest distinction between C++ and .NET inheritance
models is that C++ permits multiple inheritance.
Java, VB .NET and C# do not provide this.
It must be used extremely carefully.
If you are unsure what you are doing, it is tremendously easy to
make a huge mess of a program.
It is in fact something best avoided.
It’s not something you usually need.
There are usually better and less fragile ways of accomplishing a
What are the problems with multiple inheritance?
Hugely increased program complexity
Problems with ambiguous function calls.
The Diamond Problem
Hardly ever really needed.
For simple applications, single inheritance suffices.
For more complicated situations, design patterns exist to
resolve all the requirements.
Some languages permit multiple inheritance but resolve some
of the technical issues.
These have relatively limited traction in the real world.
Specialisation and Extension
Inheritance by itself is not useful.
It gives us a version of what we already have.
It becomes useful because we then have three options with the
things we get from a parent class.
We keep them as they are.
We specialise them to change them slightly.
We add to what we already have.
In this way we can ensure that our new classes are
substantively different from the old classes.
And thus are a worthwhile addition to our system.
Specialisation means taking a behaviour and altering it.
We can override it completely
We can have it do something extra before passing it back to the
method in the parent.
Whenever we provide a method with the same name in a child
class, it is called overriding.
In VB .NET we must indicate our intention to override.
Other languages don’t require that.
When a method is overridden, the most specialised version of
the method gets called when we invoke it.
Having overriden a method, we decide what happens next.
We either ignore the code that we inherited.
Or we pass the invocation ‘back up the chain’
Each overridden method is responsible for deciding what is to
be done in that regard.
In Visual Basic .NET, we can refer to methods in a parent class
through the MyBase keyword.
We saw that in relation to constructors a little earlier.
More on this later.
For now, it’s okay for our class diagrams to know this can be done.
With extension, we add to the attributes and behaviours we got
from the parent.
We add new attributes
We add new methods
These let us give a wider range of functionality that we
otherwise would have.
And let us branch out what our classes are for.
This works just the same way as we saw in the VB code
We just stick the new methods and attributes in there.
In our class tutorial, we had a Living class which was the parent
of NPC and Player.
Both received the base functionality for representing an object which
is considered to be ‘living’ in the game.
However, both had unique elements to go with them.
Likely many of the methods that we inherited would end up
We’ll talk about that in the tutorial.
The inheritance allows us to share code rather than re-
This in turn reduces our future maintenance burden.
Inheritance is a powerful tool in object orientation.
One of the Great Trilogy of tools.
VB .NET permits single inheritance only.
This will limit us, but only until we learn how to work around it.
Inheritance is only the start of the process.
It must then be followed through with specialisation and