classes and objects
TRANSCRIPT
Custom ClassesCustom ClassesCIS-166
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).
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.
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
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.
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.
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
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
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.
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
MethodsMethodsActions that object can completeMay return a value or notNeed to consider arguments to
make sure needed data is provided
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
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
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
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