+
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 potenti...
+
Has-A and Is-A
 In our natural language analysis, the key phrases we look for
are variations of ‘has a’ and ‘is a’
 Ha...
+
Inheritance
 One of the primary ways in which object orientation permits
effective structuring of code is through inher...
+
Inheritance
 The concept of inheritance is borrowed from the natural world.
 You get traits from your parents and you ...
+
Inheritance in the Natural World
+
Inheritance
 Inheritance is a powerful concept for several reasons.
 Ensures consistency of code across multiple diffe...
+
Inheritance
 In most OO languages the most general case of a code system
belongs at the highest place it is shared in t...
+
Inheritance
 In .NET, the concept is simple.
 You create an inheritance relationship by having one class extend
anothe...
+
Inheritance in VB .NET
Class BankAccount
private balance as Integer
property Balance() as Integer
Get
return balance
End...
+
Inheritance in Java
Class ExtendedBankAccount
inherits BankAccount
private overdraft as Integer
Function adjustBalance (...
+
Constructors And Inheritance
 When a specialized class is instantiated, it calls the
constructor on the specialized cla...
+
Constructors
 In VB .NET, a constructor is indicated by a subroutine called
New:
Public sub New()
Me.Balance = 100
End ...
+
Multiple Inheritance
 The biggest distinction between C++ and .NET inheritance
models is that C++ permits multiple inhe...
+
Multiple Inheritance
 What are the problems with multiple inheritance?
 Hugely increased program complexity
 Problems...
+
Specialisation and Extension
 Inheritance by itself is not useful.
 It gives us a version of what we already have.
 I...
+
Specialisation
 Specialisation means taking a behaviour and altering it.
 We can override it completely
 We can have ...
+
Specialisation
 Having overriden a method, we decide what happens next.
 We either ignore the code that we inherited.
...
+
Extension
 With extension, we add to the attributes and behaviours we got
from the parent.
 We add new attributes
 We...
+
Inheritance
 In our class tutorial, we had a Living class which was the parent
of NPC and Player.
 Both received the b...
+
Conclusion
 Inheritance is a powerful tool in object orientation.
 One of the Great Trilogy of tools.
 Encapsulation
...
Upcoming SlideShare
Loading in …5
×

SAD04 - Inheritance

354 views
287 views

Published on

This is a lecture on Systems Analysis and design. It focuses on inheritance.

Published in: Software, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
354
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

SAD04 - Inheritance

  1. 1. + Object Inheritance Systems Analysis and Design Michael Heron
  2. 2. + 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.
  3. 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. 4. + 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.
  5. 5. + 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.
  6. 6. + Inheritance in the Natural World
  7. 7. + 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.
  8. 8. + 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.
  9. 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. 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. 11. + 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
  12. 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. 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. 14. + 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.
  15. 15. + 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.
  16. 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. 17. + 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.
  18. 18. + 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.
  19. 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. 20. + 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.
  21. 21. + 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.

×