OBJECT ORIENTED PROGRAMMING
THE BASICS
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
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
Classes and ObjectsClass Object A class is a
template or blueprint that defines an object’s attributes and operations and created at design time
An object is a running instance of a class that consumes memory and has a finite lifespan
What is an Object?
Class: Dog
Object:Red Setter
Object:Labrador
Object:Terrier
Object:Bulldog
Every object is an instance of a class Every object has attributes and
behaviours
Class Examples
Dogs
Attributes: Four legs, a tail Behaviours: Barking
Cars
Attributes: Four wheels, engine, 3 or 5 doors Behaviours: Acceleration, braking, turning
In Code
The programming constructs of attributes and behaviours are implemented as: Attributes: Properties Behaviours: Methods
Public Class DogPublic Name As String
Public Sub Sleep()MessageBox.Show(“ZZzz”)
End SubEnd Class
Dim GoldenRetriever as New DogGoldenRetriever.Name = “Rex”GoldenRetriever.Sleep()
Object Class
Property
Method
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
VB.NET & OOP
VB .NET is an object-oriented language.
VB.NET Supports:
Encapsulation Abstraction
Inheritance Polymorphism
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
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
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.
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
Inheritance
Inheritance specifies an “is-a-kind-of” relationship
Multiple classes share the same attributes and behaviours, allowing efficient code reuse
Examples: A customer “is a kind of” person An employee “is a kind of” person
Customer Employee
Person
Base Class
Derived classes
Inheritance We can create new classes of objects
by inheriting attributes and behaviours from existing classes and then extending themWe can build hierarchies (family trees)
of classes Person
Employee
Part Time Full Time
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.
Inheritance
Example Rally Car
Inherits properties of class Car … … and extends class Car by adding a
rollcage, racing brakes, fire extinguisher, etc.
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!
DOING IT IN CODEFROM THEORY TO PRACTICE
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
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
1. Add Class to the Project
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
2. Provide Appropriate Name
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
3. Create Constructors
Sub New replaces Class_Initialize Executes code when object is
instantiated
Can overload, but does not use Overloads keyword
Public Sub New( ) 'Perform simple initialization Course = “BIS”End Sub
Public Sub New( ) 'Perform simple initialization Course = “BIS”End Sub
Public Sub New(ByVal i As Integer) 'Overloaded without Overloads 'Perform more complex initialization intValue = iEnd Sub
Public Sub New(ByVal i As Integer) 'Overloaded without Overloads 'Perform more complex initialization intValue = iEnd Sub
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
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
immediatelyProtected Overrides Sub Finalize( )
'Can close connections or other resources
conn.Close
End Sub
Protected Overrides Sub Finalize( )
'Can close connections or other resources
conn.Close
End Sub
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
5. Declare Properties
Specify accessibility of variables and procedures
Keyword DefinitionPublic 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 Friend
The union of Protected and Friend.
5. Cont’d
Properties represent a classes attributes
Student First Name Last Name StudentID Age Course
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
If a procedure can only obtain a property it is Read Only
If it can be obtained and changed it is Read-Write
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
6. Declare Methods
Methods represent a classes behaviours
Student Eat Sleep Drink Study Pass Fail Graduate
USING OUR CLASSFROM THEORY TO PRACTICE
Instantiating our Class
Create the Object
Write object attributes
Read object attributes
Use object behaviours
Instantiating our Class
Create the Object
Write object attributes
Read object attributes
Use object behaviours
Create the Object
Dim myStudent As New Student
Instantiating our Class
Create the Object
Write object attributes
Read object attributes
Use object behaviours
Write Object Attributes
myStudent.FirstName = _ txtFirstName.Text
myStudent.LastName = txtLastName.Text
myStudent.Age = Val(txtAge.Text)
myStudent.StudentID = _ txtStudentID.Text
Write Object Attributes
myStudent.FirstName = _ txtFirstName.Text
myStudent.LastName = txtLastName.Text
myStudent.Age = Val(txtAge.Text)
myStudent.StudentID = _ txtStudentID.Text
Write Object Attributes
myStudent.FirstName = _ txtFirstName.Text
myStudent.LastName = txtLastName.Text
myStudent.Age = Val(txtAge.Text)
myStudent.StudentID = _ txtStudentID.Text
Write Object Attributes
myStudent.FirstName = _ txtFirstName.Text
myStudent.LastName = txtLastName.Text
myStudent.Age = Val(txtAge.Text)
myStudent.StudentID = _ txtStudentID.Text
EXTENDING OUR CLASSFROM THEORY TO PRACTICE
Recommended