Professional Documents
Culture Documents
Real World Example: Every time you log into your email account( GMailGmail, Yahoo,
Hotmail or official mail), you have a whole lot of processes taking place in the backend,
that you have no control over. So yourTherefore your password, would probably be
retrieved in an encyrpted encrypted form, verified and only then you are given access.
You do not have any control, over how the password is verified, and this keeps it safe
from misuse.
Abstraction is a process of hiding the implementation from the user, only the
functionality is exposed here. So you are aware only of what the application does, not
how it does it.
Real World Example: When you log into your email, compose and send a mail. Again
there is a whole lot of background processing involved, verifying the recipient, sending
request to the email server, sending your email. Here you are only interested in
composing and clicking on the send button. What really happens when you click on the
send button, is hidden from you.
For example, TV remote is an object. Users are provided with only interface which is
nothing but keys. Users only know which key to press for what function. User will not
know what happens inside when we press the key..
Implementation of Abstraction
Now lets think in terms of Car rider or a person who is riding a car.
So to drive a car what a car rider should know from above category
before he starts a car driving.
Now above same thing let me put in the coding style using C#
set
_nameofcar = value;
}
get
return _nameofcar;
}
}
set
_colorofcar = value;
}
get
return _colorofcar;
}
}
Console.WriteLine("Streering of Car");
}
Console.WriteLine("RearViewMirror of Car");
}
Console.WriteLine("Brakes of Car");
}
public void Gear()
Console.WriteLine("Gear of Car");
}
Console.WriteLine("InternalDetailsofCar of Car");
}
Console.WriteLine("CarEngine of Car");
}
Console.WriteLine("DiesalEngine of Car");
}
Console.WriteLine("ExhaustSystem of Car");
}
Console.WriteLine("Silencer of Car");
}
}
As you can see from above code that necessary methods and
properties exposed by using "public" access modifier and
unnecessary methods and properties (not compulsary) hidden by
using "private" access modifier.
class Program
}
}
Conclusion
Real-time Example 1:
School bag is one of the most real examples of Encapsulation. School bag can
keep our books, pens, etc.
When you log into your email accounts such as Gmail, Yahoo mail, or Rediff
mail, there is a lot of internal processes taking place in the backend and you
have no control over it.
When you enter the password for logging, they are retrieved in an encrypted form
and verified and then you are given the access to your account. You do not have
control over it that how the password has been verified. Thus, it keeps our
account safe from being misused.
So, they declare balance variable as private for making your account safe, so
that anyone cannot see your account balance. The person who has to see his
account balance, he will have to access private members only through methods
defined inside that class and this method will ask your account holder name or
user Id, and password for authentication.
Thus, We can achieve security by utilizing the concept of data hiding. This is
called Encapsulation.
Advantages of Encapsulation
4. It keeps the data and codes safe from external inheritance. Thus,
Encapsulation helps to achieve security.
Disadvantages of Encapsulation
The main disadvantage of the encapsulation is it increases the length of the code
and slow shutdown execution.
A constructor is a special method that is used to initialize a newly created object and is called
just after the memory is allocated for the object.
i.e Consider human as a class. The birth of a child is like initialising an object for that
class.
There is no kid in this world without a name. So naming ceremony is the functionality
inside a constructor, which will be done at the time of initialisation. Hence every kid gets
a name, necessarily!
Assume, you are supposed just perform the function of talking. Say, you have to tell the
story of your day, to a total stranger. Your function will get over pretty quickly. Say, now
you are telling the same to your beloved. You will go through more details as compared
to the previous one. What has happened here, is, you have performed the same
function, but based on the parameter, stranger/beloved, your way of implementing the
function changed!
1. class You
2. {
3. void talk(Stranger obj)
4. {
5. sysout("Hi, my day was great!");
6. }
7. void talk(Beloved obj)
8. {
9. sysout("Hi, my day was great! You won't believe what happened
today! Blah!Blah!Blah!Blah!Blah!Blah!Blah!Blah!Blah!Blah! ");
10. }
11. }
Method Overriding is a technique that allows the invoking of functions from another class
(base class) in the derived class. Creating a method in the derived class with the same
signature as a method in the base class is called as method overriding.
We learn a lot from our parents! We learn to cook to some extent. The same delicacy
with same ingredients is prepared by your mother with a different taste and you with a
different taste (assuming). That is overriding, same function (cooking), same parameters
(ingredients), but different algorithms (cooking style).
Or, your learnt driving from you dad! But you both drive the same vehicle differently!
That is overriding.
1. class Father
2. {
3. void drive()
4. {
5. //drive cautiously :-)
6. }
7. }
8. class You
9. {
10. void drive()
11. {
12. //drive rash! :P
13. }
14. }
Programming perspective:
WHAT IS INHERITANCE?
Inheritance is one of the most important pillars of Object Oriented Programming.
Inheritance allows a class to access all the property of the Base class and also Base
class can have its own behaviour. Using Inheritance a class can be act as a template
for other classes. A class that allows inheritance is called as Base Class and the class
that inherits other class is called as derived class.
IS-A Relationship.
HAS-A Relationship
Polymorphism is the ability of an object to take on many forms. The most common use of
polymorphism in OOP occurs when a parent class reference is used to refer to a child class
object.
Polymorphism is the ability of an object to take on many forms. The most common use of
polymorphism in OOP occurs when a parent class reference is used to refer to a child class
object.
The virtual keyword is used to modify a method, property, indexer, or event declaration and
allow for it to be overridden in a derived class. For example, this method can be overridden
by any class that inherits it:
When a virtual method is invoked, the run-time type of the object is checked for an
overriding member. The overriding member in the most derived class is called, which might
be the original member, if no derived class has overridden the member.
Static method in C# is a method that keeps only one copy of the method at the Type level, not the
object level. That means, all instances of the class share the same copy of the method and its data. The
last updated value of the method is shared among all objects of that Type.
3. Any main() method is shared through the entire class scope so it always appears with static
keyword.
Partial Classes in C#
A partial class is a special feature of C#. It provides a special ability to implement the
functionality of a single class into multiple files and all these files are combined into a
single class file when the application is compiled. A partial class is created by using
a partial keyword. This keyword is also useful to split the functionality of methods,
interfaces, or structure into multiple files.
Syntax :
public partial Clas_name
{
// code
}
Important points:
When you want to chop the functionality of the class, method, interface, or
structure into multiple files, then you should use partial keyword and all the files
are mandatory to available at compile time for creating final file.
The partial modifier can only present instantly before the keywords like struct,
class, and interface.
Every part of the partial class definition should be in the same assembly
and namespace, but you can use different source file name.
Every part of the partial class definition should have the same accessibility like
private, protected, etc.
If any part of the partial class is declared as an abstract, sealed, or base, then
the whole class is declared of the same type.
The user is also allowed to use nested partial types.
Dissimilar part may have dissimilar base types, but the final type must inherit all
the base types.
Example: Here, we are taking a class named as Geeks and split the definition of Geeks
class into two different files named as Geeks1.cs, and Geeks2.cs as shown below:
In Geeks1.cs, and Geeks2.cs, a partial class is created using the partial keyword and
each file contains different functionality of Geeks class as shown below.
Geeks1.cs
filter_none
brightness_4
public partial class Geeks {
private string Author_name;
private int Total_articles;
public Geeks(string a, int t)
{
this.Authour_name = a;
this.Total_articles = t;
}
}
Geeks2.cs
filter_none
brightness_4
public partial class Geeks {
public void Display()
{
Console.WriteLine("Author's name is : " + Author_name);
Console.WriteLine("Total number articles is : " + Total_articles);
}
}
When we execute the above code, then compiler combines Geeks1.cs and Geeks2.cs into a
single file, i.e. Geeks as shown below.
Geeks This class may contain the Main Method. For simplicity, here Main() method is
not included.
filter_none
brightness_4
public class Geeks {
private string Author_name;
private int Total_articles;
public Geeks(string a, int t)
{
this.Authour_name = a;
this.Total_articles = t;
}
public void Display()
{
Console.WriteLine("Author's name is : " + Author_name);
Console.WriteLine("Total number articles is : " + Total_articles);
}
}
Advantages :
With the help of partial class multiple developers can work simultaneously on the
same class in different files.
With the help of partial class concept you can split the UI of design code and the
business logic code to read and understand the code.
When you were working with automatically generated code, the code can be
added to the class without having to recreate the source file like in Visual studio.
You can also maintain your application in an efficient manner by compressing
large classes into small ones.
Partial Methods in C#
C# contains a special method is known as a partial method, which contains declaration
part in one partial class and definition part in another partial class or may contain both
declaration and definition in the same partial class.
Basically, partial methods exist in the partial class, or in the struct. A partial method may
or may not contain implementation if a partial method doesn’t contain an implementation
in any part then the compiler will not create that method in the final class or driver class.
A partial method is declared with the help of the partial keyword as shown below.
Syntax:
partial void method_name
{
// Code
}
Important Points:
The declaration of the partial method must begin with partial modifier.
The partial method may contain ref.
The partial method does not contain out parameters.
It is implicitly private method.
It can be a static method.
Partial method is generic.
It can have only void return type.
A partial method is created only in partial class or in partial struct.
Example: We have a class named as Circle. The functionality of the Circle class is
chopped into two different files named as circle1.cs and circle2.cs.
These circle1.cs and circle2.cs files contain the partial class of the Circle class and
partial method, i.e area. The circle1.cs file contains the declaration of the partial area()
method and circle2.cs file contains the implementation of the area method as shown
below:
circle1.cs
filter_none
brightness_4
public partial class Circle {
{
}
}
circle2.cs
filter_none
brightness_4
public partial class Circle {
{
area(int a);
}
{
int A = 3.14 * r * r;
}
{
}
area(int a);
}
{
int A = 3.14 * r * r;
}
Composition
Inheritance is a hierarchy of relationships between objects. For example, car is a vehicle. So,
class Vehicle
//.....
//......
Where as, Composition denotes "is-a-part-of" relationship between objects. For example,
class Engine
//....
}
class Car
//.....
Composition and inheritance are two concepts which has to be applied at appropriate scenarios.
Interface
When to use
1. Need to provide common functionality to unrelated classes.
2. Need to group objects based on common behaviors.
3. Need to introduce polymorphic behavior to classes since a class can
implements more than one interfaces.
4. Need to provide more abstract view to a model which is unchangeable.
5. Need to create loosely coupled components, easily maintainable and
pluggable components (like log4net framework for logging) because
implementation of an interface is separated from itself.
Disadvantage of Interface
1. The main issue with an interface is that when you add a new members to its,
then you must implement those members within all of the classes which
implement that interface.
2. Interfaces are slow as these required extra in-direction to find corresponding
method in in the actual class.