4 - Unit No 4 Ques-Ans

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

VB.NET UNIT NO.

4 OBJECT ORIENTED PROGRAMMING (QUESTION-ANSWER) 1

1) What is the use of MyClass and MyBase keyword in VB.NET?


The MyClass keyword behaves like an object variable referring to the current instance of a
class. MyClass is similar to Me.

Class Base
Public Overridable Sub PrintName()
Console.WriteLine("I’m Base")
End Sub

Public Sub ReallyPrintMyName()


MyClass.PrintName()
End Sub
End Class

Class Derived
Inherits Base
Public Overrides Sub PrintName()
Console.WriteLine("I’m Derived")
End Sub
End Class

' … Usage: '

Dim b As New Derived()


b.PrintName() ' Prints "I’m Derived" '
b.ReallyPrintMyName() ' Prints "I’m Base" '

The MyBase keyword behaves like an object variable referring to the base class of the current instance
of a class. MyBase is commonly used to access base class members that are overridden or shadowed
in a derived class. MyBase.New is used to explicitly call a base class constructor from a derived class
constructor.

Class Button
Public Overridable Sub Paint()
' Paint button here. '
End Sub
End Class

Class ButtonWithFancyBanner
Inherits Button

Public Overrides Sub Paint()


' First, paint the button. '
MyBase.Paint() // It will call Base class (Button) Paint Method
' Now, paint the banner. … '
End Sub
End Class
VB.NET UNIT NO. 4 OBJECT ORIENTED PROGRAMMING (QUESTION-ANSWER) 2

2) What is Inheritance? Explain types of inheritance in VB.NET.


Inheritance is the process of creating a new Class, called the Derived Class, from the existing class,
called the Base Class. The Inheritance has many advantages, the most important of them being the
reusability of code. Rather than developing new Objects from scratch, new code can be based on the
work of other developers, adding only the new features that are needed. The reuse of existing classes
saves time and effort.
However, inheritance may be implemented in different combinations in Object-Oriented Programming
languages as illustrated in figure and they include:

Different Types of Inheritance


 Single Inheritance
 Multi Level Inheritance
 Hierarchical Inheritance
 Hybrid Inheritance
 Multiple Inheritance

Single Inheritance

When a Derived Class to inherit properties and behavior from a


single Base Class, it is called as single inheritance.

Multi Level Inheritance

A derived class is created from another derived class is


called Multi Level Inheritance
VB.NET UNIT NO. 4 OBJECT ORIENTED PROGRAMMING (QUESTION-ANSWER) 3

Hierarchical Inheritance

More than one derived class are created from a


single base class, is called Hierarchical
Inheritance

Hybrid Inheritance

Any combination of above three inheritance (single,


hierarchical and multi level) is called as hybrid
inheritance.

Multiple Inheritance

when a derived class is created from more than one


base class then that inheritance is called as multiple
inheritance. But multiple inheritance is not supported
by .net using classes and can be done using
interfaces.
VB.NET UNIT NO. 4 OBJECT ORIENTED PROGRAMMING (QUESTION-ANSWER) 4

3) Explain Abstraction and Encapsulation in VB.NET.


Abstraction is selecting data from a larger pool to show only the relevant details to the object. It helps to
reduce programming complexity and effort. In VB.Net, abstraction is achieved using MustInherit
classes and interfaces.

 The purpose of abstract class is to provide default functionality to its sub classes.
 When a method is declared as abstract in the base class then every derived class of that class
must provide its own definition for that method.
 An abstract class can also contain methods with complete implementation, besides abstract
methods.
 When a class contains at least one abstract method, then the class must be declared
as abstract class.
 It is mandatory to override abstract method in the derived class.
 When a class is declared as abstract class, then it is not possible to create an instance for that
class. But it can be used as a parameter in a method.
 MustOrverride keyword is used in Method overriding in Base Class (Parent Class)
 Orverrides keyword is used in Method in Derived Class (Child Class)
MustInherit Class Person
Public Name As String

MustOverride Sub PrintName() //Here in Base (parent) class, only declaration is possible

Sub Print()
PrintName()
msgbox(Name)
End Sub
End Class

Class Customer
Inherits Person

Overrides Sub PrintName() // Here in Derived (Child) class, definition must be write.
msgbox(Name)
End Sub
End Class

Encapsulation is the exposure of properties and methods of an object while hiding the actual
implementation from the outside world. In other words, the object is treated as a black box—developers
who use the object should have no need to understand how it actually works.

Data Encapsulation is an Object Oriented Programming concept that bind a group of related
properties, functions, and other members are treated as a single unit. Class is the best example of Data
Encapsulation. It sometimes referred to as data hiding that prevents the user to access the
implementation details. Encapsulation therefore guarantees the integrity of the data contained in the
Object.
VB.NET UNIT NO. 4 OBJECT ORIENTED PROGRAMMING (QUESTION-ANSWER) 5

The whole idea behind the data encapsulation is to hide the implementation details from users. This is
achieved through the state (the private fields) and the behaviors (the public methods) of a Class.

4) Explain significance of Interface using example.


An interface is a description of the actions that an object can do... for example when you flip a light
switch, the light goes on, you don't care how, just that it does. In Object Oriented Programming, an
Interface is a description of all functions that an object must have in order to fulfill the functionality.

An interface is a programming structure/syntax that allows the computer to enforce certain properties on
an object (class). For example, say we have a car class and a scooter class and a truck class. Each of
these three classes should have a start_engine() action. How the "engine is started" for each vehicle is
left to each particular class, but the fact that they must have a start_engine action is the domain of the
interface.
 Interfaces specify what a class must do and not how. It is the blueprint of the class.
 If a class implements an interface and does not provide method bodies for all functions specified in
the interface, then the class must be declared abstract.
 Interfaces are better suited to situations in which your applications require many possibly unrelated
object types to provide certain functionality.
 Interfaces are more flexible than base classes because you can define a single implementation that
can implement multiple interfaces.
 Interfaces are useful when you cannot use class inheritance. For example, structures cannot inherit
from classes, but they can implement interfaces.

Example
Public Interface Test
Sub Show_A(ByVal str As String)
Property Name() As String
Function Add(ByVal a As Int16, ByVal b As Int16) As Int16
End Interface

Public Class cls_interface_test


Implements Test

Public Function Add(ByVal a As Short, ByVal b As Short) As Short Implements Test.Add


Return a+b
End Function

Public Property Name As String Implements Test.Name


Get

End Get
Set(ByVal value As String)

End Set
End Property

Public Sub Show_A(ByVal str As String) Implements Test.Show_A


Msgbox (Str)
VB.NET UNIT NO. 4 OBJECT ORIENTED PROGRAMMING (QUESTION-ANSWER) 6

End Sub
End Class

5) Explain Polymorphism in VB.NET.


 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.
 It allows you to invoke methods of derived class through base class reference during runtime.
 It has the ability for classes to provide different implementations of methods that are called through
the same name.

Method Overloading ( Compile Time / Design Time / Static Polymorphism):


 Method with same name but with different arguments is called method overloading.
 Method Overloading forms compile-time polymorphism.

Private Overloads Sub ShowSum(ByVal No1 as int16,


ByVal No2 as int16, ByVal No3 as int16)

msgbox(No1 + No2 + No3)

End Sub

Private Overloads Sub ShowSum (ByVal No1 as int16,ByVal No2 as int16)

msgbox(No1 + No2)

End Sub

Call ShowSum (5,10,7)


Call ShowSum (15,5)

Method Overriding (Runtime / Dynamic Polymorphism) :


 Method overriding occurs when child class declares a method that has the same type arguments as
a method declared by one of its super class.
 Overridable keyword is used in Method overriding in Base Class (Parent Class)
 Orverrides keyword is used in Method in Derived Class (Child Class) to achieve
runtime polymorphism.
 Method overriding forms Run-time polymorphism.
 Note: By default functions are not virtual in C# and so you need to write “virtual” explicitly. While by
default in Java each function are virtual.
VB.NET UNIT NO. 4 OBJECT ORIENTED PROGRAMMING (QUESTION-ANSWER) 7

Example
Class Over
Public Overridable Function add(ByVal x As Integer, ByVal y As Integer)
msgbox (x + y)
End Function
End Class

Class DerOver
Inherits Over

Public Overrides Function add(ByVal x As Integer, ByVal y As Integer)


msgbox(MyBase.add(120, 100)) // call Base class add Function
msgbox (x + y)
End Function
End Class

Sub Main()
Dim obj As New DerOver
obj.add(10, 100)
End Sub

6) Explain Constructor and Destructor in VB.NET.


Constructors used in a class are member functions to initialize or set the objects of a class in VB.net.
They dont return any value and are defined in a Sub with a keyword New.
Multiple constructors can be created in class with any access specifiers, by default constructors are
of Public access type.

Example
Public Class Sample
Private a As Integer
Public Sub New(ByVal setval As Integer) Constructor
a = setval
End Sub
Public Function disp()
msgbox(a)
End Function
End Class

Sub Main()
Dim d As New Sample(5)
d.disp() '//Ans. 5
End Sub
VB.NET UNIT NO. 4 OBJECT ORIENTED PROGRAMMING (QUESTION-ANSWER) 8

Destructor
Destructor is a function that use to de-initialize Object when they are destroyed. You can think of
destructors as the opposite of constructors: constructors execute when objects are created, and
destructors execute when the objects are destroyed by the built in Garbage Collection facility. This
process occurs behind the scenes with no consequence to the programmer.
To implement Destructor, we need to create Finilize Method inside class and it should be declare
with Overrides.
Example
Class Line
Private length As Double ' Length of a line
Public Sub New()
msgbox("Object is being created")
End Sub

Protected Overrides Sub Finalize() Destructor


msgbox("Object is being deleted")
End Sub

Public Sub setLength(ByVal len As Double)


length = len
End Sub

Public Function getLength() As Double


Return length
End Function

Shared Sub Main()


Dim ObjLine As New Line()
ObjLine.setLength(6.0) '//'set line length
msgbox(ObjLine.getLength())
End Sub
End Class

7) Explain Access Specifiers in VB.NET.


AccessSpecifiers describes as the scope of accessibility of an Object and its members. We can control
the scope of the member object of a class using access specifiers. We are using access specifiers for
providing security of our applications.
VB.NET UNIT NO. 4 OBJECT ORIENTED PROGRAMMING (QUESTION-ANSWER) 9

Visual Basic .Net provide five access specifiers , they are as follows :

 Public
 Private
 Protected
 Friend
 ProtectedFriend

Public : Public is the most common access specifier. It can be access from anywhere, hat means there
is no restriction on accessibility. The scope of the accessibility is inside class also in outside the class.

Classes and class members marked with Public access modifier are available in the same class, all the
classes from the same project and to all other projects as well. This access specifier is least restrictive.

Private : The scope of the accessibility is limited only inside the classes in which they are decleared.
The Private members can not be accessed outside the class and it is the least permissive access level.

Protected : Private access modifier allows us to hide members from others but what if some one is
inheriting from your class? In many cases you want that members of base class should be available in
derived class. This cannot be achieved with private modifier. Protected access specifier provide such
access. The members marked with protected access modifier can be accessed in the same class and
all the classes inherited from it but they are not available to other classes.

Friend : The Friend access specifier can access within the program that contain its declarations and
also access within the same assembly level. You can use friend instead of Dim keyword.

Now going one step further let us assume that you want that all the classes from your project should be
able to access to your class members but classes external to your project should not be able to do so.
In this case neither private nor protected can help. Only your Friend can help you out. You guessed it!
The friend access modifier is used to declare your class members such that any class from the same
project will be able to access them but external classes cannot.

Example

Assembly1.dll

Public Class Class1


Friend age As Integer
'... other code
End Class

Public Class Class2


Public Sub SomeMethod()
Dim x As New Class1()
x.age = 99 'OK
End Sub
End Class
VB.NET UNIT NO. 4 OBJECT ORIENTED PROGRAMMING (QUESTION-ANSWER) 10

Assembly2.dll

Public Class Class3


Public Sub SomeOtherMethod()
Dim x As New Class1()
x.age = 99 'ERROR
End Sub
End Class

When applied to class the class will be available only in the project in which it is declared.

Protected Friend : Protected Friend access modifier is a combination of protected and friend access
modifiers and allows access to class members in the same project and all the inherited types.

VISIBILITY

SAME
SAME DERIVED USING OTHER
ASSEMBLY
CLASS CLASS OBJECT ASSEMBLY
USING OBJECT
Private YES NO NO NO NO

Protected YES YES NO NO NO

Friend YES YES YES YES NO

Protected friend YES YES YES YES NO

YES (USING
Public YES YES YES YES
OBJECT ONLY)

8) What is overloading? How to achieve it in VB.Net?


Refer Answer of Question No. 5

9) What is overriding? List out and explain keyword used for Overriding
Refer Answer of Question No. 5
VB.NET UNIT NO. 4 OBJECT ORIENTED PROGRAMMING (QUESTION-ANSWER) 11

10) Difference between Abstract class and Interface

Sr. Abstract class Interface


An abstract class cannot support multiple an interface can support multiple inheritance.
1
inheritance,
An interface is an empty shell, just only the An Abstract class is a class which will
signatures of the methods. The methods do contains both definition and implementation in
2
not contain anything. The interface can't do it.
anything.
Abstract classes can have consts, members, interfaces can only have consts and methods.
3
method stubs and defined methods
Various access modifiers such as private, all methods of an interface must be defined
4 protected, internal, public, etc. are useful in as public.
abstract Classes
A child class can define abstract methods a class implementing an interface must define
5
with the same or less restrictive visibility. the methods with the exact same visibility.
Abstract class can have abstract and non- Interface can have only abstract methods.
6
abstract methods.
Abstract class can provide the Interface can't provide the implementation of
7
implementation of interface. abstract class.

You might also like