2. What is Object-Oriented Design?
It promotes thinking about software in a way that
models how we think about the real world
It organises program code into classes of objects
3. What is a Class?
A class is a collection of things (objects) with
similar attributes and behaviours.
Attributes:
What is looks like
Behaviours:
What it does
4. Classes and Objects
Class Object
A class is a template or An object is a running
blueprint that defines an instance of a class that
object’s attributes and consumes memory and
operations and created at has a finite lifespan
design time
5. What is an Object?
Every object is an instance of a class
Every object has attributes and behaviours
Class: Dog
Object: Object: Object: Object:
Red Setter Labrador Terrier Bulldog
6. Class Examples
Dogs
Attributes: Four legs, a tail
Behaviours: Barking
Cars
Attributes: Four wheels, engine, 3 or 5 doors
Behaviours: Acceleration, braking, turning
7. In Code
The programming constructs of attributes
and behaviours are implemented as:
Attributes: Properties
Behaviours: Methods
8. Public Class Dog
Public Name As String
Public Sub Sleep()
MessageBox.Show(“ZZzz”)
End Sub
End Class
9. Class
Object
Dim GoldenRetriever as New Dog
GoldenRetriever.Name = “Rex”
GoldenRetriever.Sleep()
Property
Method
10. Another Example
Ferrari is an instance of the Car class
Attributes (Use properties or fields):
Red
Rear wheel drive
Max speed 330 km/h.
Behaviours (Methods):
Accelerate
Turn
Stop
12. Encapsulation
How an object performs its duties is hidden
from the outside world, simplifying client
development
Clients can call a method of an object without
understanding the inner workings or complexity
Any changes made to the inner workings are
hidden from clients
13. Example
Car Stereo
Standard case size and fittings, regardless of features
Can be upgraded without affecting rest of car
Functionality is wrapped in a self-contained manner
14. Encapsulation – In Practice
Declare internal details of a class as Private
to prevent them from being used outside
your class
This technique is called data hiding.
This is achieved by using property
procedures.
15. Abstraction
Abstraction is selective ignorance
Decide what is important and what is not
Focus on and depend on what is important
Ignore and do not depend on what is unimportant
Use encapsulation to enforce an abstraction
16. Inheritance
Inheritance specifies an “is-a-kind-of”
relationship
Multiple classes share the same attributes
and behaviours, allowing efficient code reuse
Base Class
Examples:
A customer “is a kind of” person Person
An employee “is a kind of” person
Derived classes Customer Employee
17. Inheritance
We can create new classes of objects by
inheriting attributes and behaviours from
existing classes and then extending them
We can build hierarchies (family trees) of classes
Person
Employee
Part Time Full Time
18. Inheritance Cont’d
The existing class is called the base class, and the new
class derived from the base class is called the derived
class.
The derived class inherits all the
properties, methods, and events of the base class and
can be customized with additional properties and
methods.
19. Inheritance
Example
Rally Car
Inherits properties of class Car …
… and extends class Car by adding a
rollcage, racing brakes, fire extinguisher, etc.
20. Polymorphism
The ability for objects from different classes to
respond appropriately to identical method names
or operators.
Allows you to use shared names, and the system
will apply the appropriate code for the particular
object.
Different code will execute depending on the
context!
22. Creating Classes in Code
Add a class to the project
Provide appropriate name for the class
Create constructors as needed
Create a destructor, if appropriate
Declare properties
Declare methods
23. Creating Classes in Code
Add a class to the project
Provide appropriate name for the class
Create constructors as needed
Create a destructor, if appropriate
Declare properties
Declare methods
25. Creating Classes in Code
Add a class to the project
Provide appropriate name for the class
Create constructors as needed
Create a destructor, if appropriate
Declare properties
Declare methods
28. Creating Classes in Code
Add a class to the project
Provide appropriate name for the class
Create constructors as needed
Create a destructor, if appropriate
Declare properties
Declare methods
29. 3. Create Constructors
Sub New replaces Class_Initialize
Executes code when object is instantiated
Public Sub New( )
'Perform simple initialization
Course = “BIS”
End Sub
Can overload, but does not use Overloads
keyword
Public Sub New(ByVal i As Integer) 'Overloaded without Overloads
'Perform more complex initialization
intValue = i
End Sub
30. Creating Classes in Code
Add a class to the project
Provide appropriate name for the class
Create constructors as needed
Create a destructor, if appropriate
Declare properties
Declare methods
31. 4. Create Destructor
Sub Finalize replaces Class_Terminate event
Use to clean up resources
Code executed when destroyed by garbage
collection
Important: destruction may not happen
immediately
Protected Overrides Sub Finalize( )
'Can close connections or other resources
conn.Close
End Sub
32. Creating Classes in Code
Add a class to the project
Provide appropriate name for the class
Create constructors as needed
Create a destructor, if appropriate
Declare properties
Declare methods
33. 5. Declare Properties
Specify accessibility of variables and
procedures
Keyword Definition
Public Accessible everywhere.
Private Accessible only within the type itself.
Friend Accessible within the type itself and all
namespaces and code within the same assembly.
Protected Only for use on class members. Accessible within
the class itself and any derived classes.
Protected The union of Protected and Friend.
Friend
34. 5. Cont’d
Properties represent a classes attributes
Student
First Name
Last Name
StudentID
Age
Course
35.
36. 5. Properties (Property Procedures)
To store values for a property you use the
SET property procedure
To retrieve values from a property you use
the GET property procedure
You must specify whether the value stored
in the property can obtained and changed
37.
38.
39.
40. If a procedure can only obtain a property it is
Read Only
If it can be obtained and changed it is Read-Write
41.
42.
43. Creating Classes in Code
Add a class to the project
Provide appropriate name for the class
Create constructors as needed
Create a destructor, if appropriate
Declare properties
Declare methods