OOP Concepts of
VB.net
submitted by: Henok Birhanu
UU87312R
Object-Oriented (OO) Program
• Objects Class
– Consist of one Met
h o ho
or more data Met d
values which Data
d
define the state Messag Met
or properties e ho e t ho
d M
of the object
– Encapsulated by d
a set of functions (methods) that can be applied to
that object
2
Object-Oriented (OO) Program
• Class
– Defines:
• Characteristics of the data contained by
objects of the class
• Functions that can be applied to the objects
of the class
Clas
Me t Me t Me t
s Me
tho d
ho
M et h o d
ho M e tho d
ho
d Data d Data d Data
Me t Me t Me t
ho ho ho ho ho tho
d Me t d Met d Me
d d d
3
Object-Oriented Terminology
• Encapsulation
• Inheritance
• Polymorphism
4
Encapsulation
• Combination 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
• Sometimes referred to as data hiding; an
object can expose only those data elements
and procedures that it wishes
5
Encapsulation
Class
Met
M et ho d
ho
d Data
Messag Met
e ho e t ho
d M
d
6
Inheritance
• Ability to create a new class from an existing
class
– Original class is called Base Class,
Superclass, or Parent Class
– Inherited class is called Subclass, Derived
Class, or Child Class
• For example, each form created is inherited
from the existing Form class
• Purpose of inheritance is reusability
7
Inheritance (continued)
• Examine first line of code for a form in the
Editor
Inherited Class: Subclass, Derived Class, Child Class
Public Class Form1
Inherits
System.Windows.Forms.Form
Original Class: Base Class, Superclass, Parent Class
8
Inheritance Example
• Base Class
– Person Person
-Name
• Subclasses -Address
– Employee -Phone
– Customer
Properties
– Student
Employee Customer Student
9
Instantiating An Object
• Creating a new object based on a class
• Create an instance of the class by using the
New keyword
• General Form
New className( )
10
Polymorphism
• Methods having identical names but different
implementations
• Overloading
– Several argument lists for calling the method
– Example: MessageBox.Show method
• Overriding
– Refers to a class that has the same method
name as its base class
– Method in subclass takes precedence
11
Abstraction
• Definition: Simplifying complex systems by modeling
classes based on the essential properties and behaviors.
• Focuses on what an object does rather than how it does it.
• Example: A car class may expose methods like drive()
and stop(), hiding the complexity of how these actions
occur.
Benefits of Abstraction
● Reduces complexity by hiding unnecessary details.
● Enhances code maintainability.
● Allows programmers to work with high-level concepts.
12
Designing Your Own Class
• Analyze characteristics needed by new objects
– Characteristics or properties are defined as
variables
– Define the properties as variables in the
class module
• Analyze behaviors needed by new objects
– Behaviors are methods
– Define the methods as sub procedures or
functions
13
Class Methods
• Create methods by coding public procedures
within a class
• Methods declared with the Private keyword are
available only within the class
• Methods declared with the Public keyword are
available to external objects
14
Constructors and Destructors
• Constructor
– Method that automatically executes when
an object is instantiated
– Constructor must be public and is named
New
• Destructor
– Method that automatically executes when
an object is destroyed
15
Overloading Methods
• Overloading means that two methods have the
same name but a different list of arguments
(the signature)
• Create by giving the same name to multiple
procedures in your class module, each with a
different argument list
16
Parameterized Constructor
• Constructor that requires arguments
• Allows arguments to be passed when creating
an object
17
Create a New Class
• Project, Add Class
• In Add New Item dialog box, choose
Class
• Name the Class
• Define the Class properties
• To allow access from outside the class, add
property procedures
• Code the methods
18
Creating a New Object
Using a Class
• Similar to creating a new tool for the toolbox but
not yet creating an instance of the class
• Declare a variable for the new object
• Then, instantiate the object using the New keyword
Private aBookSale As BookSale
aBookSale = New BookSale( )
Or
Dim aBookSale As New Booksale( )
19
Creating a New Object
Using a Class (continued)
• If object variable is needed in multiple
procedures, declare the object at class level
• Instantiate the object
– Only when(if) it is needed
– Inside a Try/Catch block for error handling
(Try/Catch block must be inside a procedure)
• Pass values for the arguments at instantiation
when using a parameterized constructor
20
Inheritance
• New class can
– Be based on another class (base class)
– Inherit the properties and methods (but not
constructors) of the base class, which can be
• One of the VB existing classes
• Your own class
• Use the Inherits statement following the class
header and prior to any comments
21
Overriding Methods
• Methods with the same name and the same
argument list as the base class
• Derived class (subclass) will use the new method
rather than the method in the base class
• To override a method
– Declare the original method with the
Overridable keyword
– Declare the new method with the Overrides
keyword
22
Creating a Base Class Strictly for
Inheritance
• Classes can be created strictly for inheritance by
two or more similar classes and are never
instantiated
• For a base class that you intend to inherit from,
include the MustInherit modifier on the class
declaration
• In each base class method that must be
overridden, include the MustOverride modifier
and no code in the base class method
23