SlideShare a Scribd company logo
THE BASICS

OBJECT ORIENTED PROGRAMMING
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 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
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
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 Dog
   Public Name As String

   Public Sub Sleep()
       MessageBox.Show(“ZZzz”)
   End Sub
End Class
Class
               Object



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

                            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
                                   Base Class
 Examples:
   A customer “is a kind of” person         Person
   An employee “is a kind of” person




                      Derived classes   Customer      Employee
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
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!
FROM THEORY TO PRACTICE

DOING IT IN CODE
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
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
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
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
           immediately
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     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
5. Cont’d

 Properties represent a classes attributes
 Student
   First Name
   Last Name
   StudentID
   Age
   Course
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
Is2215 lecture2 student(2)
Is2215 lecture2 student(2)
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
Is2215 lecture2 student(2)
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
6. Declare Methods

 Methods represent a classes behaviours
 Student
   Eat
   Sleep
   Drink
   Study
   Pass
   Fail
   Graduate
Is2215 lecture2 student(2)
FROM THEORY TO PRACTICE

USING OUR CLASS
Is2215 lecture2 student(2)
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
Is2215 lecture2 student(2)
Is2215 lecture2 student(2)
Write Object Attributes


myStudent.FirstName = _
 txtFirstName.Text
myStudent.LastName = txtLastName.Text
myStudent.Age = Val(txtAge.Text)
myStudent.StudentID = _
 txtStudentID.Text
Is2215 lecture2 student(2)
Is2215 lecture2 student(2)
Write Object Attributes


myStudent.FirstName = _
 txtFirstName.Text
myStudent.LastName = txtLastName.Text
myStudent.Age = Val(txtAge.Text)
myStudent.StudentID = _
 txtStudentID.Text
Is2215 lecture2 student(2)
Is2215 lecture2 student(2)
Write Object Attributes


myStudent.FirstName = _
 txtFirstName.Text
myStudent.LastName = txtLastName.Text
myStudent.Age = Val(txtAge.Text)
myStudent.StudentID = _
 txtStudentID.Text
Is2215 lecture2 student(2)
Is2215 lecture2 student(2)
FROM THEORY TO PRACTICE

EXTENDING OUR CLASS
Ad

More Related Content

What's hot (13)

Oops Concept Java
Oops Concept JavaOops Concept Java
Oops Concept Java
Kamlesh Singh
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
BG Java EE Course
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
mustafa sarac
 
Java Script Patterns
Java Script PatternsJava Script Patterns
Java Script Patterns
Allan Huang
 
Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentals
AnsgarMary
 
Java object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - BrainsmartlabsJava object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - Brainsmartlabs
brainsmartlabsedu
 
Classes And Objects
Classes And ObjectsClasses And Objects
Classes And Objects
rahulsahay19
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Presentation on java
Presentation  on  javaPresentation  on  java
Presentation on java
shashi shekhar
 
Intro to Objective C
Intro to Objective CIntro to Objective C
Intro to Objective C
Ashiq Uz Zoha
 
Java
JavaJava
Java
Raghu nath
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
Usman Mehmood
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
RatnaJava
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
mustafa sarac
 
Java Script Patterns
Java Script PatternsJava Script Patterns
Java Script Patterns
Allan Huang
 
Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentals
AnsgarMary
 
Java object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - BrainsmartlabsJava object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - Brainsmartlabs
brainsmartlabsedu
 
Classes And Objects
Classes And ObjectsClasses And Objects
Classes And Objects
rahulsahay19
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Intro to Objective C
Intro to Objective CIntro to Objective C
Intro to Objective C
Ashiq Uz Zoha
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
Usman Mehmood
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
RatnaJava
 

Viewers also liked (10)

Is2215 lecture5 lecturer_g_cand_classlibraries
Is2215 lecture5 lecturer_g_cand_classlibrariesIs2215 lecture5 lecturer_g_cand_classlibraries
Is2215 lecture5 lecturer_g_cand_classlibraries
dannygriff1
 
Is2215 lecture8 relational_databases
Is2215 lecture8 relational_databasesIs2215 lecture8 relational_databases
Is2215 lecture8 relational_databases
dannygriff1
 
Is2215 lecture4 student (1)
Is2215 lecture4 student (1)Is2215 lecture4 student (1)
Is2215 lecture4 student (1)
dannygriff1
 
Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)
dannygriff1
 
Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)
dannygriff1
 
Is2215 lecture7 lecturer_ado_intro
Is2215 lecture7 lecturer_ado_introIs2215 lecture7 lecturer_ado_intro
Is2215 lecture7 lecturer_ado_intro
dannygriff1
 
Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)
dannygriff1
 
Is2215 lecture6 lecturer_file_access
Is2215 lecture6 lecturer_file_accessIs2215 lecture6 lecturer_file_access
Is2215 lecture6 lecturer_file_access
dannygriff1
 
Stocks&bonds2214 1
Stocks&bonds2214 1Stocks&bonds2214 1
Stocks&bonds2214 1
dannygriff1
 
Mcq sample
Mcq sampleMcq sample
Mcq sample
dannygriff1
 
Is2215 lecture5 lecturer_g_cand_classlibraries
Is2215 lecture5 lecturer_g_cand_classlibrariesIs2215 lecture5 lecturer_g_cand_classlibraries
Is2215 lecture5 lecturer_g_cand_classlibraries
dannygriff1
 
Is2215 lecture8 relational_databases
Is2215 lecture8 relational_databasesIs2215 lecture8 relational_databases
Is2215 lecture8 relational_databases
dannygriff1
 
Is2215 lecture4 student (1)
Is2215 lecture4 student (1)Is2215 lecture4 student (1)
Is2215 lecture4 student (1)
dannygriff1
 
Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)
dannygriff1
 
Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)
dannygriff1
 
Is2215 lecture7 lecturer_ado_intro
Is2215 lecture7 lecturer_ado_introIs2215 lecture7 lecturer_ado_intro
Is2215 lecture7 lecturer_ado_intro
dannygriff1
 
Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)
dannygriff1
 
Is2215 lecture6 lecturer_file_access
Is2215 lecture6 lecturer_file_accessIs2215 lecture6 lecturer_file_access
Is2215 lecture6 lecturer_file_access
dannygriff1
 
Stocks&bonds2214 1
Stocks&bonds2214 1Stocks&bonds2214 1
Stocks&bonds2214 1
dannygriff1
 
Ad

Similar to Is2215 lecture2 student(2) (20)

Oops
OopsOops
Oops
Jaya Kumari
 
Unit3
Unit3Unit3
Unit3
Abha Damani
 
Coding Objects
Coding ObjectsCoding Objects
Coding Objects
Randy Riness @ South Puget Sound Community College
 
Oops
OopsOops
Oops
Sankar Balasubramanian
 
Application package
Application packageApplication package
Application package
JAYAARC
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
Terry Yoast
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
Vasilios Kuznos
 
VB.net&OOP.pptx
VB.net&OOP.pptxVB.net&OOP.pptx
VB.net&OOP.pptx
BharathiLakshmiAAssi
 
Classes and objects object oriented programming
Classes and objects object oriented programmingClasses and objects object oriented programming
Classes and objects object oriented programming
areebakanwal12
 
basic concepts of object oriented programming
basic concepts of object oriented programmingbasic concepts of object oriented programming
basic concepts of object oriented programming
infotechsaasc
 
Mca 504 dotnet_unit3
Mca 504 dotnet_unit3Mca 504 dotnet_unit3
Mca 504 dotnet_unit3
Rai Saheb Bhanwar Singh College Nasrullaganj
 
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdf
ArpitaJana28
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
Inocentshuja Ahmad
 
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScriptLotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Bill Buchan
 
Class properties
Class propertiesClass properties
Class properties
Siva Priya
 
Oop concepts classes_objects
Oop concepts classes_objectsOop concepts classes_objects
Oop concepts classes_objects
William Olivier
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
Geophery sanga
 
Object oriented basics
Object oriented basicsObject oriented basics
Object oriented basics
vamshimahi
 
OOSD Lecture 1-1.pptx FOR ENGINEERING STUDENTS
OOSD Lecture 1-1.pptx FOR ENGINEERING STUDENTSOOSD Lecture 1-1.pptx FOR ENGINEERING STUDENTS
OOSD Lecture 1-1.pptx FOR ENGINEERING STUDENTS
RajendraKumarRajouri1
 
oopsinvb-191021101327.pdf
oopsinvb-191021101327.pdfoopsinvb-191021101327.pdf
oopsinvb-191021101327.pdf
JP Chicano
 
Ad

More from dannygriff1 (16)

Risk08a
Risk08aRisk08a
Risk08a
dannygriff1
 
Profitability&npv
Profitability&npvProfitability&npv
Profitability&npv
dannygriff1
 
Npvrisk
NpvriskNpvrisk
Npvrisk
dannygriff1
 
Npv2214(1)
Npv2214(1)Npv2214(1)
Npv2214(1)
dannygriff1
 
Irr(1)
Irr(1)Irr(1)
Irr(1)
dannygriff1
 
Npv rule
Npv ruleNpv rule
Npv rule
dannygriff1
 
Ec2204 tutorial 8(2)
Ec2204 tutorial 8(2)Ec2204 tutorial 8(2)
Ec2204 tutorial 8(2)
dannygriff1
 
Ec2204 tutorial 3(1)
Ec2204 tutorial 3(1)Ec2204 tutorial 3(1)
Ec2204 tutorial 3(1)
dannygriff1
 
Ec2204 tutorial 1(2)
Ec2204 tutorial 1(2)Ec2204 tutorial 1(2)
Ec2204 tutorial 1(2)
dannygriff1
 
6 price and output determination- monopoly
6 price and output determination- monopoly6 price and output determination- monopoly
6 price and output determination- monopoly
dannygriff1
 
5 industry structure and competition analysis
5  industry structure and competition analysis5  industry structure and competition analysis
5 industry structure and competition analysis
dannygriff1
 
4 production and cost
4  production and cost4  production and cost
4 production and cost
dannygriff1
 
3 consumer choice
3 consumer choice3 consumer choice
3 consumer choice
dannygriff1
 
2 demand-supply and elasticity
2  demand-supply and elasticity2  demand-supply and elasticity
2 demand-supply and elasticity
dannygriff1
 
1 goals of the firm
1  goals of the firm1  goals of the firm
1 goals of the firm
dannygriff1
 
Is2215 lecture3 student (1)
Is2215 lecture3 student (1)Is2215 lecture3 student (1)
Is2215 lecture3 student (1)
dannygriff1
 
Profitability&npv
Profitability&npvProfitability&npv
Profitability&npv
dannygriff1
 
Ec2204 tutorial 8(2)
Ec2204 tutorial 8(2)Ec2204 tutorial 8(2)
Ec2204 tutorial 8(2)
dannygriff1
 
Ec2204 tutorial 3(1)
Ec2204 tutorial 3(1)Ec2204 tutorial 3(1)
Ec2204 tutorial 3(1)
dannygriff1
 
Ec2204 tutorial 1(2)
Ec2204 tutorial 1(2)Ec2204 tutorial 1(2)
Ec2204 tutorial 1(2)
dannygriff1
 
6 price and output determination- monopoly
6 price and output determination- monopoly6 price and output determination- monopoly
6 price and output determination- monopoly
dannygriff1
 
5 industry structure and competition analysis
5  industry structure and competition analysis5  industry structure and competition analysis
5 industry structure and competition analysis
dannygriff1
 
4 production and cost
4  production and cost4  production and cost
4 production and cost
dannygriff1
 
3 consumer choice
3 consumer choice3 consumer choice
3 consumer choice
dannygriff1
 
2 demand-supply and elasticity
2  demand-supply and elasticity2  demand-supply and elasticity
2 demand-supply and elasticity
dannygriff1
 
1 goals of the firm
1  goals of the firm1  goals of the firm
1 goals of the firm
dannygriff1
 
Is2215 lecture3 student (1)
Is2215 lecture3 student (1)Is2215 lecture3 student (1)
Is2215 lecture3 student (1)
dannygriff1
 

Is2215 lecture2 student(2)

  • 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
  • 11. VB.NET & OOP  VB .NET is an object-oriented language.  VB.NET Supports:  Encapsulation  Abstraction  Inheritance  Polymorphism
  • 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!
  • 21. FROM THEORY TO PRACTICE DOING IT IN CODE
  • 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
  • 24. 1. Add Class to the Project
  • 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
  • 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
  • 40.  If a procedure can only obtain a property it is Read Only  If it can be obtained and changed it is Read-Write
  • 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
  • 44. 6. Declare Methods  Methods represent a classes behaviours  Student  Eat  Sleep  Drink  Study  Pass  Fail  Graduate
  • 46. FROM THEORY TO PRACTICE USING OUR CLASS
  • 48. Instantiating our Class Create the Object Write object attributes Read object attributes Use object behaviours
  • 49. Instantiating our Class Create the Object Write object attributes Read object attributes Use object behaviours
  • 50. Create the Object Dim myStudent As New Student
  • 51. Instantiating our Class Create the Object Write object attributes Read object attributes Use object behaviours
  • 52. Write Object Attributes myStudent.FirstName = _ txtFirstName.Text myStudent.LastName = txtLastName.Text myStudent.Age = Val(txtAge.Text) myStudent.StudentID = _ txtStudentID.Text
  • 55. Write Object Attributes myStudent.FirstName = _ txtFirstName.Text myStudent.LastName = txtLastName.Text myStudent.Age = Val(txtAge.Text) myStudent.StudentID = _ txtStudentID.Text
  • 58. Write Object Attributes myStudent.FirstName = _ txtFirstName.Text myStudent.LastName = txtLastName.Text myStudent.Age = Val(txtAge.Text) myStudent.StudentID = _ txtStudentID.Text
  • 61. Write Object Attributes myStudent.FirstName = _ txtFirstName.Text myStudent.LastName = txtLastName.Text myStudent.Age = Val(txtAge.Text) myStudent.StudentID = _ txtStudentID.Text
  • 64. FROM THEORY TO PRACTICE EXTENDING OUR CLASS

Editor's Notes

  • #18: Every time you create an application in VB.NET you are using inheritance (Forms)
  翻译: