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