(In object-oriented software, complexity is managed by using abstraction. Abstraction is a process that involves identifying the critical behavior of an object and eliminating irrelevant and complex denials. Abstraction is a process of identifying the relevant qualities and behaviors an object should possess.
Example- A Laptop consists of many things such as processor, motherboard, RAM, keyboard, LCD screen, wireless antenna, web camera, USB ports, battery, speakers etc. To use it, you don't need to know how internally LCD screens, keyboard, web camera, battery, wireless antenna, speaker’s works. You just need to know how to operate the laptop by switching it on.)
The purpose of an abstract class is to provide a common definition of base class that multiple derived classes can share, and can be used only as a base class and never want to create the object of this class. Any class can be converted into abstract class by adding abstract modifier to it. Common features of an abstract class:
- An abstract class cannot be instantiated.
- An abstract class can contain abstract as well as non-abstract methods.
- An abstract method cannot have it implementation in abstract class itself.
- An abstract class can never have a sealed modifier, because it is opposite to the abstract class, an abstract class is created to inherit while a sealed class cannot be inherited.
- A class cannot be inherited from multiple abstract classes but from multiple interfaces.
- An abstract cannot be inherited from a class and multiple interfaces.
- A property can also be abstract in an abstract class.
- An abstract method is by default a virtual.
- An abstract method can only be declared in an abstract class.
- All derived classes must implement all the abstract methods in it.
- Abstarct class can have constructors(no build error)
- Abstaract class inherit from another Abstarct class (no build error)
An abstract class cab be created by using abstract modifier
public abstract class SomeClass
{
// Class members here.
}
Let’s create an abstract class “Person” with a non-abstract and an abstract method
public abstract class Person
{
public void CanWalk()
{
Console.WriteLine("Yes, I can walk");
}
public abstract void CanSpeakLanguages();
}
In this example CanWalk is concrete method while CanSpeakLanguages is an abstract method which must be implemented in derived classes. Now let’s derived some classes from our Abstract Person class, I used constructor to print the class name
public class American : Person
{
public American()
{
Console.WriteLine("American Class:");
}
public override void CanSpeakLanguages()
{
Console.WriteLine("I can speak English");
}
}
public class Indian : Person
{
public Indian()
{
Console.WriteLine("Indian Class:");
}
public override void CanSpeakLanguages()
{
Console.WriteLine("I can speak Hindi, Urdu and English");
}
}
public class Chinese : Person
{
public Chinese()
{
Console.WriteLine("Chinese Class:");
}
public override void CanSpeakLanguages()
{
Console.WriteLine("I can speak Mandolin");
}
}
Now we will test our classes:
Person person = new American();
person.CanWalk();
person.CanSpeakLanguages();
person = new Indian();
person.CanWalk();
person.CanSpeakLanguages();
person = new Chinese();
person.CanWalk();
person.CanSpeakLanguages();
Here is the output:
American Class:
Yes, I can walk
I can speak English
Indian Class:
Yes, I can walk
I can speak Hindi, Urdu and English
Chinese Class:
Yes, I can walk
I can speak Mandolin
As we can see here CanWalk function is printing the same content every time from every class because it is not overridden in derived classes. So it suggest us, we can use an abstract class where some methods are same for all the derived classes and some are different and need to be implemented separately in derived classes.
Abstract properties
Abstract property is similar to abstract method except the declaration 1. A static property cannot be abstract 2. Similar to abstract method, abstract properties are by default virtual. 3. An abstract property must be implemented in its derived classes.
Let’s see it in action
public abstract class Person
{
public abstract String Name
{
get;
set;
}
// Other code
}
And here is the derived American class
public class American : Person
{
Int32 _age;
public override Int32 Age
{
get
{
return _age;
}
set
{
_age = value;
}
}
//Other code
}
Abstract class inherited from Interface(s)
This article cannot be completed until we will discuss abstract class inherited from an interface or from multiple interfaces. Let’s say we have to inherit an abstract class from an interface, do we need to implement all the inherited methods in a derived abstract class or we can use them as abstract methods? Answer is up to you, which method you want to implement you can and which you don’t want to implement mark them abstract e.g.
public interface IPerson
{
String CanWalk();
String CanSpeak();
}
public abstract class Person : IPerson
{
public String CanWalk()
{
return "Yes, I can walk";
}
public abstract String CanSpeak();
}
As we can see we implemented CanWalk method while CanSpeak is marked as an abstract method which can be implemented in derived classes
No comments:
Post a Comment