Você está na página 1de 9

NAME: JIMOH HASSAN A.

MATRIC NUMBER: F/HD/14/3210137


COURSE: DATABASE MANAGEMET II
COURSE CODE: COM 322
OBJECT ORIENTED PARADIGM VISUAL BASIC.NET
There are three widely used programming paradigms there: Procedural
programming, functional programming and object-oriented programming.
Visual Basic supports both procedural and object-oriented programming.
Object-oriented programming (OOP) is a programming paradigm that uses
objects and their interactions to design applications and computer
programs. (Wikipedia)
There are some basic programming concepts in OOP:
Abstraction
Polymorphism
Encapsulation
Inheritance
The abstraction is simplifying complex reality by modeling classes
appropriate to the problem. The polymorphism is the process of using an
operator or function in different ways for different data input. The
encapsulation hides the implementation details of a class from other
objects. The inheritance is a way to form new classes using classes that
have already been defined.
OBJECTS
Objects are basic building blocks of a Visual Basic OOP program. An object
is a combination of data and methods. In a OOP program, we create
objects. These objects communicate together through methods. Each
object can receive messages, send messages and process data.
There are two steps in creating an object. First, we create a class. A class
is a template for an object. It is a blueprint, which describes the state and
behavior that the objects of the class all share. A class can be used to
create many objects. Objects created at runtime from a class are called
instances of that particular class.
Option Strict On
Module Example
Class Being
End Class
Sub Main()
Dim b as New Being

Console.WriteLine(b.ToString())
End SubEnd Module

METHODS

Methods are functions/procedures defined inside the body of a class. They


are used to perform operations with the attributes of our objects. Methods
are essential in encapsulation concept of the OOP paradigm. For example,
we might have a Connect method in our AccessDatabase class. We need
not to be informed how exactly Connect connects to the database. We only
know that it is used to connect to a database. This is essential in dividing
responsibilities in programming, especially in large applications.

Option Strict On
Module Example
Class Circle
Public Radius As Integer
Public Sub SetRadius(ByVal Radius As Integer)
Me.Radius = Radius
End Sub
Public Function Area() As Double
Return Me.Radius * Me.Radius * Math.PI
End Function
End Class
Sub Main()
Dim c As New Circle
c.SetRadius(5)
Console.WriteLine(c.Area())
End Sub
End Module

A constructor is a special kind of a method. It is automatically called, when


the object is created. The purpose of the constructor is to initiate the state

of the object. The name of the constructor in Visual Basic is New. The
constructors are methods, so they can be overloaded too.
Option Strict On
Module Example
Class Being
Sub New()
Console.WriteLine("Being is being created")
End Sub
Sub New(ByVal name As String)
Console.WriteLine("Being {0} is created", name)
End Sub
End Class
Sub Main()
Dim b As New Being
Dim t As New Being("Tom")
End Sub
End Module
INHERITANCE
The inheritance is a way to form new classes using classes that have
already been defined. The newly formed classes are called derived
classes; the classes that we derive from are called base classes. Important
benefits of inheritance are code reuse and reduction of complexity of a
program. The derived classes (descendants) override or extend the
functionality of base classes (ancestors).
Option Strict On
Module Example
Class Being
Sub New()
Console.WriteLine("Being is created")
End Sub
End Class
Class Human
Inherits Being

Sub New()
Console.WriteLine("Human is created")
End Sub
End Class
Sub Main()
Dim h As New Human
End Sub
End Module
In this program, we have two classes. A base Being class and a derived
Human class. The derived class inherits from the base class.
Member Functions and Encapsulation
A member function of a class is a function that has its definition or its
prototype within the class definition like any other variable. It operates on
any object of the class of which it is a member and has access to all the
members of a class for that object.
Member variables are attributes of an object (from design perspective)
and they are kept private to implement encapsulation. These variables
can only be accessed using the public member functions.
Let us put above concepts to set and get the value of different class
members in a class:
Modulemybox
ClassBox
Public length AsDouble' Length of a box
Public breadth As Double 'Breadth of a box
Public height AsDouble' Height of a box
Public Sub setLength(ByVallen As Double)
length = len
End Sub
Public Sub setBreadth(ByValbre As Double)
breadth = bre
End Sub
Public Sub setHeight(ByValhei As Double)
height = hei

End Sub
Public Function getVolume() As Double
Return length * breadth * height
End Function
End Class
Sub Main()
Dim Box1 As Box = New Box()

'DeclareBox1 of type Box

DimBox2AsBox=NewBox()' Declare Box2 of type Box


Dim volume As Double = 0.0

'Store the volume of a box here

' box 1 specification


Box1.setLength(6.0)
Box1.setBreadth(7.0)
Box1.setHeight(5.0)
'box2 specification
Box2.setLength(12.0)
Box2.setBreadth(13.0)
Box2.setHeight(10.0)
' volume of box 1
volume = Box1.getVolume()
Console.WriteLine("Volume of Box1 : {0}", volume)
'volume of box 2
volume=Box2.getVolume()
Console.WriteLine("Volume of Box2 : {0}", volume)
Console.ReadKey()
EndSub
EndModule
When the above code is compiled and executed, it produces the following
result:
Volume of Box1:210

Volume of Box2:1560

ABSTRACT CLASSES AND METHODS

Abstract classes cannot be instantiated. If a class contains at least one


abstract method, it must be declared abstract too. Abstract methods
cannot be implemented; they merely declare the methods' signatures.
When we inherit from an abstract class, all abstract methods must be
implemented by the derived class. Furthermore, these methods must be
declared with the same of less restricted visibility. Unlike Interfaces,
abstract classes may have methods with full implementation and may also
have defined member fields. So abstract classes may provide a partial
implementation. Programmers often put some common functionality into
abstract classes. And these abstract classes are later sub classed to
provide more specific implementation. For example, the Qt graphics library
has a QAbstractButton, which is the abstract base class of button widgets,
providing functionality common to buttons. Buttons Q3Button, QCheckBox,
QPushButton, QRadioButton, and QToolButton inherit from this base
abstract class.
Formally put, abstract classes are used to enforce a protocol. A protocol is
a set of operations, which all implementing objects must support.
Option Strict On
Module Example
MustInherit Class Drawing
Protected x As Integer = 0
Protected y As Integer = 0
Public MustOverride Function Area() As Double
Public Function GetCoordinates() As String
Return String.Format("x: {0}, y: {1}", _
Me.x, Me.y)
End Function
End Class
Class Circle
Inherits Drawing
Private Radius As Integer
Sub New(ByVal x As Integer, ByVal y As Integer, _
ByVal r As Integer)

Me.x = x
Me.y = y
Me.Radius = r
End Sub
Public Overrides Function Area() As Double
Return Me.Radius * Me.Radius * Math.PI
End Function
Public Overrides Function ToString() As String
Return String.Format("Circle, at x: {0}, y: {1}, radius: {2}", _
Me.x, Me.y, Me.Radius)
End Function
End Class
Sub Main()
Dim c as New Circle(12, 45, 22)
Console.WriteLine(c)
Console.WriteLine("Area of circle: {0}", c.Area())
Console.WriteLine(c.GetCoordinates())
End Sub
End Module
Polymorphism
Polymorphism allows objects to be represented in multiple forms. Even
though classes are derived or inherited from the same parent class, each
derived class will have its own behavior. Polymorphism is a concept linked
to inheritance and assures that derived classes have the same functions
even though each derived class performs different operations.Figure 2
shows a Rectangle, a Circle, and Square. All of them are shapes and as
shapes their area and perimeter can be calculated; however, each shape
calculates its area in a specialized way. Declaring a member as abstract
allows polymorphism. The Shape class defines the CalculateArea() and
CalculatePerimeter() methods as abstract, this allows each derived class to
override the implementation of the parent's methods.To following sample
code shows an implementation of a derived class (rectangle). The specific
CalculateArea() and CalculatePerimeter() methods for the rectangle class
illustrate polymorphism:
Code
1. using System;

2. namespace DotNetTreats.OOSE.OOPSamples
3. {
4.

class Rectangle: Shape

5.

6.

private float _height;

7.

private float _width;

8.

public rectangle(float height, float width)

9.

10.

_height = height;

11.

_width = width;

12.

13.

public float Height

14.

15.

get

16.

17.

return _height;

18.

19.

set

20.

21.

_height = value;

22.

23.

24.

public float Width

25.

26.

get

27.

28.

return _width;

29.

30.

set

31.

32.

_width = value;

33.

34.

35.

public override void CalculateArea()

36.

37.

this.Area = _height * _width;

38.

39.

public override void CalculatePerimeter()

40.

41.

this.Perimeter = (_height * 2) + (_width * 2);

42.

43.
44.

}
}

Você também pode gostar