4 - Unit No 4 Ques-Ans
4 - Unit No 4 Ques-Ans
4 - Unit No 4 Ques-Ans
Class Base
Public Overridable Sub PrintName()
Console.WriteLine("I’m Base")
End Sub
Class Derived
Inherits Base
Public Overrides Sub PrintName()
Console.WriteLine("I’m Derived")
End Sub
End Class
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
Single Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Multiple Inheritance
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.
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
End Get
Set(ByVal value As String)
End Set
End Property
End Sub
End Class
End Sub
msgbox(No1 + No2)
End Sub
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
Sub Main()
Dim obj As New DerOver
obj.add(10, 100)
End Sub
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
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
Assembly2.dll
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
YES (USING
Public YES YES YES YES
OBJECT ONLY)
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