SlideShare a Scribd company logo
1 of 64
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
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
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
FROM THEORY TO PRACTICE

USING OUR CLASS
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
FROM THEORY TO PRACTICE

EXTENDING OUR CLASS

More Related Content

What's hot

Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingmustafa sarac
 
Java Script Patterns
Java Script PatternsJava Script Patterns
Java Script PatternsAllan Huang
 
Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentalsAnsgarMary
 
Java object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - BrainsmartlabsJava object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - Brainsmartlabsbrainsmartlabsedu
 
Classes And Objects
Classes And ObjectsClasses And Objects
Classes And Objectsrahulsahay19
 
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 overridingNithyaN19
 
Intro to Objective C
Intro to Objective CIntro to Objective C
Intro to Objective CAshiq Uz Zoha
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascriptUsman Mehmood
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented ProgrammingRatnaJava
 

What's hot (13)

Oops Concept Java
Oops Concept JavaOops Concept Java
Oops Concept Java
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Java Script Patterns
Java Script PatternsJava Script Patterns
Java Script Patterns
 
Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentals
 
Java object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - BrainsmartlabsJava object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - Brainsmartlabs
 
Classes And Objects
Classes And ObjectsClasses And Objects
Classes And Objects
 
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
 
Presentation on java
Presentation  on  javaPresentation  on  java
Presentation on java
 
Intro to Objective C
Intro to Objective CIntro to Objective C
Intro to Objective C
 
Java
JavaJava
Java
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 

Viewers also liked

Is2215 lecture5 lecturer_g_cand_classlibraries
Is2215 lecture5 lecturer_g_cand_classlibrariesIs2215 lecture5 lecturer_g_cand_classlibraries
Is2215 lecture5 lecturer_g_cand_classlibrariesdannygriff1
 
Is2215 lecture8 relational_databases
Is2215 lecture8 relational_databasesIs2215 lecture8 relational_databases
Is2215 lecture8 relational_databasesdannygriff1
 
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_introdannygriff1
 
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_accessdannygriff1
 
Stocks&bonds2214 1
Stocks&bonds2214 1Stocks&bonds2214 1
Stocks&bonds2214 1dannygriff1
 

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
 
Is2215 lecture8 relational_databases
Is2215 lecture8 relational_databasesIs2215 lecture8 relational_databases
Is2215 lecture8 relational_databases
 
Is2215 lecture4 student (1)
Is2215 lecture4 student (1)Is2215 lecture4 student (1)
Is2215 lecture4 student (1)
 
Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)
 
Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)
 
Is2215 lecture7 lecturer_ado_intro
Is2215 lecture7 lecturer_ado_introIs2215 lecture7 lecturer_ado_intro
Is2215 lecture7 lecturer_ado_intro
 
Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)
 
Is2215 lecture6 lecturer_file_access
Is2215 lecture6 lecturer_file_accessIs2215 lecture6 lecturer_file_access
Is2215 lecture6 lecturer_file_access
 
Stocks&bonds2214 1
Stocks&bonds2214 1Stocks&bonds2214 1
Stocks&bonds2214 1
 
Mcq sample
Mcq sampleMcq sample
Mcq sample
 

Similar to OOP BASICS

oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfArpitaJana28
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .NetGreg Sohl
 
Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPRick Ogden
 
(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHPRick Ogden
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Javabackdoor
 
Object oriented basics
Object oriented basicsObject oriented basics
Object oriented basicsvamshimahi
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsMaryo Manjaruni
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceEng Teong Cheah
 
Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programmingNeelesh Shukla
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialscesarmendez78
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHPVibrant Technologies & Computers
 

Similar to OOP BASICS (20)

Oops
OopsOops
Oops
 
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdf
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
 
Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHP
 
Lab 4 (1).pdf
Lab 4 (1).pdfLab 4 (1).pdf
Lab 4 (1).pdf
 
(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
Object oriented basics
Object oriented basicsObject oriented basics
Object oriented basics
 
python.pptx
python.pptxpython.pptx
python.pptx
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
 
Oops
OopsOops
Oops
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
 
Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programming
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorials
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 

More from dannygriff1

Profitability&npv
Profitability&npvProfitability&npv
Profitability&npvdannygriff1
 
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- monopolydannygriff1
 
5 industry structure and competition analysis
5  industry structure and competition analysis5  industry structure and competition analysis
5 industry structure and competition analysisdannygriff1
 
4 production and cost
4  production and cost4  production and cost
4 production and costdannygriff1
 
3 consumer choice
3 consumer choice3 consumer choice
3 consumer choicedannygriff1
 
2 demand-supply and elasticity
2  demand-supply and elasticity2  demand-supply and elasticity
2 demand-supply and elasticitydannygriff1
 
1 goals of the firm
1  goals of the firm1  goals of the firm
1 goals of the firmdannygriff1
 
Is2215 lecture3 student (1)
Is2215 lecture3 student (1)Is2215 lecture3 student (1)
Is2215 lecture3 student (1)dannygriff1
 

More from dannygriff1 (16)

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

OOP BASICS

  • 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
  • 27.
  • 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
  • 44. 6. Declare Methods  Methods represent a classes behaviours  Student  Eat  Sleep  Drink  Study  Pass  Fail  Graduate
  • 45.
  • 46. FROM THEORY TO PRACTICE USING OUR CLASS
  • 47.
  • 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
  • 53.
  • 54.
  • 55. Write Object Attributes myStudent.FirstName = _ txtFirstName.Text myStudent.LastName = txtLastName.Text myStudent.Age = Val(txtAge.Text) myStudent.StudentID = _ txtStudentID.Text
  • 56.
  • 57.
  • 58. Write Object Attributes myStudent.FirstName = _ txtFirstName.Text myStudent.LastName = txtLastName.Text myStudent.Age = Val(txtAge.Text) myStudent.StudentID = _ txtStudentID.Text
  • 59.
  • 60.
  • 61. Write Object Attributes myStudent.FirstName = _ txtFirstName.Text myStudent.LastName = txtLastName.Text myStudent.Age = Val(txtAge.Text) myStudent.StudentID = _ txtStudentID.Text
  • 62.
  • 63.
  • 64. FROM THEORY TO PRACTICE EXTENDING OUR CLASS

Editor's Notes

  1. Every time you create an application in VB.NET you are using inheritance (Forms)