advanced object oriented concepts tonga institute of higher education
TRANSCRIPT
![Page 1: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/1.jpg)
Advanced Object Oriented Concepts
Tonga Institute of Higher Education
![Page 2: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/2.jpg)
Inheritance Similarities
“You look just like your father.”People inherit features from their parents:
Eye color Hair color Height
You are similar to your parents
![Page 3: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/3.jpg)
Inheritance in Visual Basic .Net Inheritance – When a class inherits
members from another class Objects are like people. They can have
children!Children can inherit variables from a parentChildren can inherit methods from a parent
Parent / Base Class
Child / Sub Class
![Page 4: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/4.jpg)
History of Inheritance
In the sixties, a programmers created a program to simulate traffic They used objects for their vehicles
Cars Trucks Vans
They noticed that all vehicles did the same things Turn left Turn right Brake Go
![Page 5: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/5.jpg)
Plan #1 - Van, Car and Truck Objects
Create one class for each vehicle Van Car Truck
VanTurnLeft()
TurnRight()
Brake()
Go()
CarTurnLeft()
TurnRight()
Brake()
Go()
TruckTurnLeft()
TurnRight()
Brake()
Go()
![Page 6: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/6.jpg)
Plan #1 - Advantages
It’s quick and easy to understand
VanTurnLeft()
TurnRight()
Brake()
Go()
CarTurnLeft()
TurnRight()
Brake()
Go()
TruckTurnLeft()
TurnRight()
Brake()
Go()
![Page 7: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/7.jpg)
Plan #1 - Disadvantages
Code is repeated in each object Changing the code in Brake() requires 3 changes to 3
different objects
Method names can be changed. After a while, the objects are not similar
VanTurnLeft() -> Left()
TurnRight() -> Right()
Brake()
Go()
CarTurnLeft()
TurnRight()
Brake()
Go() -> Move()
TruckTurnLeft()
TurnRight()
Brake()
Go() -> Start()
![Page 8: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/8.jpg)
Plan #2 - Inheritance Make one object with common methods. The code in the parent object is used in the child
objects. VehicleTurnLeft()
TurnRight()
Brake()
Go()
CarTurnLeft()TurnRight()Brake()Go()
TruckTurnLeft()TurnRight()Brake()Go()
Methods in theparent comedown to thechildren!
![Page 9: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/9.jpg)
Plan #2 – Advantages A change in the method code in the parent automatically changes the children
classes Method code is consistent and easy to maintain
A change in the method name in the parent automatically changes the children. Names are consistent and easy to maintain
We can change a class that someone else created It is difficult to write your own button class. But we can add changes to the button class
using inheritance
VehicleTurnLeft() -> Left()
TurnRight() -> Right()
Brake()
Go()
CarTurnLeft() -> Left()TurnRight() -> Right()Brake()Go()
TruckTurnLeft() -> Left()TurnRight() -> Right()Brake()Go()
![Page 10: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/10.jpg)
Plan #2 – Disadvantages Inheritance requires special code Inheritance requires more understanding
VehicleTurnLeft()
TurnRight()
Brake()
Go()
CarTurnLeft()TurnRight()Brake()Go()
TruckTurnLeft()TurnRight()Brake()Go()
![Page 11: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/11.jpg)
Inheritance Differences
“You look just like your father.”People inherit features from their parents:
Eye color Hair color Height
You are similar to your parents. “But you are much taller”
You are also different from them.
![Page 12: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/12.jpg)
Inheritance Differences Each child object can have additional different
members.VehicleTurnLeft()
TurnRight()
Brake()
Go()
CarTurnLeft()TurnRight()Brake()Go()ConservePetrol()
TruckTurnLeft()TurnRight()Brake()Go()CarryLoad()
![Page 13: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/13.jpg)
Example of Parent / Base Class
All non-private members are shared with child classes
![Page 14: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/14.jpg)
Example of Child / Sub Class
Specialkeywordthat bringsmembersfromparent
Child specific classes
![Page 15: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/15.jpg)
Example of Driver for Inheritance
Inherited classes work exactly like a normal class
There is no difference when using an inherited method and a non-inherited method
![Page 16: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/16.jpg)
Demonstration
Inheritance Code
![Page 17: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/17.jpg)
Inheritance in VB.Net Classes
Look in the class definitions.
Almost every class inherits from another class!
![Page 18: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/18.jpg)
Demonstration
Inheritance in VB .Net Classes
![Page 19: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/19.jpg)
Access Specifiers
Public Can be used by
everything
Private Can only be used
by code inside the same class
Dim Same as Private
Friend Can be used by
code inside the same project
Dim FirstName as String
Access SpecifierName
Type
Protected Can be used by code that inherits
from this class Protected Friend
Combination of Protected and Friend
![Page 20: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/20.jpg)
Demonstration
Access Specifiers
![Page 21: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/21.jpg)
Multi-Level Inheritance
When a subclass is also a base class.
VehicleTurnLeft()
TurnRight()
Brake()
Go()
CarTurnLeft()TurnRight()Brake()Go()ConservePetrol()
RentalCarTurnLeft()TurnRight()Brake()Go()ConservePetrol()RentalPrice()
![Page 22: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/22.jpg)
Demonstration
Multi-Level Inheritance
![Page 23: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/23.jpg)
Overriding Base Methods Overriding – When a child
class replaces the behavior of a method defined in a base class.
To override (replace) a method: Define a method in the
parent class to be overridable
Define a method in the child class to override
Parent Class
Child Class
![Page 24: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/24.jpg)
Demonstration
Overriding Base Methods
![Page 25: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/25.jpg)
MyBase
Use in a child class
Use the MyBase keyword to call methods in a base class.
![Page 26: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/26.jpg)
Demonstration
MyBase
![Page 27: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/27.jpg)
MyClass and Me 1 If inheritance is not
used, they work the same way.
![Page 28: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/28.jpg)
MyClass and Me 2 If inheritance is used, use
these in parent classes Use the MyClass
keyword to call methods in the class this keyword is used.
Use the Me keyword to call methods in the current instance.
![Page 29: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/29.jpg)
Demonstration
MyClass and Me
![Page 30: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/30.jpg)
Abstract Classes and Methods Abstract class – A class that must be inherited from
An abstract class cannot be instantiated MustInherit – Used to make a class abstract
Abstract method – A method that must be overridden Method code does not exist in the base class because it will be
overridden MustOverride – Used to make a method abstract
![Page 31: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/31.jpg)
Demonstration
Abstract Class or Method
![Page 32: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/32.jpg)
Preventing Inheritance
NotInheritable – Used to make a class uninheritable.
![Page 33: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/33.jpg)
Demonstration
Preventing Inheritance
![Page 34: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/34.jpg)
Introduction to Interfaces 1
All DVD players have the same controls, even if they are made by different companies (Sony, Panasonic, Toshiba, etc.)
The buttons are a contract for how to operate the DVD player
An interface is a contract
detailing how an object is used Using this interface, I know how to
use the DVDPlayer Object
DVD PlayerPlay()Pause()Stop()Rewind()FastForward()
![Page 35: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/35.jpg)
Introduction to Interfaces 2
All DVD players have the same controls, even if they are made by different companies (Sony, Panasonic, Toshiba, etc.)
But these different companies are not related to each other
This is the difference between interfaces and inheritance
![Page 36: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/36.jpg)
Defining an Interface Defining an interface tells the computer that any class
that uses this interface is guaranteed to have the elements described in the interface
Example: Any class that implements the ISummary will have these functions and properties GetShortSummary GetFullSummary HasFullSummary
![Page 37: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/37.jpg)
Demonstration
Defining an Interface
![Page 38: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/38.jpg)
Implementing an Interface
![Page 39: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/39.jpg)
Demonstration
Implementing an Interface
![Page 40: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/40.jpg)
Primary/Native Interfaces vs. Secondary Interfaces
Primary/Native Interface of a class is composed of all the members defined in a class
Secondary Interfaces involves the implementation of other interfaces
SecondaryInterface
PrimaryInterface
![Page 41: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/41.jpg)
Abstract Classes vs. Interfaces
It is sometimes difficult to tell the difference between an abstract class and an interfaceAn abstract class is a class that cannot be
instantiated, but must be inherited fromAn interface, by contrast, is a set of members
that defines a contract for conduct
![Page 42: Advanced Object Oriented Concepts Tonga Institute of Higher Education](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d0b5503460f949ded17/html5/thumbnails/42.jpg)
Abstract Classes vs. Interfaces 2
It is sometimes also difficult to decide whether to use an abstract class or an interface A consideration is that a class may implement more
than one interface. A class may only inherit from only 1 class.
Abstract classes are best for objects that are closely related. Interfaces are best for providing common functionality to unrelated classes.
Use an abstract class to provide members that use the same code among many objects. Interfaces only force classes to have the same member names.