classes and objects

15
Custom Classes Custom Classes CIS-166

Upload: randy-riness-south-puget-sound-community-college

Post on 11-Jun-2015

309 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Classes and Objects

Custom ClassesCustom ClassesCIS-166

Page 2: Classes and Objects

ObjectsObjectsObject-oriented languages 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 classbtnExit is an instance of the class.A single class type may be used to

create many objects (instances).

Page 3: Classes and 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 4: Classes and 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 5: Classes and 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 6: Classes and 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 7: Classes and 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 8: Classes and 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 9: Classes and 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 10: Classes and Objects

Public Variables v. Public Variables v. PropertiesPropertiesPublic variables are exposed,

visible to rest of application; no validation is performed◦Only limit on what’s stored is the

variable’s data type◦May be referred to as “fields”

Properties provide more control◦Can execute code to check data◦Can limit to read only or write only

Page 11: Classes and Objects

MethodsMethodsActions that object can completeMay return a value or notNeed to consider arguments to

make sure needed data is provided

Page 12: Classes and Objects

Property or Method?Property or Method?Use a property when you’re

reading or writing data (that’s all you’re doing)

Use a method if you’re implementing an action

Typically use a method if you need to pass multiple arguments, not a single value◦Alternative is to make a new data

type that combines values

Page 13: Classes and 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◦ Data about an object is specific to that

object

Page 14: Classes and Objects

Static MembersStatic MembersProperties and methods can be about

the class rather than about the instance

Single memory location that is available for ALL objects of a class

Can be accessed without instantiating an object of the class

Page 15: Classes and 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