0% found this document useful (0 votes)
64 views17 pages

Assignment 3

This document contains an assignment submission for a VB.NET class. It includes questions and answers about classes, properties, events, constructors, inheritance, access modifiers, interfaces, polymorphism, and exceptions. The submission contains the student's name, roll number, and answers to 21 multiple choice and short answer questions about key concepts in object-oriented programming and VB.NET classes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
64 views17 pages

Assignment 3

This document contains an assignment submission for a VB.NET class. It includes questions and answers about classes, properties, events, constructors, inheritance, access modifiers, interfaces, polymorphism, and exceptions. The submission contains the student's name, roll number, and answers to 21 multiple choice and short answer questions about key concepts in object-oriented programming and VB.NET classes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 17

Assignment No.

3
Name -: Aniket shrikrushna waghode
Roll No- 101
Subject -: VB.NET

Q 1. Answer the following?(Each carry 2 marks)


1. What is Class?
▪ A class definition starts with the keyword Class followed by the class name; and the class
body, ended by the End Class statement.

2. Write the declaration syntax of Class?


▪ [ <attributelist> ] [ accessmodifier ] [ Shadows ] [ MustInherit | NotInheritable ] [ Partial ] _
Class name [ ( Of typelist ) ]
[ Inherits classname ]
[ Implements interfacenames ]
[ statements ] End
Class

3. What is property member in a class?


▪ In VB.NET, properties are nothing but natural extension of data fields. They are
usually known as 'smart fields' in VB.NET community. We know that data
encapsulation and hiding are the two fundamental characteristics of any object
oriented programming language. In VB.NET, data encapsulation is possible
through either classes or structures. By using
various access modifiers like private, public, protected, internal etc it is possible to
control the accessibility of the class members.
4. What is mean by an event of a class?
▪ events in a class built in VB.NET is done using Event specification.
Defining event involves assigning the name and arguments providing.
... Event message is sent to the entity in which it was defined. For example, a
derived class cannot exchange messages related
to events inherited from base class.
5. Write syntax of property member of a class?
▪ class Pen
{
private int color; // private field
// public property
public int Color
{
get
{
return this.color;
} set
{
if (value > 0) {
this.color = value;
}
}
}
}
A property, in some object-oriented programming languages, is a special sort of
class member, intermediate in functionality between a field (or data member) and
a method. The syntax for reading and writing of properties is like for fields, but
property reads and writes are (usually) translated to 'getter' and 'setter' method
calls.

6. What is WriteOnly property?


▪ f you had a situation in which you wanted to write to a member variable but not
make its property part of the class interface, you would use the
WriteOnly keyword. For example, an oven might track the total elapsed time that it
has been in use but not make that information available through the interface. (Such
information might be useful in
product-life studies, but you wouldn't want the user to have access to the
data.) In that case, you might use something like this:
Public WriteOnly Property SetHours() Set(ByVal Value) End Set End Property

7. What is ReadOnly property?


▪ The ReadOnly and WriteOnly Properties
There may be times when you would like to have a property that can only
be read or that can only be written. For example, you might have a property
named mSerialNumber that holds the serial number of an oven. (This is not as
farfetched as it might seem because many modern ovens are controlled by
microprocessors.) There is no reason you would want the user to be able to write
a new serial number. You could make the property read-only by using the
following code fragment:
Public ReadOnly Property GetSerialNumber() Get End Get End Property

8. What is mean by Raising an event of a class? ▪ You can add code


to raise an event by writing RaiseEvent followed by the event name and arguments
anywhere you want to. However, this makes your code look a bit sloppy, and if you
need to create arguments for your events or program in other languages then the
sloppiness is exacerbated. As an alternative we can employ a convention that works
very well in languages like Visual Basic
.NET, C#, C, C++, and Delphi too

9. What is a constructor?
▪ A constructor in VB.NET is defined as a procedure that has the name New
(rather than Initialize as in VB 6.0) and can accept arguments to allow clients
to pass data into the instance to assist with initialization. Constructors do not
return values and therefore are always
declared as a Sub.

10. List types of Constructors?


▪ Default Constructor.
▪ Parameterized Constructor.
▪ Copy Constructor.
▪ Private Constructor.

11. What is a destructor?


▪ Destructor } A Destructor is a special function which is called automatically when
a class is destroyed. In VB.NET, you should use Finalize() routine to create
Destructors. } A destructor is a special member
Sub of a class that is executed whenever an object of its class goes out of scope.
12. What is method overloading?
▪ Overloading in general refers to creating multiple procedures with the same name
that accept different argument types in a given class. You can use the Overloads
keyword to declare a property or a method with the same name but with a
different argument list
13. Define Inheritance?
▪ inheritance is the idea that one class, called a subclass, can be based on
another class, called a base class. Implementation inheritance is the object-
oriented feature that supports IsA relationships. ... Containment supports HasA
relationships.

14. List types of inheritance?


o Single Inheritance.
o Multi Level Inheritance.
o Hierarchical Inheritance.
o Hybrid Inheritance.
o Multipath inheritance. o
Multiple Inheritance.

15. What is the use of NotInheritable keyword? ▪ “when it will not be [


] necessary to create derived classes” 11 and also when your class consists of
nothing but shared methods and properties.

16. What is the use of MustInherit keyword? ▪ The MustInherit


keyword specifies that a class is to be used only as a template. It cannot be used
directly. We create new classes, with the Inherits keyword, that use this template.

17. What is Method Overriding?


▪ Method overriding gives a programmer the ability to write an improved version of
the method of the base class without affecting the message passed by the client
programs, which uses the derived class. The ability to override has been included
in VB.NET.
18. What is the use of MyBase keyword?
▪ 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.

19. What is the use of MyClass keyword?


▪ MyClass is called from the parent class. You use this when you want to refer to a
member of that same parent class, and ignore whether the parent member might
be overridden by a child member.
20. List types of access modifiers?
▪ Public: No access restrictions.
▪ Protected: Access limited to containing/derived classes.
▪ Private: Access limited to containing type.
▪ Friend: Access limited to the current project.
▪ Protected Friend: Access limited to containing/derived classes and the current project.

21. What is an Interface?


▪ Interfaces define the properties, methods, and events that classes can implement. ...
Interfaces are useful when you cannot use class inheritance. For example, structures
cannot inherit from classes, but they can implement interfaces.

22. List types of polymorphism?


▪ There are two types of polymorphism: Static or compile time polymorphism.
Dynamic or runtime polymorphism.

23. List predefined exception classes?


▪ Exceptions provide a way to transfer control from one part of a program to another. VB.Net
exception handling is built upon four keywords - Try, Catch, Finally and Throw.

• Try − A Try block identifies a block of code for which particular exceptions will be

activated. It's followed by one or more Catch blocks.

• Catch − A program catches an exception with an exception handler at the place in a program

where you want to handle the problem. The Catch keyword indicates the catching of an

exception.

• Finally − The Finally block is used to execute a given set of statements, whether an

exception is thrown or not thrown. For example, if you open a file, it must be closed
whether an exception is raised or not.

• Throw − A program throws an exception when a problem shows up. This is

done using a Throw keyword.

24. Give the name of the class used to derived user defined
exception?
▪ You can also define your own exception. User-defined exception classes are derived from
the ApplicationException class.

Q 2. Answer the following?(Each carry 4 marks)


1. Explain the property of the class with proper syntax and
example?
Class Definition

A class definition starts with the keyword Class followed by the class name; and the class

body, ended by the End Class statement. Following is the general form of a class

definition −

[ <attributelist> ] [ accessmodifier ] [ Shadows ] [ MustInherit | NotInheritable ] [ Partial ]


_
Class name [ ( Of typelist ) ]
[ Inherits classname ]
[ Implements interfacenames ]
[ statements ] End
Class
Class Example :

Imports System
Module Module1
Class Figure
Public length As Double
Public breadth As Double
End Class
Sub Main()
Dim Rectangle As Figure = New Figure()
Dim area As Double = 0.0
Rectangle.length = 8.0
Rectangle.breadth = 7.0
area = Rectangle.length * Rectangle.breadt h
Console.WriteLine("Area of Rectangle is : {0}", area)
Console.ReadKey()
End Su b
End Modul e
2. Explain the types of property of a class with example? The
types of property of class are :
1) The ReadOnly Property
2) The WriteOnly Property

▪ The ReadOnly Property


There may be times when you would like to have a property that can only be read
or that can only be written. For example, you might have a property named
mSerialNumber that holds the serial number of an oven. (This is not as farfetched
as it might seem because many modern ovens are controlled by microprocessors.)
There is no reason you would want the user to be able to write a new serial
number. You could make the property read-only by using the following code
fragment:
Public ReadOnly Property GetSerialNumber() Get End Get End Property ▪
EXAMPLE :
Class employee
' Only code inside class employee can change the value of hireDateValue.
Private hireDateValue As Date
' Any code that can access class employee can read property dateHired.
Public ReadOnly Property dateHired() As Date
Get
Return hireDateValue
End Get
End Property
End Class
▪ The WriteOnly Property f you had a situation in which you wanted to write to a
member variable but not make its property part of the class interface, you
would use the WriteOnly keyword. For example, an oven might track the total
elapsed time that it has been in use but not make that information available
through the interface. (Such information might be useful in product-life
studies, but you wouldn't want the user to have access to the data.) In that
case, you might use something like this:
Public WriteOnly Property SetHours() Set(ByVal Value) End Set End Property
▪ EXAMPLE :
Class employee
' Only code inside class employee can change the value of hireDateValue.
Private hireDateValue As Date
' Any code that can access class employee can read property dateHired.
Public WriteOnly Property dateHired() As Date
Get
Return hireDateValue
End Get
End Property End
Class

3. Explain an event of a class with an example?


The Event classes represent the event. Java provides us various Event classes but we will
discuss those which are more frequently used.

EventObject class

It is the root class from which all event state objects shall be derived. All Events are
constructed with a reference to the object, the source, that is logically deemed to be the
object upon which the Event in question initially occurred upon.This class is defined in
java.util package. Class declaration

Following is the declaration for java.util.EventObject class:


public class EventObject extends
Object
implements Serializable
Field

Following are the fields for java.util.EventObject class:


protected Object source -- The object on which the Event initially
occurred. Class constructors
S.N. Constructor & Description
1
EventObject(Object source)

Constructs a prototypical Event.

Class methods

S.N. Method & Description


1
Object getSource()

The object on which the Event initially occurred.

2
String toString()

Returns a String representation of this EventObject.

4. Explain constructor with an example?


▪ When a class or struct is created, its constructor is called. Constructors have the same name as
the class or struct, and they usually initialize the data members of the new object.
In the following example, a class named Taxi is defined by using a simple constructor. This class
is then instantiated with the new operator. The Taxi constructor is invoked by the new operator
immediately after memory is allocated for the new object.
C# Copy public
class Taxi
{
public bool IsInitialized; public
Taxi()
{
IsInitialized = true;
}
}
class TestTaxi
{
static void Main()
{
Taxi t = new Taxi();
Console.WriteLine(t.IsInitialized);
}
}
A constructor that takes no parameters is called a parameterless constructor.
Parameterless constructors are invoked whenever an object is instantiated by using the new
operator and no arguments are provided to new. For more information, see Instance
Constructors.
5. Discuss Method Overloading with an example? Method
Overloading allows us to write different versions of a method (i.e. a single class can contain
more than one methods (Sub / Functions) of same name but of different implementation).
And compiler will automatically select the appropriate method based on parameters passed.

Example :
Imports System.Console
Module Module1
Public Class demo
Public Overloads Function add(ByVal a As Integer, ByVal b As
Integer) WriteLine("You are in function add(integer, integer)")
Return a + b
End Function
Public Overloads Function add(ByVal a As Long, ByVal b As
Long) WriteLine("You are in function add(long, long)")
Return a + b
End Function
End Class
Sub Main()
Dim obj As New demo
WriteLine(obj.add(2147483640, 4))
WriteLine(obj.add(2147483648, 1))
WriteLine("press return to exit...")
Read()
End Sub
End Module
6. What is Inheritance? Explain with proper syntax and example?
▪ Inheritance is the idea that one class, called a subclass, can be based on another class,
called a base class. Inheritance provides a mechanism for creating hierarchies of
objects.

WriteLine("Hello World”

cl

lc

how methods are overridden?


In visual basic, Method Overriding means override a base class method in the derived
class by creating a method with the same name and signatures to perform a different task.
The Method Overriding in visual basic can be achieved by using Overridable & Overrides
keywords along with the inheritance principle. ▪ The following modifiers are used to
control how properties and methods Are overridden o Overridable : Allows a Property
or method in a class to be overridden in a derived class
o Overrides : overrides an overriadable property or method defined in The
base class
o Nonoverriadable : Prevents a property or method from being overridden in
an inheriting class
o Mustoverride : Reuires that a derived class override the property Or
method

8. Explain the use MyBase keyword with an example?

▪ Providesa reference to the base class from within a derived class. If you
want to call a member of the base class from within
a derived class, you can use the syntax:

MyBase.MemberName EXAMPLE :
Public Class CTestClass
...
End Class

Public Class CTestClass2


Inherits CTestClass

Public Function ShowType( ) As Type


Return Mybase.GetType End
Function
End Class

9. Explain the use MyClass keyword with an example


? MyClass is a reference to the class in which the keyword is used.
Example

The following code defines a class, Class1, and a derived class, Class1Derived, each of which has
an IncSalary method.

Public Class Class1


Public Overridable Function IncSalary(ByVal sSalary As Single) _
As Single
IncSalary = sSalary * CSng(1.1)
End Function

Public Sub ShowIncSalary(ByVal sSalary As Single)


MsgBox(Me.IncSalary(sSalary))
MsgBox(MyClass.IncSalary(sSalary))
End Sub
End Class

Public Class Class1Derived


Inherits Class1
Public Overrides Function IncSalary(ByVal sSalary As Single) As Single
IncSalary = sSalary * CSng(1.2)

End Function
End Class

10. Explain use of access modifiers used in VB.NET?


enerally, in visual basic only one access modifier is allowed to use with any
member or type, except when we use Protected Friend combination.

In visual basic, we are not allowed to use any access modifiers on namespaces, because
the namespaces have no access restrictions.

Only certain access modifiers are allowed to specify based on the context in which the
member declaration occurs. In case, if we didn’t mention any access modifiers during the
member declaration, then the default access modifiers will be used depending on the
member declaration context.

For example, the top-level types which are not nested in any other types can only have
Public or Friend accessibility. The default accessibility for top-level types is Friend.
11. Write a note on Polymorphism?
Polymorphism is the concept that different objects have different implementations of the
same characteristic. but the implementations of the methods could be drastically different.
Polymorphism sounds a great deal like encapsulation, but it is different.
Encapsulation has to do with hiding the internal implementation of an object.
Polymorphism has to do with multiple classes having the same interface.
Visual Basic introduced a very limited support for polymorphism in VB 4.0 through the
use of late binding. Late binding is the technology for code to determine at runtime what
properties and methods a given object provides. This allows code to create any object and
attempt to call a method or property of that object without knowing whether the object
supports the method at compile time. Given two different classes with the same properties
and methods, a variable of type object could be used to instantiate an object of either class.
This worked, but did not provide any type safety at compile time. Type safety is the
concept to ensure that a string is used as a string, an integer is used as an integer, and a
Boolean is used as a Boolean. Without type safety, potentially more errors can occur when
a user is executing a program rather than when the program is compiled. Type safety also
is one mechanism to prevent hackers from overloading code with a destructive piece of
code.

12. What is Exception? Explain various predefined exceptions used


in VB.NET?
▪ An exception is a problem that arises during the execution of a program. An exception is a
response to an exceptional circumstance that arises while a program is running, such as an
attempt to divide by zero.
▪ Exceptions provide a way to transfer control from one part of a program to another. VB.Net
exception handling is built upon four keywords - Try, Catch, Finally and Throw.
▪ Try − A Try block identifies a block of code for which particular exceptions will be

activated. It's followed by one or more Catch blocks.

▪ Catch − A program catches an exception with an exception handler at the place in a

program where you want to handle the problem. The Catch keyword indicates the

catching of an exception.

▪ Finally − The Finally block is used to execute a given set of statements, whether an

exception is thrown or not thrown. For example, if you open a file, it must be closed

whether an exception is raised or not.


▪ Throw − A program throws an exception when a problem shows up. This is

done using a Throw keyword.

13. Explain the use of Try-catch block with example? Try − A Try

block identifies a block of code for which particular exceptions will be activated. It's

followed by one or more Catch blocks.

Catch − A program catches an exception with an exception handler at the place in

a program where you want to handle the problem. The Catch keyword

indicates the catching of an exception.


Syntax of Try/Catch

The Try/Catch statements take the syntax given below:


Try
[ try_Statement(s) ]
[ Exit Try ]
[ Catch [ exception_name [ As type ] ] [ When expression ]
[ catch_Statement(s) ]
[ Exit Try ] ]
[ Catch ... ]
[ Finally
[ finally_Statement(s) ] ]
End Try

EXAMPLE :
Module Module1
Sub divisionFunction(ByVal n1 As Integer, ByVal n2 As Integer)
Dim answer As Integer
Try answer = n1
\ n2
Catch ex As DivideByZeroException
Console.WriteLine("Exception: {0}", ex)
Finally
Console.WriteLine("Answer is: {0}", answer)
End Try
End Sub Sub Main()
divisionFunction(4, 0)
Console.ReadKey() End
Sub

End Module

14. What is user defined exception? Explain with an example?


It allows us to create custom exceptions derived from the ApplicationException
class.Let's create a program to understand the uses of User-Defined Exceptions in VB.NET
Exception Handling. User_Exception.EXAMPLE

Module User_Exception
Public Class StudentIsZeroException : Inherits Exception

Public Sub New(ByVal stdetails As String)


MyBase.New(stdetails)
End Sub
End Class
Public Class StudentManagement Dim
stud As Integer = 0
Sub ShowDetail()
If (stud = 0) Then
Throw (New StudentIsZeroException(" Student roll no 'zero' does not exist")) Else
Console.WriteLine(" Student is {0}", stud)
End If
End Sub
End Class
Sub Main()
Dim stdmg As StudentManagement = New StudentManagement()
Try stdmg.ShowDetail()
Catch ex As StudentIsZeroException
Console.WriteLine(" StudentIsZeroException {0}", ex.message) End
Try
Console.ReadKey() End
Sub
End Module

You might also like