Tuesday 7 January 2020

Example of Abstract Class in C#

(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:
  1. An abstract class cannot be instantiated.
  2. An abstract class can contain abstract as well as non-abstract methods.
  3. An abstract method cannot have it implementation in abstract class itself.
  4. 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.
  5. A class cannot be inherited from multiple abstract classes but from multiple interfaces.
  6. An abstract cannot be inherited from a class and multiple interfaces.
  7. A property can also be abstract in an abstract class.
  8. An abstract method is by default a virtual.
  9. An abstract method can only be declared in an abstract class.
  10. All derived classes must implement all the abstract methods in it.
  11. Abstarct class can have constructors(no build error)
  12. Abstaract class inherit from another Abstarct class (no build error)
An abstract class cab be created by using abstract modifier
  1. public abstract class SomeClass
  2. {
  3.   // Class members here.
  4. }
Let’s create an abstract class “Person” with a non-abstract and an abstract method
  1. public abstract class Person
  2. {
  3.     public void CanWalk()
  4.     {
  5.         Console.WriteLine("Yes, I can walk");
  6.     }
  7.     public abstract void CanSpeakLanguages();
  8. }
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
  1. public class American : Person
  2. {
  3.     public American()
  4.     {
  5.         Console.WriteLine("American Class:");
  6.     }
  7.     public override void CanSpeakLanguages()
  8.     {
  9.         Console.WriteLine("I can speak English");
  10.     }
  11. }
  12. public class Indian : Person
  13. {
  14.     public Indian()
  15.     {
  16.         Console.WriteLine("Indian Class:");
  17.     }
  18.     public override void CanSpeakLanguages()
  19.     {
  20.         Console.WriteLine("I can speak Hindi, Urdu and English");
  21.     }
  22. }
  23. public class Chinese : Person
  24. {
  25.     public Chinese()
  26.     {
  27.         Console.WriteLine("Chinese Class:");
  28.     }
  29.     public override void CanSpeakLanguages()
  30.     {
  31.         Console.WriteLine("I can speak Mandolin");
  32.     }
  33. }
Now we will test our classes:
  1. Person person = new American();
  2. person.CanWalk();
  3. person.CanSpeakLanguages();
  4. person = new Indian();
  5. person.CanWalk();
  6. person.CanSpeakLanguages();
  7. person = new Chinese();
  8. person.CanWalk();
  9. person.CanSpeakLanguages();
Here is the output:
  1. American Class:
  2.   Yes, I can walk
  3.   I can speak English
  4. Indian Class:
  5.   Yes, I can walk
  6.   I can speak Hindi, Urdu and English
  7. Chinese Class:
  8.   Yes, I can walk
  9.   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
  1. public abstract class Person
  2. {
  3.     public abstract String Name
  4.     {
  5.         get;
  6.         set;
  7.     }
  8.     // Other code
  9. }
And here is the derived American class
  1. public class American : Person
  2. {
  3.     Int32 _age;
  4.     public override Int32 Age
  5.     {
  6.         get
  7.         {
  8.             return _age;
  9.         }
  10.         set
  11.         {
  12.             _age = value;
  13.         }
  14.     }
  15.     //Other code
  16. }
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.
  1. public interface IPerson
  2. {
  3.     String CanWalk();
  4.     String CanSpeak();
  5. }
  6. public abstract class Person : IPerson
  7. {
  8.     public String CanWalk()
  9.     {
  10.         return "Yes, I can walk";
  11.     }
  12.     public abstract String CanSpeak();
  13. }
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

Baisic Useful Git Commands

  Pushing a fresh repository Create a fresh repository(Any cloud repository). Open terminal (for mac ) and command (windows) and type the be...