Abstraction in C#
The word abstract means a concept or an idea not associated with any specific instance. In programming we apply the same meaning of abstraction by making classes not associated with any specific instance. The abstraction is done when we need to only inherit from a certain class, but do not need to instantiate objects of that class. In such case the base class can be regarded as "Incomplete". Such classes are known as an "Abstract Base Class".
Abstract Base Class
There are some important points about Abstract Base Class :
An Abstract Base class can not be instantiated; it means the object of that class can not be created.
Class having abstract keyword and having abstract keyword with some of its methods (not all) is known as an Abstract Base Class.
Class having Abstract keyword and having abstract keyword with all of its methods is known as pure Abstract Base Class.
The method of abstract class that has no implementation is known as "operation". It can be defined as abstract void method ();
An abstract class holds the methods but the actual implementation of those methods is made in derived class.
Lets have a look at this code!
abstract class animal
{
public abstract void eat();
public void sound()
{
Console.WriteLine("dog can sound");
}
}
This is the Abstract Base Class, if I make both of its methods abstract then this class would become a pure Abstract Base Class.
Now we derive a class of 'dog' from the class animal.
abstract class animal
{
public abstract void eat();
public void sound()
{
Console.WriteLine("dog can sound");
}
}
class dog: animal
{
public override void eat()
{
Console.WriteLine("dog can eat");
}
}
Here you can see we have 2 methods in the Abstract Base Class, the method eat() has no implementation; that is why it is being declared as 'abstract' while the method sound() has its own body so it is not declared as 'abstract'.
In the derived class we have the same named method but this method has its body.
We are doing abstraction here so that we can access the method of derived class without any trouble.
Let's have a look!
class program
{
abstract class animal
{
public abstract void eat();
public void sound()
{
Console.WriteLine("dog can sound");
}
}
class dog: animal
{
public override void eat()
{
Console.WriteLine("dog can eat");
}
}
static void Main(string[] args)
{
dog mydog = new dog();
animal thePet = mydog;
thePet.eat();
mydog.sound();
}
}
Finally we created an Object 'mydog' of class dog, but we didn't instantiate any object of Abstract Base Class 'animal'.
According to "Ivor Horton" (a programmer of Java) an object can not be instantiated, but we can declare a variable of the Abstract Class type. If this statement is true then it could be possible:
animal thePet;
This is an object which is declared as thePet and its data type is the abstract base class 'animal'.
We can use this Object to store Objects of the subclass.
In the above code we declare an Object 'thePet', of the type animal (the Abstract Base Class) and simply copy the object of another object (only the reference is copied as they belong to reference type). Now we can use object 'thePet' just as object 'mydog'.
The word abstract means a concept or an idea not associated with any specific instance. In programming we apply the same meaning of abstraction by making classes not associated with any specific instance. The abstraction is done when we need to only inherit from a certain class, but do not need to instantiate objects of that class. In such case the base class can be regarded as "Incomplete". Such classes are known as an "Abstract Base Class".
Abstract Base Class
There are some important points about Abstract Base Class :
An Abstract Base class can not be instantiated; it means the object of that class can not be created.
Class having abstract keyword and having abstract keyword with some of its methods (not all) is known as an Abstract Base Class.
Class having Abstract keyword and having abstract keyword with all of its methods is known as pure Abstract Base Class.
The method of abstract class that has no implementation is known as "operation". It can be defined as abstract void method ();
An abstract class holds the methods but the actual implementation of those methods is made in derived class.
Lets have a look at this code!
abstract class animal
{
public abstract void eat();
public void sound()
{
Console.WriteLine("dog can sound");
}
}
This is the Abstract Base Class, if I make both of its methods abstract then this class would become a pure Abstract Base Class.
Now we derive a class of 'dog' from the class animal.
abstract class animal
{
public abstract void eat();
public void sound()
{
Console.WriteLine("dog can sound");
}
}
class dog: animal
{
public override void eat()
{
Console.WriteLine("dog can eat");
}
}
Here you can see we have 2 methods in the Abstract Base Class, the method eat() has no implementation; that is why it is being declared as 'abstract' while the method sound() has its own body so it is not declared as 'abstract'.
In the derived class we have the same named method but this method has its body.
We are doing abstraction here so that we can access the method of derived class without any trouble.
Let's have a look!
class program
{
abstract class animal
{
public abstract void eat();
public void sound()
{
Console.WriteLine("dog can sound");
}
}
class dog: animal
{
public override void eat()
{
Console.WriteLine("dog can eat");
}
}
static void Main(string[] args)
{
dog mydog = new dog();
animal thePet = mydog;
thePet.eat();
mydog.sound();
}
}
Finally we created an Object 'mydog' of class dog, but we didn't instantiate any object of Abstract Base Class 'animal'.
According to "Ivor Horton" (a programmer of Java) an object can not be instantiated, but we can declare a variable of the Abstract Class type. If this statement is true then it could be possible:
animal thePet;
This is an object which is declared as thePet and its data type is the abstract base class 'animal'.
We can use this Object to store Objects of the subclass.
In the above code we declare an Object 'thePet', of the type animal (the Abstract Base Class) and simply copy the object of another object (only the reference is copied as they belong to reference type). Now we can use object 'thePet' just as object 'mydog'.
0 comments:
Post a Comment