Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Coding Objects

559 views

Published on

Programming with classes and objects in vb.net

Published in: Education
  • Be the first to comment

  • Be the first to like this

Coding Objects

  1. 1. Custom Classes CIS-166
  2. 2. Objects <ul><li>VB allows the creation of new object types by creating a class. </li></ul><ul><ul><li>Classes may have properties, methods, and events and describe what’s possible . </li></ul></ul><ul><ul><li>Objects are particular things such as an Add button, and describe what is . </li></ul></ul><ul><li>Button is a class </li></ul><ul><li>exitButton is an instance of the class. </li></ul><ul><li>A class type may be used to create many objects (instances). </li></ul>
  3. 3. Cookie Analogy <ul><li>Class = Cookie cutter (what cookie will look like) </li></ul><ul><li>Instantiate = Make a cookie using cookie cutter (use the NEW keyword) </li></ul><ul><li>Instance = Newly made cookie (object) </li></ul><ul><li>Properties of different Instances may have different values. </li></ul><ul><ul><li>One cookie may have icing, another not </li></ul></ul><ul><ul><li>Each cookie can have a different flavor </li></ul></ul><ul><li>Methods = Eat, Bake, or Crumble </li></ul><ul><li>Events = Cookie crumbling </li></ul>
  4. 4. Reusability <ul><li>Reusability is a major advantage of OOP over traditional programming </li></ul><ul><li>New classes created can be used in multiple projects. </li></ul><ul><li>Each object created from the class can have its own property values. </li></ul>
  5. 5. Object-Oriented Terminology <ul><li>Encapsulation : Object is complete by itself </li></ul><ul><li>Inheritance : One class can serve as the starting point for a second class </li></ul><ul><li>Polymorphism : One word/name may be used in different places, and operates correctly for its context </li></ul>
  6. 6. Encapsulation <ul><li>Combination of characteristics of an object along with its behavior in &quot;one package&quot; </li></ul><ul><li>Cannot make object do anything it does not already &quot;know&quot; how to do. </li></ul><ul><li>Cannot make up new properties, methods, or events for an object. </li></ul><ul><li>Sometimes referred to as data hiding, an object can expose only those data elements and procedures that it wishes. </li></ul>
  7. 7. Inheritance <ul><li>Ability to create a new class from an existing class </li></ul><ul><li>Original class is called Base Class, Superclass, or Parent Class. </li></ul><ul><li>Inherited class is called Subclass, Derived Class, or Child Class. </li></ul><ul><li>Example: each form created is inherited from the Form class and customized to current needs </li></ul><ul><li>Purpose of inheritance is reusability .  </li></ul>
  8. 8. Inheritance Example <ul><li>Can create a class that describes a person </li></ul><ul><ul><li>Firstname, Lastname, Birthday properties </li></ul></ul><ul><li>Can create a derived class that starts from person class and adds properties, methods of a student </li></ul><ul><ul><li>Major, Student ID </li></ul></ul><ul><li>Common phrasing: “IS A” </li></ul><ul><ul><li>“ A student IS A person” denotes student as derived class from person </li></ul></ul>
  9. 9. Polymorphism <ul><li>Methods having identical names, but different implementations </li></ul><ul><li>Radio button, check boxes, and list boxes all have a Select method—the Select method operates appropriately for its class. </li></ul><ul><li>Messagebox has multiple ways to Show, depending on arguments </li></ul>
  10. 10. Polymorphism in Practice <ul><li>Overloading — Several procedures have the same name with different argument lists </li></ul><ul><ul><li>Argument list creates a signature </li></ul></ul><ul><li>Overriding — Refers to a method that has the same name as its base class </li></ul><ul><ul><li>Method in subclass takes precedence, replaces the method from the parent class. </li></ul></ul>
  11. 11. Instance Variables <ul><li>Instance variables or properties store data specific to one object </li></ul><ul><li>Separate memory locations for the variables and procedures for each instance (copy) of the object </li></ul>
  12. 12. Shared Variables, Properties <ul><li>Single memory location that is available for ALL objects of a class </li></ul><ul><li>Can be accessed without instantiating an object of the class </li></ul><ul><li>Use the Shared keyword to create  </li></ul>
  13. 13. Constructors & Destructors <ul><li>Constructor : Method that automatically executes when an object is instantiated </li></ul><ul><ul><li>Create by writing a Public Sub New procedure </li></ul></ul><ul><li>Destructor : Method that automatically executes when an object is destroyed </li></ul><ul><ul><li>Create by writing a Finalize procedure </li></ul></ul>

×