SlideShare ist ein Scribd-Unternehmen logo
1 von 21
+
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.

Weitere ähnliche Inhalte

Was ist angesagt?

Insertion into linked lists
Insertion into linked lists Insertion into linked lists
Insertion into linked lists MrDavinderSingh
 
Stack application
Stack applicationStack application
Stack applicationStudent
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked listsAbdullah Al-hazmy
 
Polish Notation In Data Structure
Polish Notation In Data StructurePolish Notation In Data Structure
Polish Notation In Data StructureMeghaj Mallick
 
08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMS08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMSkoolkampus
 
Java string handling
Java string handlingJava string handling
Java string handlingSalman Khan
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introductionSmriti Jain
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptxvishal choudhary
 
Multi catch statement
Multi catch statementMulti catch statement
Multi catch statementmyrajendra
 

Was ist angesagt? (20)

Data structures using c
Data structures using cData structures using c
Data structures using c
 
Insertion into linked lists
Insertion into linked lists Insertion into linked lists
Insertion into linked lists
 
Vb.net (loop structure)
Vb.net (loop structure)Vb.net (loop structure)
Vb.net (loop structure)
 
Stack application
Stack applicationStack application
Stack application
 
Circular Queue data structure
Circular Queue data structureCircular Queue data structure
Circular Queue data structure
 
Stack project
Stack projectStack project
Stack project
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Polish Notation In Data Structure
Polish Notation In Data StructurePolish Notation In Data Structure
Polish Notation In Data Structure
 
08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMS08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMS
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Hashing
HashingHashing
Hashing
 
Array Presentation
Array PresentationArray Presentation
Array Presentation
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
Stack
StackStack
Stack
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptx
 
Multi catch statement
Multi catch statementMulti catch statement
Multi catch statement
 

Andere mochten auch

Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interactionMichael Heron
 
CPP18 - String Parsing
CPP18 - String ParsingCPP18 - String Parsing
CPP18 - String ParsingMichael Heron
 
Web Services: Encapsulation, Reusability, and Simplicity
Web Services: Encapsulation, Reusability, and SimplicityWeb Services: Encapsulation, Reusability, and Simplicity
Web Services: Encapsulation, Reusability, and Simplicityhannonhill
 
2CPP01 - Intro to Module
2CPP01 - Intro to Module2CPP01 - Intro to Module
2CPP01 - Intro to ModuleMichael Heron
 
2CPP07 - Inheritance
2CPP07 - Inheritance2CPP07 - Inheritance
2CPP07 - InheritanceMichael Heron
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator OverloadingMichael Heron
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility SupportMichael Heron
 
2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented Program2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented ProgramMichael Heron
 
2CPP03 - Object Orientation Fundamentals
2CPP03 - Object Orientation Fundamentals2CPP03 - Object Orientation Fundamentals
2CPP03 - Object Orientation FundamentalsMichael Heron
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method OverloadingMichael Heron
 
2CPP10 - Polymorphism
2CPP10 - Polymorphism2CPP10 - Polymorphism
2CPP10 - PolymorphismMichael Heron
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and AutershipMichael Heron
 

Andere mochten auch (20)

ofdm
ofdmofdm
ofdm
 
Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interaction
 
CPP18 - String Parsing
CPP18 - String ParsingCPP18 - String Parsing
CPP18 - String Parsing
 
Web Services: Encapsulation, Reusability, and Simplicity
Web Services: Encapsulation, Reusability, and SimplicityWeb Services: Encapsulation, Reusability, and Simplicity
Web Services: Encapsulation, Reusability, and Simplicity
 
2CPP01 - Intro to Module
2CPP01 - Intro to Module2CPP01 - Intro to Module
2CPP01 - Intro to Module
 
2CPP18 - Modifiers
2CPP18 - Modifiers2CPP18 - Modifiers
2CPP18 - Modifiers
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
 
2CPP07 - Inheritance
2CPP07 - Inheritance2CPP07 - Inheritance
2CPP07 - Inheritance
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator Overloading
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility Support
 
2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented Program2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented Program
 
2CPP03 - Object Orientation Fundamentals
2CPP03 - Object Orientation Fundamentals2CPP03 - Object Orientation Fundamentals
2CPP03 - Object Orientation Fundamentals
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method Overloading
 
CPP15 - Inheritance
CPP15 - InheritanceCPP15 - Inheritance
CPP15 - Inheritance
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 
2CPP10 - Polymorphism
2CPP10 - Polymorphism2CPP10 - Polymorphism
2CPP10 - Polymorphism
 
CPP17 - File IO
CPP17 - File IOCPP17 - File IO
CPP17 - File IO
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and Autership
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
 
Chapter1 Introduction to OOP (Java)
Chapter1 Introduction to OOP (Java)Chapter1 Introduction to OOP (Java)
Chapter1 Introduction to OOP (Java)
 

Ähnlich wie SAD04 - Inheritance

Inheritance in OOPs with java
Inheritance in OOPs with javaInheritance in OOPs with java
Inheritance in OOPs with javaAAKANKSHA JAIN
 
SAD02 - Object Orientation
SAD02 - Object OrientationSAD02 - Object Orientation
SAD02 - Object OrientationMichael Heron
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS ConceptRicha Gupta
 
SAD05 - Encapsulation
SAD05 - EncapsulationSAD05 - Encapsulation
SAD05 - EncapsulationMichael Heron
 
Java OOPs Concepts.docx
Java OOPs Concepts.docxJava OOPs Concepts.docx
Java OOPs Concepts.docxFredWauyo
 
Inheritance and Substitution
Inheritance and SubstitutionInheritance and Substitution
Inheritance and Substitutionadil raja
 
oopsinvb-191021101327.pdf
oopsinvb-191021101327.pdfoopsinvb-191021101327.pdf
oopsinvb-191021101327.pdfJP Chicano
 
Object And Oriented Programing ( Oop ) Languages
Object And Oriented Programing ( Oop ) LanguagesObject And Oriented Programing ( Oop ) Languages
Object And Oriented Programing ( Oop ) LanguagesJessica Deakin
 
Inheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptxInheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptxnaeemcse
 
Oops And C++ Fundamentals
Oops And C++ FundamentalsOops And C++ Fundamentals
Oops And C++ FundamentalsSubhasis Nayak
 
Evolve Your Code
Evolve Your CodeEvolve Your Code
Evolve Your CodeRookieOne
 
Summer Training Project On C++
Summer Training Project On  C++Summer Training Project On  C++
Summer Training Project On C++KAUSHAL KUMAR JHA
 

Ähnlich wie SAD04 - Inheritance (20)

Inheritance in OOPs with java
Inheritance in OOPs with javaInheritance in OOPs with java
Inheritance in OOPs with java
 
SAD02 - Object Orientation
SAD02 - Object OrientationSAD02 - Object Orientation
SAD02 - Object Orientation
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
SAD05 - Encapsulation
SAD05 - EncapsulationSAD05 - Encapsulation
SAD05 - Encapsulation
 
Java OOPs Concepts.docx
Java OOPs Concepts.docxJava OOPs Concepts.docx
Java OOPs Concepts.docx
 
Inheritance and Substitution
Inheritance and SubstitutionInheritance and Substitution
Inheritance and Substitution
 
Python-Inheritance.pptx
Python-Inheritance.pptxPython-Inheritance.pptx
Python-Inheritance.pptx
 
Java_notes.ppt
Java_notes.pptJava_notes.ppt
Java_notes.ppt
 
oopsinvb-191021101327.pdf
oopsinvb-191021101327.pdfoopsinvb-191021101327.pdf
oopsinvb-191021101327.pdf
 
Oops in vb
Oops in vbOops in vb
Oops in vb
 
Object And Oriented Programing ( Oop ) Languages
Object And Oriented Programing ( Oop ) LanguagesObject And Oriented Programing ( Oop ) Languages
Object And Oriented Programing ( Oop ) Languages
 
Inheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptxInheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptx
 
C#
C#C#
C#
 
Oops And C++ Fundamentals
Oops And C++ FundamentalsOops And C++ Fundamentals
Oops And C++ Fundamentals
 
Advance oops concepts
Advance oops conceptsAdvance oops concepts
Advance oops concepts
 
Evolve Your Code
Evolve Your CodeEvolve Your Code
Evolve Your Code
 
Research paper
Research paperResearch paper
Research paper
 
Oop
OopOop
Oop
 
Viva file
Viva fileViva file
Viva file
 
Summer Training Project On C++
Summer Training Project On  C++Summer Training Project On  C++
Summer Training Project On C++
 

Mehr von Michael Heron

Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMichael Heron
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconductMichael Heron
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkMichael Heron
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityMichael Heron
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - TexturesMichael Heron
 
GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)Michael Heron
 
GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)Michael Heron
 
GRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationGRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationMichael Heron
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsMichael Heron
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsMichael Heron
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationMichael Heron
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - AbstractionMichael Heron
 
2CPP12 - Method Overriding
2CPP12 - Method Overriding2CPP12 - Method Overriding
2CPP12 - Method OverridingMichael Heron
 
2CPP09 - Encapsulation
2CPP09 - Encapsulation2CPP09 - Encapsulation
2CPP09 - EncapsulationMichael Heron
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and OverridingMichael Heron
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and PointersMichael Heron
 
2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and ClassesMichael Heron
 

Mehr von Michael Heron (18)

Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game Accessibility
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconduct
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS Framework
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and Radiosity
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - Textures
 
GRPHICS06 - Shading
GRPHICS06 - ShadingGRPHICS06 - Shading
GRPHICS06 - Shading
 
GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)
 
GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)
 
GRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationGRPHICS03 - Graphical Representation
GRPHICS03 - Graphical Representation
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D Graphics
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D Graphics
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art Appreciation
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 
2CPP12 - Method Overriding
2CPP12 - Method Overriding2CPP12 - Method Overriding
2CPP12 - Method Overriding
 
2CPP09 - Encapsulation
2CPP09 - Encapsulation2CPP09 - Encapsulation
2CPP09 - Encapsulation
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers
 
2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and Classes
 

Kürzlich hochgeladen

SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineeringssuserb3a23b
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 

Kürzlich hochgeladen (20)

SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Odoo Development Company in India | Devintelle Consulting Service
Odoo Development Company in India | Devintelle Consulting ServiceOdoo Development Company in India | Devintelle Consulting Service
Odoo Development Company in India | Devintelle Consulting Service
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineering
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 

SAD04 - Inheritance

  • 1. + Object Inheritance Systems Analysis and Design Michael Heron
  • 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. + 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 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. + 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. + Inheritance in the Natural World
  • 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. + 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. + 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 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. + 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  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. + 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. + 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 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. + 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. + 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 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. + 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.