You are on page 1of 6

OPP

1. Encapsulation
2. Inheritance
3. Polymorphism
Encapsulation in C#:
Encapsulation is process of keeping data and methods together inside
objects. In C#, encapsulation is realized through the classes. A Class can contain
data structures and methods
public class Aperture
{
public Aperture()
{}
protected double height;
protected double width;
protected double thickness;

public double GetVolume()


{double volume = height*width*thickness;
if(volume<0)
return 0;
return volume;}}

Other methods or objects can interact with this object through methods that have
public access modifier.
Inheritance in C#:
In a few words, Inheritance is the process of creation new classes from already
existing classes. The inheritance feature allows us to reuse some parts of
code. So, now we have some derived class that inherits base class's members.
public class Door : Aperture
{ public Door() : base()
{}
public bool isOutside = true;}
As you see to inherit one class from another, we need to write base class name
after : symbol. Next thing that was done in code Door () constructor also
inherits base class constructor. And at last we add new private field. All
members of Aperture class are also in Door class. We can inherit all the
members that has access modifier higher than protected.

Polymorphism in C#:
Polymorphism is possibility to change behavior with objects depending of object's
data type. In C# polymorphism realizes through the using of keyword virtual and
override
public virtual void Out()
{
Console.WriteLine("Aperture virtual method called");
}
//This method is defined in Aperture class.
public override void Out()
{
Console.WriteLine("Door virtual method called");
}
Now we need to re-define it in our derived Door class. The usage of virtual methods
can be clarified when we creating an instance of derived class from the base class:
Aperture ap = new Door();
ap.Out();
Virtual and Overridden Methods
Only if a method is declared virtual, derived classes can override this method if they
are explicitly declared to override the virtual base class method with the override
keyword.
using System;
namespace Polymorphism
{
class A
{
public virtual void Foo() { Console.WriteLine("A::Foo()"); }
}

class B : A
{
public override void Foo() { Console.WriteLine("B::Foo()"); }
}

class Test
{
static void Main(string[] args)
{
A a;
B b;

a = new A();
b = new B();
a.Foo(); // output --> "A::Foo()"
b.Foo(); // output --> "B::Foo()"

a = new B();
a.Foo(); // output --> "B::Foo()"
}

}
}
Method Hiding
A hiding method has to be declared using the new keyword.
using System;
namespace Polymorphism
{
class A
{
public void Foo() { Console.WriteLine("A::Foo()"); }
}
class B : A
{
public new void Foo() { Console.WriteLine("B::Foo()"); }
}

class Test
{
static void Main(string[] args)
{
A a;
B b;

a = new A();
b = new B();
a.Foo(); // output --> "A::Foo()"
b.Foo(); // output --> "B::Foo()"

a = new B();
a.Foo(); // output --> "A::Foo()"
}
}
}
Constructors

Constructors are used for initializing the members of a class whenever an


object is created with the default values for initialization. If a class is not
defined with the constructor then the CLR (Common Language Runtime) will provide
an implicit constructor which is called as Default Constructor.
Constructors do not return a value.
Constructors can be overloaded.
The following are the access modifiers for constructors:
Public : A constructor that is defined as public will be called whenever a class is
instantiated.
Protected : A constructor is defined as protected in such cases where the base
class will initialize on its own whenever derived types of it are created.
Private : A constructor is defined as private in such cases whenever a class which
contains only static members has to be accessed will avoid the creation of the
object for the class.
Internal : An internal constructor can be used to limit concrete implementations of
the abstract class to the assembly defining the class. A class containing an internal
constructor cannot be instantiated outside of the assembly.
External : When a constructor is declared using an extern modifier, the constructor
is said to be an external constructor.

Types of Constructors
Static
Non-Static

What is abstract class?


Abstract class is a base class or a parent class. Abstract classes can have empty
abstract methods or it can have implemented methods which can be overridden by
child classes.

What are interfaces?


Interface is a contract class with empty methods, properties and functions.
Any class which implements the interface has to compulsory implement all the
empty methods, functions and properties of the interface.
What's the difference between abstract class and interface?
There are many differences, below are some key two differences:-
• Abstract class are base class or parent class while interfaces are
contracts.
• Abstract class can have some implemented methods and functions
while interfaces methods and functions are completely empty.
• Abstract classes are inherited while interfaces are implemented.
• Abstract classes are used when we want to increase reusability in inheritance
while interfaces are used to force a contract.
Can we create an object of abstract class or interface?
No we cannot.
In what scenarios will you use an abstract class and in what scenarios will
you use a interface?
If you want to increase reusability in inheritance then abstract classes are good.
If you want implement or force some methods across classes must be for
uniformity you can use an interface. So to increase reusability via inheritance
use abstract class as it is nothing but a base class and to force methods use
interfaces.

Overloading and Overriding


Overloading is about creating multiple methods with the same name, but
different signatures, in the same scope. Overriding is about changing the
behavior of a certain method in the child class from the way it is behaving in the
parent class
Overloading occurs when a method has more than one definition in the
same scope. It's important to remember two key points from the
previous statement: same name and same scope.

public string GetName( int id) {...}

public string GetName( string ssn) {...}

Application Pool
Application pools isolate sites and applications to address reliability, availability,
and security issues.
- To group sites and applications that run with the same configuration settings
- To isolate sites and applications that run with unique configuration settings.
- To increase security by using a custom identity to run an application.
Integrated, if you want to use the integrated IIS and ASP.NET request-
processing pipeline.
Classic, if you want to use IIS and ASP.NET request-processing modes
separately. In classic mode, managed code is processed by using Aspnet_isapi.dll
instead of the IIS 7 integrated pipeline.
Localization
Localization is customizing data and resources for specific culture or language.
Controls
HTML controls, HTML server controls, web server controls, validation controls, and
controls created by the developer.
Custom controls are compiled controls. Custom controls can be created in one of
three ways:
- derived custom control- By deriving a new custom control from an existing
control
- composite custom control - By composing a new custom control out of two or
more existing controls
- full custom control By deriving from the base control class
UserControl
.ascx extension

.Net C# Delegates and Events


Delegates - Pointers to Functions to pass them as parameters to other functions.

delegate result-type identifier ([parameters]);

There are three steps in defining and using delegates: declaration, instantiation,
invocation

In C#, delegates are multicast, which means that they can point to more than
one function at a time

Events

An event is a message sent by an object to signal the occurrence of an action.


The action could be caused by user interaction, such as a mouse click, or it could
be triggered by some other program logic. The object that raises the event is
called the event sender. The object that captures the event and responds
to it is called the event receiver.

Partial

split the definition of a class or a struct, an interface or a method over two or more
source files.Example: When working on large projects, spreading a class over
separate files enables multiple programmers to work on it at the same time

public
The type or member can be accessed by any other code in the same assembly or another
assembly that references it.

private

The type or member can be accessed only by code in the same class or struct.

protected

The type or member can be accessed only by code in the same class or struct, or in a class
that is derived from that class.

internal

The type or member can be accessed by any code in the same assembly, but not from
another assembly.

protected internal

The type or member can be accessed by any code in the assembly in which it is declared,
or from within a derived class in another assembly. Access from another assembly must
take place within a class declaration that derives from the class in which the protected
internal element is declared, and it must take place through an instance of the derived
class type

SQL
Referential integrity is a database concept that ensures that relationships
between tables remain consistent. When one table has a foreign key to another
table, the concept of referential integrity states that you may not add a record to
the table that contains the foreign key unless there is a corresponding record in the
linked table. Example: Employees and Managers

1. We may not add a record to the Employees table unless the ManagedBy attribute
points to a valid record in the Managers table.
2. If the primary key for a record in the Managers table changes, all corresponding
records in the Employees table must be modified using a cascading update.
3. If a record in the Managers table is deleted, all corresponding records in the Employees
table must be deleted using a cascading delete.

You might also like