Unit 3 Oops in
Unit 3 Oops in
Concepts of OOP:
● Objects
● Classes
● Data Abstraction and Encapsulation
● Inheritance
● Polymorphism
Objects
Objects are the basic run-time entities in an object-oriented system. Programming
problem is analyzed in terms of objects and nature of communication between them.
When a program is executed, objects interact with each other by sending messages.
Different objects can also interact with each other without knowing the details of their
data or code.
Classes
A class is a collection of objects of similar type. Once a class is defined, any number
of objects can be created which belong to that class.
In Visual Basic we create a class with the Class statement and end it with End Class.
The Syntax for a Class looks as follows:
To create a object for this class we use the new keyword and that looks like this:
FIELDS, PROPERTIES, METHODS, AND EVENTS ARE MEMBERS OF THE CLASS. THEY CAN BE DECLARED AS
PUBLIC, PRIVATE, PROTECTED, FRIEND OR PROTECTED FRIEND
Abstraction refers to the act of representing essential features without including the
background details or explanations.
● Classes use the concept of abstraction and are defined as a list of abstract
attributes.
● Storing data and functions in a single unit is encapsulation.
● Data cannot be accessible to the outside world and only those functions which
are stored in the class can access it.
CONTRUCTORS
A CONSTRUCTOR IS A SPECIAL FUNCTION WHICH IS CALLED AUTOMATICALLY WHEN A CLASS IS
CREATED. WHEN YOU CREATE AN OBJECT, YOU MIGHT WANT TO CUSTOMIZE THAT OBJECT WITH
DATA—FOR EXAMPLE, WHEN YOU CREATE AN OBJECT OF A CLASS NAMED EMPLOYEE, YOU MIGHT WANT
TO STORE THE EMPLOYEE'S NAME, PHONE NUMBER, ID NUMBER, AND SO ON IN THAT OBJECT.
NOW I CAN STORE THE VALUE 5 INSIDE AN OBJECT OF THE DATACLASS CLASS SIMPLY BY PASSING 5 TO
THE CONSTRUCTOR, AND I CAN RETRIEVE THAT VALUE WITH THE GETDATA METHOD:
MSGBOX(DATA.GETDATA())
DESTRUCTORS
DESTRUCTORS ARE USED TO CLEAN UP AFTER AN OBJECT (DEALLOCATING RESOURCES, FOR EXAMPLE, OR
INFORMING OTHER OBJECTS THAT THE CURRENT OBJECT WILL NO LONGER BE AVAILABLE).
FINALIZE IS CALLED AUTOMATICALLY BY THE SYSTEM WHEN AN OBJECT IS DESTROYED (WHICH MEANS THAT
YOU SHOULD NOT EXPLICITLY CALL FINALIZE YOURSELF).
THE .NET FRAMEWORK AUTOMATICALLY RUNS THE FINALIZE DESTRUCTOR AND DESTROYS OBJECTS WHEN THE
SYSTEM DETERMINES THAT SUCH OBJECTS ARE NO LONGER NEEDED.
Inheritance
Inheritance is the process by which objects can acquire the properties of objects of
other class.
THE PROCESS OF DERIVING A NEW CLASS FROM AN EXISTING CLASS IS CALLED INHERITANCE. THE OLD CLASS IS
CALLED THE BASE CLASS AND THE NEW CLASS IS CALLED DERIVED CLASS. THE DERIVED CLASS INHERITS SOME
OR EVERYTHING OF THE BASE CLASS.
IN VISUAL BASIC WE USE THE INHERITS KEYWORD TO INHERIT ONE CLASS FROM OTHER.
THE GENERAL FORM OF DERIVING A NEW CLASS FROM AN EXISTING CLASS LOOKS AS FOLLOWS:
Polymorphism
Polymorphism means the ability to take more than one form. An operation may
exhibit different behaviors in different instances. The behavior depends on the data
types used in the operation. Polymorphism is extensively used in implementing
Inheritance.
Module Module1
Sub Main()
Dim two As New One()
WriteLine(two.add(10))
'calls the function with one argument
WriteLine(two.add(10, 20))
'calls the function with two arguments
WriteLine(two.add(10, 20, 30))
'calls the function with three arguments
Read()
End Sub
End Module
Public Class One
Public i, j, k As Integer