+
Object Inheritance
Systems Analysis and Design
Michael Heron
+
Introduction
 Last week we talked about how we use natural language
analysis to end up with the bare bones of a potential class
diagram.
 We missed out some parts of it because we haven’t yet covered the
fundamentals.
 In this lecture we’re going to look at the relationship between
specific classes.
 Inheritance
 Composition
 Aggregation
 These allow us to arrange classes in terms of how they
interact.
+
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
been discarded.
+
Inheritance
 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
limitations.
+
Inheritance
 The concept of inheritance is borrowed from the natural world.
 You get traits from your parents and you pass them on to your
offspring.
 In OO programming, ‘traits’ are behaviours and attributes.
 In most OO languages any class can be the parent of any other
object.
 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 in the Natural World
+
Inheritance
 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.
 Theoretically…
 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
interrelate.
 This can’t be changed at runtime, but design patterns permit us to work
around that.
+
Inheritance
 In most OO languages the most general case of a code system
belongs at the highest place it is shared in the inheritance
hierarchy.
 You will have seen this in player and npc and living from the
tutorial exercise.
 It is successively specialised into new and more precise
implementations.
 Children specialise their parents
 Parents generalise their children.
 Functionality and attributes belong to the most general class in
which they are cohesive.
+
Inheritance
 In .NET, the concept is simple.
 You create an inheritance relationship by having one class extend
another.
 The process of inheriting from a class is often called extension as a
result.
 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
Class BankAccount
private balance as Integer
property Balance() as Integer
Get
return balance
End Get
Set (ByValue Value as Integer)
balance = Value
End Set
End Property
public Function doubleBalance()
balance = balance * 2;
end Function
End Class
+
Inheritance in Java
Class ExtendedBankAccount
inherits BankAccount
private overdraft as Integer
Function adjustBalance (byVal val as Integer) as Boolean
if Me.Balance – val < 0 – overdraft then
return false
end if
Me.Balance = (getBalance() - val);
return true;
End Function
End Class
+
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
an object.
 If it doesn’t find a valid one it will error, even if one exists in the
parent.
 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.
+
Constructors
 In VB .NET, a constructor is indicated by a subroutine called
New:
Public sub New()
Me.Balance = 100
End Sub
Public sub New (ByVal value as initialBalance)
Me.Balance = initialBalance
End Sub
 We can overload these methods too.
 And all methods in Visual Basic .NET.
+
Multiple Inheritance
 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.
 Usually.
 It’s not something you usually need.
 There are usually better and less fragile ways of accomplishing a
goal.
+
Multiple Inheritance
 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
 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.
+
Specialisation
 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.
+
Extension
 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
example.
 We just stick the new methods and attributes in there.
+
Inheritance
 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
being overridden.
 We’ll talk about that in the tutorial.
 The inheritance allows us to share code rather than re-
implement it.
 This in turn reduces our future maintenance burden.
+
Conclusion
 Inheritance is a powerful tool in object orientation.
 One of the Great Trilogy of tools.
 Encapsulation
 Inheritance
 Polymorphism.
 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
extension.

SAD04 - Inheritance

  • 1.
    + Object Inheritance Systems Analysisand Design Michael Heron
  • 2.
    + Introduction  Last weekwe talked about how we use natural language analysis to end up with the bare bones of a potential class diagram.  We missed out some parts of it because we haven’t yet covered the fundamentals.  In this lecture we’re going to look at the relationship between specific classes.  Inheritance  Composition  Aggregation  These allow us to arrange classes in terms of how they interact.
  • 3.
    + 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 been discarded.
  • 4.
    + Inheritance  One ofthe 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 limitations.
  • 5.
    + Inheritance  The conceptof inheritance is borrowed from the natural world.  You get traits from your parents and you pass them on to your offspring.  In OO programming, ‘traits’ are behaviours and attributes.  In most OO languages any class can be the parent of any other object.  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.
  • 6.
    + Inheritance in theNatural World
  • 7.
    + Inheritance  Inheritance isa 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.  Theoretically…  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 interrelate.  This can’t be changed at runtime, but design patterns permit us to work around that.
  • 8.
    + Inheritance  In mostOO languages the most general case of a code system belongs at the highest place it is shared in the inheritance hierarchy.  You will have seen this in player and npc and living from the tutorial exercise.  It is successively specialised into new and more precise implementations.  Children specialise their parents  Parents generalise their children.  Functionality and attributes belong to the most general class in which they are cohesive.
  • 9.
    + Inheritance  In .NET,the concept is simple.  You create an inheritance relationship by having one class extend another.  The process of inheriting from a class is often called extension as a result.  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.
  • 10.
    + Inheritance in VB.NET Class BankAccount private balance as Integer property Balance() as Integer Get return balance End Get Set (ByValue Value as Integer) balance = Value End Set End Property public Function doubleBalance() balance = balance * 2; end Function End Class
  • 11.
    + Inheritance in Java ClassExtendedBankAccount inherits BankAccount private overdraft as Integer Function adjustBalance (byVal val as Integer) as Boolean if Me.Balance – val < 0 – overdraft then return false end if Me.Balance = (getBalance() - val); return true; End Function End Class
  • 12.
    + 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 an object.  If it doesn’t find a valid one it will error, even if one exists in the parent.  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.
  • 13.
    + Constructors  In VB.NET, a constructor is indicated by a subroutine called New: Public sub New() Me.Balance = 100 End Sub Public sub New (ByVal value as initialBalance) Me.Balance = initialBalance End Sub  We can overload these methods too.  And all methods in Visual Basic .NET.
  • 14.
    + Multiple Inheritance  Thebiggest 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.  Usually.  It’s not something you usually need.  There are usually better and less fragile ways of accomplishing a goal.
  • 15.
    + Multiple Inheritance  Whatare 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.
  • 16.
    + 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.
  • 17.
    + Specialisation  Specialisation meanstaking 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.
  • 18.
    + Specialisation  Having overridena 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.
  • 19.
    + Extension  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 example.  We just stick the new methods and attributes in there.
  • 20.
    + Inheritance  In ourclass 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 being overridden.  We’ll talk about that in the tutorial.  The inheritance allows us to share code rather than re- implement it.  This in turn reduces our future maintenance burden.
  • 21.
    + Conclusion  Inheritance isa powerful tool in object orientation.  One of the Great Trilogy of tools.  Encapsulation  Inheritance  Polymorphism.  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 extension.