Search This Blog

Thursday 4 September 2014

OOPS Concepts

Class:
A class is a blueprint of an object that contains variables for storing data and
functions to perform operations on the data.
It is a collection of objects.
A class will not occupy any memory space and hence it is only a logical
representation of data
Object:
It is a real time entity.
An object is an instance of a class.
An object can be considered a “thing” that can perform a set of related activities.
What is encapsulation?
Encapsulation is the process of hiding irrelevant data from the user. To understand
encapsulation, consider an example of mobile phone. Whenever you buy a mobile,
you don’t see how circuit board works. You are also not interested to know how
digital signal converts into analog signal and vice versa. These are the irrelevant
information for the mobile user, that’s why it is encapsulated inside a cabinet.
In C# programming, we will do same thing. We will create a cabinet and keep all the irrelevant
information in it that will be unavailable for the user.
What is abstraction?
Abstraction is just opposite of Encapsulation. Abstraction is mechanism to show
only relevant
data to the user. Consider the same mobile example again. Whenever you buy a
mobile phone,
you see their different types of functionalities as camera, mp3 player, calling
function,
recording function, multimedia etc. It is abstraction, because you are seeing only
relevant
information instead of their internal engineering.
Encapsulation and Abstraction
Encapsulation and abstraction is the advanced mechanism in C# that lets your
program to
hide unwanted code within a capsule and shows only essential features of an object.
Encapsulation is used to hide its members from outside class or interface, whereas
abstraction
is used to show only essential features.
In C# programming, Encapsulation uses five types of modifier to encapsulate data.
These modifiers are public, private, internal, protected and protected internal.
Acquiring  or inherit the properties of one class (Base Class )to another class(Derived Class).
This also provides an opportunity to reuse the code functionality and fast implementation time.
Inheritance can be classified to 5 types.
1 Single Inheritance
2 Hierarchical Inheritance
3 Multi Level Inheritance
4 Hybrid Inheritance
5 Multiple Inheritance
1. Single Inheritance
when a single derived class is created from a single  base class then the inheritance is called as single inheritance.
2. Hierarchical Inheritance

when more than one derived class are created from a single base class, then that
inheritance is called as hierarchical inheritance.
3. Multi Level Inheritance

when a derived class is created from another derived class, then that inheritance is
called as multi level inheritance.
4. Hybrid Inheritance

Any combination of single, hierarchical and multi level inheritances is called as
hybrid inheritance.
5. Multiple Inheritance

when a derived class is created from more than one base class then that inheritance is
called as multiple inheritance. But multiple inheritance is not supported by
.net using classes and can be done using interfaces.
Handling the complexity that causes due to multiple inheritance is very 
complex. Hence it is not supported in dotnet with class and it can be 
achieved with interfaces.
Polymorphism
Polymorphism means many forms (ability to take more than one form). In
Polymorphism poly means “multiple” and morph means “forms”
so polymorphism means  many forms.
In polymorphism we will declare methods with same name and different
parameters in same class or methods with same name and same parameters in
different classes. Polymorphism has ability to provide different implementation
of methods that are  implemented with same name.
In Polymorphism we have 2 different types those are
  1.  Overloading 
Called as Early Binding or Compile Time Polymorphism or static binding
In a class  we will declare two or more  methods with same name but with different parameters
because of this we will perform different tasks with same method name.
Two Types
  1. Method Overloading
This can be achieved by :
Passing different number of parameters.
Passing different order of parameters.
Passing different types of parameters.
  1. Operator Overloading
  2. Method Overriding
Called as Late Binding or Run Time Polymorphism or dynamic binding
In this run time polymorphism or method overriding we can override a method in base class
by creating similar function in derived class this can be achieved by using inheritance
principle and using “virtual &override” keywords.
In base class if we declare methods with virtual keyword then only we can override those
methods in derived class using override keyword
Types of classes
  • Abstract Class (somtimes called a Pure Virtual Class)
An Abstract Class means that, no object of this class can be instantiated, but can make
derivation of this. It can serve the purpose of base class only as no object of this class can
be created.
Abstract Class is denoted by the keyword abstract.
  • Partial Class
This special type of class called “Partial Class” is introduced with .Net Framework 2.0. Partial
Class allows its members – method, properties, and events –
to be divided into multiple source files (.cs). At compile time these files
get combined into a single class.
Partial Class is denoted by the keyword partial.
Some do’s and don’ts about partial class:-
• All the parts of a partial class must be prefixed with the keyword partial.
• Accessibility, signature etc. must be same in all parts of the partial class.
• You cannot sealed one part of the partial class. In that case entire class in sealed.
• If you define any part of the partial class abstract, entire class will become abstract.
• Inheritance cannot be applied to a part of partial class. If you do so, it applies to entire
class.
  • Sealed Class
A sealed class is a class which cannot be inherited. A sealed class cannot be a base class.
The modifier abstract cannot be applied to a sealed class. It is the last class in hierarchy.
To access the members of a sealed class, you must create objects of that class.
Sealed Class is denoted by the keyword sealed.
  • Static Class
A Static Class is one which cannot be instantiated. The keyword new cannot be used
with static classes as members of such class can be called directly by using the class
name itself.
Following are the main characteristics of a static class:-
• A Static Class can only have static members.
• A Static Class cannot be instantiated.
• A Static Class is sealed, so cannot be inherited.
• A Static Class cannot have a constructor (except static constructor).
Static Class is denoted by the keyword static.

No comments:

Post a Comment