2. Summary Slide
• Instantiating An Object
• Encapsulation
• Inheritance
• Polymorphism
▫ Overriding Methods
▫ Overloading vs. Overriding
• Implementing a Time Abstract Data type with a Class
▫ Controlling Access to Members
▫ Initializing Class Objects: Constructors
▫ Properties
▫ Composition
▫ Shared Class Members
▫ Const and ReadOnly Members
• Garbage Collection
3. Object Terminology Review
• Object - like a noun, a thing
▫ Buttons, Text Boxes, Labels
• Properties - like an adjective, characteristics of object
▫ Text, ForeColor, Checked, Visible, Enabled
• Methods - like a verb, an action or behavior, something the
object can do or have done to it
▫ ShowDialog, Focus, Clear, ToUpper, ToLower
• Events - object response to user action or other events
▫ Click, Enter, Activate
4. Instantiating An Object
• Creating a new object based on a class
• Create an instance of the class by using the
New keyword and specify the class
6. Inheritance
• Inheritance is a form of reusability in which classes
are created by absorbing an existing class’s data and
behaviors and improving by adding new capabilities.
• Inheritance allows a software developer to derive a
new class from an existing one
• The existing class is called the parent class, or
superclass, or base class
• The derived class is called the child class or
subclass.
• As the name implies, the child inherits
characteristics of the parent
6
8. Deriving Subclasses
• In VB.NET, we use the reserved word Inherits
to establish an inheritance relationship
8
9. Controlling Inheritance
• Visibility modifiers determine which class
members get inherited and which do not
• Variables and methods declared with public
visibility are inherited, and those with private
visibility are not
• But public variables violate our goal of
encapsulation
• There are two more visibility modifiers that
helps in inheritance situations: Protected and
Friend
9
10. The Protected Modifier
• The Protected visibility modifier allows a
member of a base class to be inherited into the
child
• But Protected visibility provides more
encapsulation than public does
• However, Protected visibility is not as tightly
encapsulated as Private visibility
10
11. The Friend Modifier
• The Friend visibility modifier allows a member
of a base class to be inherited into the child only
if the the derived class is in the same assembly
11
13. Single vs. Multiple Inheritance
• VB.NET supports single inheritance, meaning that a
derived class can have only one parent class
• Multiple inheritance allows a class to be derived from
two or more classes, inheriting the members of all
parents
• Collisions, such as the same variable name in two
parents, have to be resolved
• In most cases, the use of interfaces gives us the best
aspects of multiple inheritance without the overhead
14. Polymorphism
• Different classes of objects may have behaviors
that are named the same but are implemented
differently
• Programmers can request an action without
knowing exactly what kind of object they have or
exactly how it will carry out the action
15. Polymorphism Implemented
• Overloading
▫ Argument type determines which version of a
method is used
▫ Example: MessageBox.Show method
• Overriding
▫ Refers to a class that has the same method name
as its base class
▫ Method in subclass takes precedence
16. Overriding Methods
• A child class can override the definition of an
inherited method in favor of its own
• That is, a child can redefine a method that it
inherits from its parent
• The new method must have the same
signature as the parent's method, but can
have different code in the body
16
17. Overloading vs. Overriding
• Don't confuse the concepts of overloading and
overriding
• Overloading deals with multiple methods in the same
class with the same name but different signatures
• Overriding deals with two methods, one in a parent
class and one in a child class, that have the same
signature
17
18. Reusability
• The main purpose behind OOOP and
Inheritance in particular
• New classes created with Class Module can be
used in multiple projects
• Each object created from the class can have its
own properties
19. Multitier Applications
• Common use of classes is 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
20. Three-tier Model
• Most common implementation of multitier
Presentation Tier Business Tier Data Tier
User Interface
Forms
Controls
Menus
Business Objects
Validation
Calculations
Business Logic
Business Rates
Data Retrieval
Data Storage
21. Implementing a Time Abstract Data
type with a Class
• VB programmers concentrate on creating
their own user-defined types called classes
(also referred as programmer defined types)
• Classes in VB facilitate the creation of special
data types, called abstract data types (ADT)
22. Class Scope
• A class’s instance variables and methods belong
to the class’s scope.
• Class members that are visible can be accessed
only through a “handle”
(ObjectReferenceName.memberName)
• Variables within methods
▫ Only methods can access that variable
• Keyword Me is a hidden instance variable can be
accessed in a method by preceding its name with
the keyword Me and dot operator
23. Controlling Access to Members
• The member access modifiers Public, Private,
Protected, and Friend control access to a class’s
instance variables and methods.
• Control access to a class’s instance variables and
methods
▫ Public: Serves primarily to present interfaces of a
class
▫ Private: Holds clients private data safely
▫ Get and set functions have ability to access private
data
24. Initializing Class Objects: Constructors
• A constructor method initializes its class’s members
• When appropriate, provide a default constructor to
ensure that every object is initialized with
meaningful values
• Parametized constructors have arguments
• If a class does not have a defined constructor, the
compiler will create an empty constructor.
• If an instance variable is not initialized the compiler
will assign a default value
• Overloaded Constructors must have different
numbers and/or types and/or orders of parameters
25. Properties
• Methods in a class can manipulate the class’s
Private instance variables. Public methods allow
other object to change a class’s properties.
• Get accessor
▫ In Visual Basic instance variables as private does
not guarantee data integrity
• Set accessor
▫ Cannot return values indicating a failed attempt to
assign invalid data to objects of the class
▫ Control the setting of instance variables to valid
values
• Get and Set accessors are not required
• A property with only Get accessor is called
ReadOnly
• A property with only Set accessor is called
WriteOnly
• After we define a property, we can use it in the
same way as we use a variable.
27. Shared Class Members
• Contains only one copy of this variable in memory
• When a single copy of the data will suffice, use
Shared class variables to save storage.
• Shared class variables are not the same as global
variables because Shared class variables have class
scope
• A class’s shared class members are available as soon
as the class is loaded into memory at execution time
• Shared method has no Me reference
• A shared method cannot access non-shared class
members.
29. Garbage Collection
• Resource leaks
▫ Objects must have an efficient way to return memory and release resources
when the program no longer uses those objects
• Memory leaks
▫ In Visual Basic memory is reclaimed automatically, hence it experiences rare
memory leaks as compared to C and C++
• Finalization
▫ Finalizer method performs termination housekeeping on that object just
before the garbage collector reclaims the object's memory.
• Feature of .NET Common Language Runtime (CLR) that cleans up
unused components
• Periodically checks for unreferenced objects and releases all memory
and system resources used by the objects
• Microsoft recommends depending on Garbage Collection rather than
Finalize procedures