Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming: Classes and Objects

Similar presentations


Presentation on theme: "Object-Oriented Programming: Classes and Objects"— Presentation transcript:

1 Object-Oriented Programming: Classes and Objects

2 Multitier Applications
Classes are used to create multitier applications. Each of the functions of a multitier application can be coded in a separate component and stored and run on different machines. Goal is to create components that can be combined and replaced. A common practice for writing professional applications is to write independent components that work in multiple “tiers” or layers. If one part of the application needs to change, such as a redesign of the user interface or a new database format, the other components do not need to be replaced.

3 Three-tier Model Most popular implementation
The term “n-tier” application is an expansion of the three-tier model. The middle tier may be written in multiple classes that can be stored and run from multiple locations. The Presentation tier refers to the user interface, which in VB is the form. The Business tier is a class or classes that handle the data. The Data tier includes retrieving and storing the data in a database. It retrieves the data and passes the results to the Business tier, or takes data from the Business tier and writes it in the appropriate location.

4 "Cookie Analogy" Class = Cookie cutter
Instantiate = Making a cookie using the cookie cutter Instance = Newly made cookie Properties of the Instance may have different values. Icing property can be True or False. Flavor property could be Lemon or Chocolate Methods = Eat, Bake, or Crumble Events = Cookie crumbling and informing you Sometimes the distinction between a method and an event is somewhat fuzzy. Generally, anything that an object is told to do is a method; if the object does an action and needs to make it known, that’s an event. The properties of the cookie object are: Cookie1.Flavor = "Lemon" Cookie1.Topping = "Cream Frosting"

5 Object-Oriented Terminology
Encapsulation Inheritance Polymorphism Key features of an object-oriented language are encapsulation, inheritance, and polymorphism. *The next several slides are going to define several object-oriented terms.

6 Encapsulation Combination of characteristics of an object along with its behavior in "one package" Cannot make object do anything it does not already "know" how to do. Cannot make up new properties, methods, or events. Sometimes referred to as data hiding, an object can expose only those data elements and procedures that it wishes. Think of it being a completed package; all parts of the package are in a capsule. Encapsulation can be witnessed by looking at any program; the form is actually a class; all methods and events that are coded are enclosed within the Class and End Class statements; the variables placed in the code are properties of the specific form class being created. Through use of the Public, Private, Protected, and Friend keywords, an object can “expose” only those data elements and procedures that it wishes to allow the outside world to see.

7 Inheritance Ability to create a new class from an existing class
Original class is called Base Class, Superclass, or Parent Class. Inherited class is called Subclass, Derived Class, or Child Class. For example, each form created is inherited from the existing Form class. Purpose of inheritance is reusability.

8 Inheritance Example The derived classes inherit from the base class.
Base/Parent Class Person Derived/Child Class Employee Customer Student In inheritance, typically the classes go from general to the more specific. The derived classes inherit from the base class.

9 Polymorphism Methods having identical names, but different implementations Radio button, check boxes, and list boxes all have a Select method—the Select method operates appropriately for its class. Overloading — Several argument lists for calling the method Example: MessageBox.Show method Overriding — Refers to a method that has the same name as its base class Method in subclass takes precedence. The term, polymorphism, actually means the ability to take on many shapes or forms. Polymorphism allows a single class to have more than one method with the same name. When the method is called, the argument type determines which version of the method to use.

10 Designing Your Own Class
Analyze characteristics needed by new objects. Characteristics or properties are defined as variables. Define the properties as variables in the class module. Analyze behaviors needed by new objects. Behaviors are methods. Define the methods as sub procedures or functions.

11 Creating Properties in a Class
Define variables inside the Class module by declaring them as Private — these store the value of the properties of the class. Do not make Public, since that would violate encapsulation (each object should be in charge of its own data). Protected variables behave as private but are available in any class that inherits from this class—as a private or protected variable, the value is available only to procedures within the class, the same way that private module-level variables are available only to procedures within a form’s class code.

12 Property Procedures Properties in a class are accessed with accessor methods in a property procedure. Name used for property procedure is the name of the property seen by the outside world. Set accessor method. Uses Value keyword to refer to incoming value for property Assigns a value to the property Get Statement uses the value keyword to refer to the incoming value of the property. Must assign a return value to the procedure name or use a Return Statement to return a value. The Set statement uses the value keyword to refer to the incoming value for the property. Property procedures are public by default, so omit the optional Public keyword can be omitted. *The next slide displays the Property Procedure General Form.

13 Property Procedure General Form
{Private | Protected } ClassVariable As DataType [Public] Property PropertyName( ) As DataType Get Return ClassVariable [|PropertyName = ClassVariable] End Get Set (ByVal Value As DataType) [statements, such As validation] ClassVariable = Value End Set End Property Before the End Get, assign a return value to the procedure name or use a Return statement to return a value. The Property Get and Set retrieve the current value and assign a new value to the property. If you do not specify an access modifier, the default is Public.

14 Read-Only Properties In some instances, a value for a property can be retrieved by an object but not changed. A property can be written to create a read-only property. Create a read-only property by using the ReadOnly modifier. ' The property procedure for a read-only property. ReadOnly Property TotalPay() As Decimal Get Return TotalPayDecimal End Get End Property Use the ReadOnly modifier and write only the Get portion of the property procedure.

15 Write-Only Properties
At times, a property can be assigned by an object, but not retrieved. Create a property block that contains only a Set to create a write-only property. ' Private module-level variable to hold the property value. Private PriceDecimal As Decimal Public WriteOnly Property Price() As Decimal Set(ByVal value As Decimal) If value >= 0 Then PriceDecimal = value End If End Set End Property

16 Constructors and Destructors
Method that automatically executes when an object is instantiated Constructor must be public and is named New. Ideal location for an initialization tasks such as setting the initial values of variable and properties Destructor Method that automatically executes when an object is destroyed Rarely needed in .NET classes (automatic garbage collection) The constructor must be public, because the objects that are created must execute this method. If a class does not contain a constructor, the compiler creates an implicit method called the default constructor and has an empty argument list.

17 Overloading the Constructor
Overloading means that two methods have the same name but a different list of arguments (the signature). Create by giving the same name to multiple procedures in a class module, each with a different argument list.

18 Parameterized Constructor
Constructor that requires arguments Allows arguments to be passed when creating an object Within the class code, use the Me keyword to refer to the current class. When a class has both an empty constructor and a parameterized constructor, the program that creates the object can choose which method to use.

19

20 Value Types and Reference Types
Data types are divided into two categories—value types and reference types. A variable of a value type (such as Integer) contains a single value of that type. Dim CountInteger as Integer = 7 A reference type variable is initialized by default to the value Nothing if you do not initialize it in its declaration to refer to an object. When you attempt to use a variable that contains Nothing to interact with an object, you’ll receive a NullReferenceException. Dim Employee as Person

21 Class Scope A class’s instance variables, properties and methods have class scope. Within this scope, a class’s members are accessible to all of the class’s other members and can be referenced simply by name. Outside a class’s scope, class members cannot be referenced directly by name. Those class members that are visible (such as Public members) can be accessed through a variable that refers to an object of the class (for example, time.Hour).

22 Auto-Implemented Properties
For properties that do not have any additional logic in their Set and Get accessors, there’s a feature—called auto-implemented properties—that allows you to write one line of code and have the compiler to generate the property’s code for you. Public Property Hour As Integer The compiler would then generate a Private instance variable of type Integer named _Hour and the following property code Public Property Hour As Integer Get Return _Hour End Get Set(value As Integer) _Hour = value End Set End Property

23 Garbage Collection Every object you create uses various system resources, including the memory that holds the object itself. These resources must be returned to the system when they’re no longer needed to avoid resource leaks. The Common Language Runtime (CLR) performs automatic garbage collection to reclaim the memory occupied by objects that are no longer in use. When there are no more references to an object, it’s marked for garbage collection by the CLR.

24 Garbage Collection Resources like memory that are allocated and reclaimed by the CLR are known as managed resources. Other types of resource leaks can occur. For example, an app may open a file on disk to modify the file’s contents. If the app does not close the file, other apps may not be allowed to use the file until the app that opened the file finishes. Resources like files that you must manage are known as unmanaged resources (because they’re not managed by the CLR). Such resources are typically scarce and should be released as soon as they’re no longer needed by a program.

25 Instance Variables versus Shared Variables
Instance variables or properties Separate memory location for each instance of the object Shared variables or properties Single variable that is available for ALL objects of a class Can be accessed without instantiating an object of the class When creating, use the Shared keyword to create. Shared properties can be set to read-only so that their values can be retrieved but not set directly. Terminology varies from one OOP language to another — in some languages, shared members are called class variables or static variables. Microsoft documentation refers to instance members and shared members, which includes both properties and methods. A shared member has one copy for all objects of the class, and an instance member has one copy for each instance or object of the class. Methods also can be declared as shared and are considered shared members.


Download ppt "Object-Oriented Programming: Classes and Objects"

Similar presentations


Ads by Google