13
Custom Classes Custom Classes CIS-166

Coding Objects

Embed Size (px)

DESCRIPTION

Programming with classes and objects in vb.net

Citation preview

Page 1: Coding Objects

Custom ClassesCustom ClassesCIS-166

Page 2: Coding Objects

ObjectsObjectsVB allows the creation of new object

types by creating a class.◦ Classes may have properties, methods,

and events and describe what’s possible.◦ Objects are particular things such as an

Add button, and describe what is.Button is a classexitButton is an instance of the class.A class type may be used to create

many objects (instances).

Page 3: Coding Objects

Cookie AnalogyCookie AnalogyClass = Cookie cutter (what cookie will

look like)Instantiate = Make a cookie using cookie

cutter (use the NEW keyword)Instance = Newly made cookie (object)Properties of different Instances may

have different values.◦ One cookie may have icing, another not◦ Each cookie can have a different flavor

Methods = Eat, Bake, or CrumbleEvents = Cookie crumbling

Page 4: Coding Objects

ReusabilityReusabilityReusability is a major advantage of

OOP over traditional programmingNew classes created can be used in

multiple projects.Each object created from the class can

have its own property values.

Page 5: Coding Objects

Object-Oriented Object-Oriented Terminology Terminology Encapsulation: Object is complete by

itself Inheritance: One class can serve as

the starting point for a second classPolymorphism: One word/name may

be used in different places, and operates correctly for its context

Page 6: Coding Objects

EncapsulationEncapsulationCombination 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 for an object.

Sometimes referred to as data hiding, an object can expose only those data elements and procedures that it wishes.

Page 7: Coding Objects

InheritanceInheritanceAbility to create a new class from an

existing classOriginal class is called Base Class,

Superclass, or Parent Class.Inherited class is called Subclass,

Derived Class, or Child Class.Example: each form created is

inherited from the Form class and customized to current needs

Purpose of inheritance is reusability. 

Page 8: Coding Objects

Inheritance ExampleInheritance ExampleCan create a class that describes a

person◦Firstname, Lastname, Birthday properties

Can create a derived class that starts from person class and adds properties, methods of a student◦Major, Student ID

Common phrasing: “IS A”◦“A student IS A person” denotes student

as derived class from person

Page 9: Coding Objects

PolymorphismPolymorphismMethods having identical names, but

different implementationsRadio button, check boxes, and list

boxes all have a Select method—the Select method operates appropriately for its class.

Messagebox has multiple ways to Show, depending on arguments

Page 10: Coding Objects

Polymorphism in Polymorphism in PracticePracticeOverloading — Several procedures

have the same name with different argument lists◦ Argument list creates a signature

Overriding — Refers to a method that has the same name as its base class◦ Method in subclass takes precedence,

replaces the method from the parent class.

Page 11: Coding Objects

Instance VariablesInstance VariablesInstance variables or properties store

data specific to one objectSeparate memory locations for the

variables and procedures for each instance (copy) of the object

Page 12: Coding Objects

Shared Variables, Shared Variables, PropertiesPropertiesSingle memory location that is

available for ALL objects of a classCan be accessed without instantiating

an object of the classUse the Shared keyword to create 

Page 13: Coding Objects

Constructors & Constructors & DestructorsDestructorsConstructor: Method that

automatically executes when an object is instantiated◦ Create by writing a Public Sub New

procedureDestructor: Method that

automatically executes when an object is destroyed◦ Create by writing a Finalize procedure