Is2215 lecture2 student(2)

390 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
390
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Every time you create an application in VB.NET you are using inheritance (Forms)
  • Is2215 lecture2 student(2)

    1. 1. THE BASICSOBJECT ORIENTED PROGRAMMING
    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. 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. 4. Classes and ObjectsClass 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. 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. 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. 7. In Code The programming constructs of attributes and behaviours are implemented as:  Attributes: Properties  Behaviours: Methods
    8. 8. Public Class Dog Public Name As String Public Sub Sleep() MessageBox.Show(“ZZzz”) End SubEnd Class
    9. 9. Class ObjectDim GoldenRetriever as New DogGoldenRetriever.Name = “Rex”GoldenRetriever.Sleep() Property Method
    10. 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. 11. VB.NET & OOP VB .NET is an object-oriented language. VB.NET Supports:  Encapsulation  Abstraction  Inheritance  Polymorphism
    12. 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. 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. 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. 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. 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. 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. 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. 19. Inheritance Example  Rally Car  Inherits properties of class Car …  … and extends class Car by adding a rollcage, racing brakes, fire extinguisher, etc.
    20. 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. 21. FROM THEORY TO PRACTICEDOING IT IN CODE
    22. 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. 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. 24. 1. Add Class to the Project
    25. 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
    26. 26. 2. Provide Appropriate Name
    27. 27. 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. 28. 3. Create Constructors  Sub New replaces Class_Initialize  Executes code when object is instantiatedPublic Sub New( ) Perform simple initialization Course = “BIS”End Sub  Can overload, but does not use Overloads keywordPublic Sub New(ByVal i As Integer) Overloaded without Overloads Perform more complex initialization intValue = iEnd Sub
    29. 29. 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
    30. 30. 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.CloseEnd Sub
    31. 31. 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
    32. 32. 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
    33. 33. 5. Cont’d Properties represent a classes attributes Student  First Name  Last Name  StudentID  Age  Course
    34. 34. 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
    35. 35.  If a procedure can only obtain a property it is Read Only If it can be obtained and changed it is Read-Write
    36. 36. 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
    37. 37. 6. Declare Methods Methods represent a classes behaviours Student  Eat  Sleep  Drink  Study  Pass  Fail  Graduate
    38. 38. FROM THEORY TO PRACTICEUSING OUR CLASS
    39. 39. Instantiating our Class Create the Object Write object attributes Read object attributes Use object behaviours
    40. 40. Instantiating our Class Create the Object Write object attributes Read object attributes Use object behaviours
    41. 41. Create the ObjectDim myStudent As New Student
    42. 42. Instantiating our Class Create the Object Write object attributes Read object attributes Use object behaviours
    43. 43. Write Object AttributesmyStudent.FirstName = _ txtFirstName.TextmyStudent.LastName = txtLastName.TextmyStudent.Age = Val(txtAge.Text)myStudent.StudentID = _ txtStudentID.Text
    44. 44. Write Object AttributesmyStudent.FirstName = _ txtFirstName.TextmyStudent.LastName = txtLastName.TextmyStudent.Age = Val(txtAge.Text)myStudent.StudentID = _ txtStudentID.Text
    45. 45. Write Object AttributesmyStudent.FirstName = _ txtFirstName.TextmyStudent.LastName = txtLastName.TextmyStudent.Age = Val(txtAge.Text)myStudent.StudentID = _ txtStudentID.Text
    46. 46. Write Object AttributesmyStudent.FirstName = _ txtFirstName.TextmyStudent.LastName = txtLastName.TextmyStudent.Age = Val(txtAge.Text)myStudent.StudentID = _ txtStudentID.Text
    47. 47. FROM THEORY TO PRACTICEEXTENDING OUR CLASS

    ×