UNIT.3 .net

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

UNIT – 3

OOPS and Exception Handling


Types,
structures:
Declare a structures,
structures variable,
structures and array,
structures and objects,
structures and procedures,
structures within structures.

classes :
class Definition,
Member functions and Encapsulation,
Constructor and Destructors,
Parameterized Constructor,
Shared Members of vb.net Class,

Inheritance:
Base and Derived Classes,
Base Class Initialization,
MyBase,

Interfaces:
creating interfaces,
using multiple interfaces,
using the Mustinherit keyword(creating abstract classes),
using Mustoverride, Mustoverridable, and Notoverridable,
Polymorphism,
Inheritance based polymorphism,
Interface based polymorphism.
Exception Handling: Try catch statement,
Exception classes in .Net Framework,
Handling Exceptions,
Creating User define Exceptions.

Q1. Explain public, private and protected specifiers. 2022 mca.


Q2. Write any two classes of system.xml name space.
Q1. Describe creating the class and object in .net programming. 3no.
Q1.

Q1.
Q2.
Q1.
Q2. What is an identifier? Mca 2021 3no.
Q1. What are the major built-in objects in ASP.NET? 6no.

Q1. Define exceptions. Mca 2021 5th 2no.


Q2.
Q1.
Q2. What is exception handling statement. 3no.
Q1.

Q1. Mca2020
Q2.
Q1.
Q2. How has exception handling changed in .NET framework. 3no.
Q1.
structures:
 A structure in C# is simply a composite data type consisting of a number
elements of other types.

 A C# structure is a value type and the instances or objects of a structure are


created in stack.

 Structure is defined using struct keyword.

 The structure in VB.NET can contain fields, methods, constants, constructors,


properties, indexers, operators and even other structure types.

Structure Declaration & Object Creation


 The keyword struct can be used to declare a structure. The general form
of a structure declaration in C# is as follows.

Where the modifier can be private, public, internal or public. The struct is the required
keyword.

For example

The objects of a strcut can be created by using the new operator as follows.

Declare a structures,
structures variable,
structures and array,
structures and objects,
structures and procedures,
structures within structures.

classes :
 In VB.NET, a class is a blueprint for creating objects.
 It defines a data structure that includes data members (field and
properties) and member functions(methods).
 Class can be defined using the class keyword.
 It is basic unit of Object-Oriented Programming and represents
real life entities.
class Definition,
A new class requires a class definition. It starts with the class keyword
and contains the class name.

The syntax of a class definition is given as follows:

Example:

Member functions and Encapsulation,


Member functions
 The member function of a class is used to define the structure of member inside the
definition of the class.

 It can be accessed by all defined objects of the class and operated on the data
member.

 Furthermore, member variables are the attributes of an object to be implemented to


a member function.

 And we can access member variables using the public member function.

Encapsulation,
 Encapsulation is the concept of wrapping data into a single unit.

 It collects data members and member functions into a single unit called class.

 The purpose of encapsulation is to prevent alteration of data from outside.

 This data can only be accessed by getter functions of the class.

Constructor and Destructors,


 Constructors are special methods that are called when an object
is created.
 Constructor is also useful for creating and setting default values for a new object of a
data member.

 When we create a class without defining a constructor, the compiler automatically


creates a default constructor.

 In this way, there is a constructor that is always available in every class.


Default Constructor: In VB.NET, when we create a constructor without defining an argument,
it is called a default constructor.
VB.NET Default Constructor Syntax
The following is the syntax for creating a constructor using the New keyword in VB.NET.

Destructors,
 In VB.NET, Destructor is a special function that is used to destroy a class object when
the object of the class goes out of scope.

 It can be represented as the Finalize() method and does not accept any parameter
nor return any value.

 Furthermore, it can be called automatically when a class object is not needed.

Parameterized Constructor,
 In VB.NET, when we pass one or more arguments to a constructor, the constructor is
known as a parameterized constructor.

 And the object of the class should be initialized with arguments when it is created.

Shared Members of vb.net Class,


Inheritance:
 Inheritance is a fundamental concept in object-oriented programming that
allows us to define a new class based on an existing class.

 The new class inherits the properties and methods of the existing class and
can also add new properties and methods of its own.

 Inheritance promotes code reuse, simplifies code maintenance, and


improves code organization.

We group the "inheritance concept" into two categories:


Base and Derived Classes,

 Derived Class (child) - the class that inherits from another class
 Base Class (parent) - the class being inherited from

Types of inheritance
There are the following types of inheritance:

1. Single Inheritance

In single inheritance, a single derived class inherits from a single base


class.
2. Multilevel Inheritance

In multilevel inheritance, a derived class inherits from a base and then the
same derived class acts as a base class for another class.

3. Hierarchical Inheritance

In hierarchical inheritance, multiple derived classes inherit from a single


base class.

4. Multiple Inheritance

In multiple inheritance, a single derived class inherits from multiple base


classes. C# doesn't support multiple inheritance. However, we can
achieve multiple inheritance through interfaces.
Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance.


The combination of multilevel and hierarchical inheritance is an example of
Hybrid inheritance.

Base Class Initialization,


MyBase,
Interfaces:
 Interface in C# is a blueprint of a class.
 An interface is a completely "abstract class", which can only contain
abstract methods and properties (with empty bodies).
 It is used to achieve multiple inheritance which can't be achieved by
class.
 It is used to achieve fully abstraction because it cannot have method
body.
creating interfaces,
you can create interfaces using the ‘Interface’ keyword.

using multiple interfaces,


A class can implement multiple interfaces, separated by commas:
using the Mustinherit keyword(creating abstract classes),
using Mustoverride, Mustoverridable, and Notoverridable,

Polymorphism,

 Polymorphism allows treating objects of a derived class as objects of its


base class.
 It provides the ability for a class to have multiple implementations with the
same name.

There are two types of polymorphism.

 Compile-time Polymorphism
 Runtime Polymorphism

Compile-time Polymorphism#
 Compile-time polymorphism is achieved using method overloading and
operator overloading.

 The method overloading means defining multiple methods with the same
name but with different parameters.

Runtime Polymorphism#
 Runtime polymorphism is achieved by method overriding.

 The method overriding means defining methods in parent and child class with
the same name and signature but different implementation.

Inheritance based polymorphism,


Interface based polymorphism
Exception Handling: Try catch statement,
 An exception is an unwanted error that occurs during the execution of a program
and can be a system exception or application exception.

 Exceptions are nothing but some abnormal and typically an event or condition that
arises during the execution, which may interrupt the normal flow of the program.

 Exception handling in .NET is a mechanism that allows you to gracefully handle and
recover from runtime errors or exceptional situations that may occur during the
execution of a program.

 The key elements of exception handling in .NET are the try, catch, finally, and throw
statements.

1. Try Statement:
 The try block contains the code that might raise an exception.

2. Catch Statement:
 The catch block follows the try block and contains the code that handles the
exception.
3. Finally Statement:
 The finally block, if present, is executed regardless of whether an exception is
thrown or not.
 It is typically used for cleanup operations.

4. Throw Statement:
 The throw statement is used to manually throw an exception.
Exception classes in .Net Framework,
 In the .Net Framework, exceptions are represented by
classes.
 The exception classes in .Net Framework are mainly directly or
indirectly derived from the System.Exception class.
 Some of the exception classes derived from the System.Exception class are
the System.ApplicationException and System.SystemException classes

The following table provides some of the predefined exception classes derived
from the Sytem.SystemException class –
Exception Class Description

System.IO.IOException Handles I/O errors.

Handles errors generated from


System.DivideByZeroException
dividing a dividend with zero.

Handles errors generated


System.InvalidCastException
during typecasting.

Handles errors generated from


System.OutOfMemoryException
insufficient free memory.

Handles errors generated from


System.StackOverflowException
stack overflow.

Handling Exceptions,
Creating User define Exceptions.

You might also like