Vous êtes sur la page 1sur 6

OBJECT ORIENTED PROGRAMMING LANGUAGE

CLASSES
Which contains the data and its functions together as a class? A class can also be defined as a
programmatic representation of an entity and the behavior of that entity can be represented by the
functions in the class.

Abstraction
This behavior to ignore unwanted details of an entity is termed as abstraction.

Encapsulation

Encapsulation is an implementation of abstraction Encapsulation directly leads to two main


advantages:

Data Hiding: -- The user of the class does not come to know about the internals of the class.
Hence, the user never comes to know about the exact data members in the class. The user interacts
with the data members only through the various member functions provided by the class.

Data Security: - Since the data, members are not directly available to the user directly but are
available only through the member functions a validity check can always be imposed to ensure that
only valid data is been inserted into the class. So a Date class, which contains individual data
members for storing date, month and year, will have it ensured the month is never 13 or the date is
never exceeding 31.

Inheritance

Class which would be reusing the functionalities of the other class in object oriented terms we
would say that the class is “deriving” from the former class and is termed as the derived class. The
class that is being “derived from” is termed as the base class.

Benefits:
Reusability: -- Inheritance results in functionalities defined in one class being reused in the derived
classes. So the efforts of rewriting the same functionality for every derived class is being saved.
This definitely saves a lot of development time.
Enhancement and Specification: -- Due to the characteristic of inheritance, we can club the
common functionalities in the base class and have the specific functionalities in the derived class.
This feature can be used to have a functionality defined in the base class to be further modified for
betterment or specification by the derived class. This mechanism of redefining the functionality of
the base class in the derived class is termed as “overriding”

1
Inheritance

Base class:- A class which contains common properties and methods that can shared
with other classes by inheritance is called Base class. Ex:- Person class

Derived class:- A class which inherits the base class is knows as Derived class.
ex:- Employee class and Customer class.

Implementation:- A derived class can inherit only one base class. its shown in the above
examples, ie., employee class inherits person class and customer class inherits person
class.

Public keyword declarations in the class will be accessed by the object users and the
derived class (the class which inherits the base class).

Private keyword declarations can be accessed only within the class (it means the class in
which the declaration is done).

Protected Its functionality is a hybrid of public and protected keyword. So, its very
important in class inheritance, because in the situation where the data is to be
communicated only between the base and derived classes irrespective of the external
object user (means the end user) the protected keyword is used

Virtual Members :-Virtual members are those that can be overridden and replaced by the
derived classes. They are declared with Overridable keyword. The methods or properties
which doesn’t contain overridable keyword are called Non-Virtual members.

For ex:- DisplayInfo method is an virtual method of Person base class, because it
has been overwritten in the Employee derived class with new DisplayInfo method
with same signature.

Restricting Polymorphism :-
We have already seen polymorphism is implemented with Overridable and Overrides
keyword. Some situation arises where we need to break the polymorphism effect of the
base class method. It means changing the signature
of the base class member while overriding it in the derived class violates the polymorhism
rule “signatures must be same”. Restriction is required to completely change the
implementation of the member in the derived class. So it
restricts the polymorphism of the base class member with new implementation in
the derived class. The Shadow keyword used to implement this concept. Take an
example shown below which contains two classes 1) Person Class 2) Employee class.
Person class is a base class contains method to be overwritten from the derived
class. The signature of the overridable method is
Abstract Classes (Virtual Class)
So far, we have seen how to inherit the class, how to overload and override methods. In
the examples shown in inheritance topic, parent class has been useful in both inheritance
and create an instance also.

2
Actually in some situation the classes are required only for inheritance, such type
of classes are called Abstract classes. This concept is very useful when creating a
framework for the applications where there will not be any implementation for the
methods and properties in the abstract class. It just
defines the structure.

Abstract classes are declared using MustInherit and MustOverride keyword.

Implementation

Avoiding type inspection:--

Inheritance, we would have a common base class called as “Employee” which would have all the
common functionalities defined where as the specific routines do be done for various types of
employees would go in the respective derived classes. So there would be class defined for every
type of employee and the class would all the specifications for that type of employee and would be
derived from the base class Employee to inherit the common functionalities. Therefore, in the
functions now we wont have to check for the type of employee every time because every employee
type has its own specific routines defined within it.

Polymorphism
“polymorphism” means “different forms”. Applied in object-oriented paradigm it means the ability
of an entity to exhibit different forms at runtime.

Constructors
Constructors are special member functions, which are used for initializing the
class data members.
C# supports following types of constructors
■ Default Constructors
■ Parameterized constructors
■ Private constructors
■ Static constructors
3
Destructors
supports automatic memory cleanup by having a garbage collector element. Then if there
is a memory management mechanism why would we require destructors?

Static Members
we have two different MS WORD windows opened with some documents opened. We
select some text from one of the windows, copy it and try to paste it another window. Now
we know that both these windows happen to be two separate entities. So lets imagine two
objects of the same kind to have similar kind of interaction. Definitely we would a common
memory are which both the objects would be able to access. This memory area also has
to restricted to the objects of that class only. The key point over here is the common area
available to the objects.
Abstract Classes (Virtual Class)
So far, we have seen how to inherit the class, how to overload and override methods. In
the examples shown in inheritance topic, parent class has been useful in both inheritance
and create an instance also.

Actually in some situation the classes are required only for inheritance, such type
of classes are called Abstract classes. This concept is very useful when creating a
framework for the applications where there will not be any implementation for the
methods and properties in the abstract class. It just
defines the structure.

Abstract classes are declared using MustInherit and MustOverride keyword.

Implementation

Sealed
When we want an employee class need not to be used as a Base class, we can
declare the employee class structure with Not Inheritable keyword.

4
Interfaces
interface is like an abstract class which allows the derived class to inherit more than one
interfaces into it. An abstract class can have methods with or without implementation. But
an interface can only have members without implementation. So it provides only structure
of the objects like abstract class.

Implementation of Interface:- Interfaces are implemented using Implements keyword. All the
members of the interface are implemented with Implements keyword.

5
Delegates and Events
Delegates:-
A delegate is defined as a data structure that refers to a static method or to a class
instance and an instance method of that class. In other words, a delegate can be used
like a type safe pointer. You can use a delegate to point to a method.

Vous aimerez peut-être aussi