Abstraction
"Abstraction"
refers to the act of representing essential features without including the
background details or explanations. Classes use the concept of abstraction and
are defined as a list of abstract attributes.
Real
time e.g. /we invented flight based on the mechanism of Birds. So
Flight
is derived from the base of birds.
For example code
abstract class MobilePhone
{
public void Calling();
public void SendSMS();
}
public class Nokia1400 : MobilePhone
{
}
public class Nokia2700 : MobilePhone
{
public void FMRadio();
public void MP3();
public void Camera();
}
public class BlackBerry : MobilePhone
{
public void FMRadio();
public void MP3();
public void Camera();
public void Recording();
public void ReadAndSendEmails();
}
Encapsulation
Is
the ability of an object to hide its data and methods from the rest of the
world. It is one of the fundamental principles of OOPs.
Real time
e.g.
/ Ink is the important component in pen but it is hiding by some other material
Say we create a class, named Calculations. This class may contain a few members in the form of properties, events, fields or methods. Once the class is created, we may instantiate the class by creating an object out of it. The object acts as an instance of this class, the members of the class are not exposed to the outer world directly; rather, they are encapsulated by the class.
For example code
Public class Calculations
{
private void fnMultiply(int x, int y)
{
return x * y;
}
}
...
...
Calculations obj;
int Result;
Result = obj.fnMultiply(5,10)
Inheritance
One class properties occur
from another class properties it’s called about inheritance
Real time
e.g. / parent-child
relation
For example code
public class Asset
{
public string Name;
}
Next,
we define classes called Stock and House, which will inherit from Asset. They
Get everything an Asset
has, plus any additional members that they define:
public class House : Asset // inherits from Asset
{
public decimal Mortgage;
}
Here’s
how we can use these classes:
Stock msft = new Stock
{
Name = "MSFT",
SharesOwned = 1000
};
Console.WriteLine(msft.Name); // MSFT
Console.WriteLine(msft.SharesOwned); // 1000
House mansion = new House
{
Name = "Mansion",
Mortgage = 250000
};
Console.WriteLine(mansion.Name); // Mansion
Console.WriteLine(mansion.Mortgage); // 250000
The subclasses,
Stock and House, inherit the Name property from the base class,
Asset.
Polymorphism
A
single function or single operator has different character
In
different place.
Real
time e.g. /A girl plays a role of daughter at home and a manager at
Office
For example code
References
are polymorphic. This means a variable of type x can refer to
an object
That
subclasses x. For instance, consider the following method:
public static void Display (Asset asset)
{
System.Console.WriteLine (asset.Name);
}
This
method can display both a Stock and a House, since they are both Assets:
Stock msft = new Stock ... ;
House mansion = new House ... ;
Display (msft);
Display (mansion);
Polymorphism
works on the basis that subclasses (Stock and House) have all the
features
of their base class (Asset). The converse, however, is not true. If Display
was
modified to accept a House, you could not pass in an Asset:
static void Main()
{
Display(new Asset());
} // Compile-time error
public static void Display(House house) // Will not accept Asset
{
System.Console.WriteLine(house.Mortgage);
}
Thanks…
Ref
C# nutshell and Google
nice blog thanks abiruban
ReplyDeleteThis blog is very nice and informative!
ReplyDeleteKeep Update....
Thanks for sharing.
you explain the abstraction point with coding that are nice way so high most probably i understand all condition.
offshore software web development
thanks.....
Deletevery good
ReplyDeleteIn abstract class you should maintain abstract method as abstract keyword like this,
ReplyDeleteabstract class MobilePhone
{
public void Calling(); // public abstract void Calling();
public void SendSMS(); // public abstract void SendSMS();
}
True, since by making the method as abstract, you can overide it in the other class.
DeleteBetter example for Abstract class
ReplyDeleteabstract class MobilePhone
{
public abstract int Calling();
public abstract string SendSMS();
}
public class Nokia1400 : MobilePhone
{
public override int Calling()
{// Implementation
}
public override string SendSMS()
{// Implementation
}
}
Hi, Nice examples about Oops Concepts.Thanks, its really helped me......
ReplyDelete-Aparna
Theosoft
thanks.....
Deleteabstract class MobilePhone
ReplyDelete{
public void Calling();
public void SendSMS();
}
in this class we have to use atleast 1 abstract method. otherwise when we extend this class Child class will not compile.
Very gud explanation...
ReplyDelete