64
OBJECT ORIENTED PROGRAMMING THE BASICS

Is2215 lecture2 student(2)

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Is2215 lecture2 student(2)

OBJECT ORIENTED PROGRAMMING

THE BASICS

Page 2: Is2215 lecture2 student(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

Page 3: Is2215 lecture2 student(2)

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

Page 4: Is2215 lecture2 student(2)

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

Page 5: Is2215 lecture2 student(2)

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

Page 6: Is2215 lecture2 student(2)

Class Examples

Dogs

Attributes: Four legs, a tail Behaviours: Barking

Cars

Attributes: Four wheels, engine, 3 or 5 doors Behaviours: Acceleration, braking, turning

Page 7: Is2215 lecture2 student(2)

In Code

The programming constructs of attributes and behaviours are implemented as: Attributes: Properties Behaviours: Methods

Page 8: Is2215 lecture2 student(2)

Public Class DogPublic Name As String

Public Sub Sleep()MessageBox.Show(“ZZzz”)

End SubEnd Class

Page 9: Is2215 lecture2 student(2)

Dim GoldenRetriever as New DogGoldenRetriever.Name = “Rex”GoldenRetriever.Sleep()

Object Class

Property

Method

Page 10: Is2215 lecture2 student(2)

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

Page 11: Is2215 lecture2 student(2)

VB.NET & OOP

VB .NET is an object-oriented language.

VB.NET Supports:

Encapsulation Abstraction

Inheritance Polymorphism

Page 12: Is2215 lecture2 student(2)

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

Page 13: Is2215 lecture2 student(2)

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

Page 14: Is2215 lecture2 student(2)

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.

Page 15: Is2215 lecture2 student(2)

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

Page 16: Is2215 lecture2 student(2)

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

Page 17: Is2215 lecture2 student(2)

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

Page 18: Is2215 lecture2 student(2)

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.

Page 19: Is2215 lecture2 student(2)

Inheritance

Example Rally Car

Inherits properties of class Car … … and extends class Car by adding a

rollcage, racing brakes, fire extinguisher, etc.

Page 20: Is2215 lecture2 student(2)

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!

Page 21: Is2215 lecture2 student(2)

DOING IT IN CODEFROM THEORY TO PRACTICE

Page 22: Is2215 lecture2 student(2)

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

Page 23: Is2215 lecture2 student(2)

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

Page 24: Is2215 lecture2 student(2)

1. Add Class to the Project

Page 25: Is2215 lecture2 student(2)

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

Page 26: Is2215 lecture2 student(2)

2. Provide Appropriate Name

Page 27: Is2215 lecture2 student(2)
Page 28: Is2215 lecture2 student(2)

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

Page 29: Is2215 lecture2 student(2)

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

Page 30: Is2215 lecture2 student(2)

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

Page 31: Is2215 lecture2 student(2)

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

Page 32: Is2215 lecture2 student(2)

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

Page 33: Is2215 lecture2 student(2)

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.

Page 34: Is2215 lecture2 student(2)

5. Cont’d

Properties represent a classes attributes

Student First Name Last Name StudentID Age Course

Page 35: Is2215 lecture2 student(2)
Page 36: Is2215 lecture2 student(2)

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

Page 37: Is2215 lecture2 student(2)
Page 38: Is2215 lecture2 student(2)
Page 39: Is2215 lecture2 student(2)
Page 40: Is2215 lecture2 student(2)

If a procedure can only obtain a property it is Read Only

If it can be obtained and changed it is Read-Write

Page 41: Is2215 lecture2 student(2)
Page 42: Is2215 lecture2 student(2)
Page 43: Is2215 lecture2 student(2)

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

Page 44: Is2215 lecture2 student(2)

6. Declare Methods

Methods represent a classes behaviours

Student Eat Sleep Drink Study Pass Fail Graduate

Page 45: Is2215 lecture2 student(2)
Page 46: Is2215 lecture2 student(2)

USING OUR CLASSFROM THEORY TO PRACTICE

Page 47: Is2215 lecture2 student(2)
Page 48: Is2215 lecture2 student(2)

Instantiating our Class

Create the Object

Write object attributes

Read object attributes

Use object behaviours

Page 49: Is2215 lecture2 student(2)

Instantiating our Class

Create the Object

Write object attributes

Read object attributes

Use object behaviours

Page 50: Is2215 lecture2 student(2)

Create the Object

Dim myStudent As New Student

Page 51: Is2215 lecture2 student(2)

Instantiating our Class

Create the Object

Write object attributes

Read object attributes

Use object behaviours

Page 52: Is2215 lecture2 student(2)

Write Object Attributes

myStudent.FirstName = _ txtFirstName.Text

myStudent.LastName = txtLastName.Text

myStudent.Age = Val(txtAge.Text)

myStudent.StudentID = _ txtStudentID.Text

Page 53: Is2215 lecture2 student(2)
Page 54: Is2215 lecture2 student(2)
Page 55: Is2215 lecture2 student(2)

Write Object Attributes

myStudent.FirstName = _ txtFirstName.Text

myStudent.LastName = txtLastName.Text

myStudent.Age = Val(txtAge.Text)

myStudent.StudentID = _ txtStudentID.Text

Page 56: Is2215 lecture2 student(2)
Page 57: Is2215 lecture2 student(2)
Page 58: Is2215 lecture2 student(2)

Write Object Attributes

myStudent.FirstName = _ txtFirstName.Text

myStudent.LastName = txtLastName.Text

myStudent.Age = Val(txtAge.Text)

myStudent.StudentID = _ txtStudentID.Text

Page 59: Is2215 lecture2 student(2)
Page 60: Is2215 lecture2 student(2)
Page 61: Is2215 lecture2 student(2)

Write Object Attributes

myStudent.FirstName = _ txtFirstName.Text

myStudent.LastName = txtLastName.Text

myStudent.Age = Val(txtAge.Text)

myStudent.StudentID = _ txtStudentID.Text

Page 62: Is2215 lecture2 student(2)
Page 63: Is2215 lecture2 student(2)
Page 64: Is2215 lecture2 student(2)

EXTENDING OUR CLASSFROM THEORY TO PRACTICE