Wednesday 24 December 2014

Abstraction 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.
Note- When derived class inherited with abstract class; derived class must be override abstract class methods.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AccessModifiers
{
    class Program
    {
        public abstract class Person
        {
            public void CanWalk()
            {
                Console.WriteLine("Yes, I can walk");
            }
            public abstract void CanSpeakLanguages();
        }
        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");
            }
        }
        static void Main(string[] args)
        {
            Person objperson = new American ();
            objperson.CanWalk();
            objperson.CanSpeakLanguages();

            Person objperson1 = new Indian();
            objperson1.CanWalk();
            objperson1.CanSpeakLanguages();

            Person objperson2 = new Chinese();
            objperson2.CanWalk();
            objperson2.CanSpeakLanguages();


            Console.ReadLine();

        }
    }
}

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 class inherited from Interface :-

Features of Abstract Class

  1. An abstract class cannot be instantiated.
  2. An abstract class contain abstract members as well as non-abstract members.
  3. An abstract class cannot be a sealed class because the sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
  4. A non-abstract class which is derived from an abstract class must include actual implementations of all the abstract members of parent abstract class.
  5. An abstract class can be inherited from a class and one or more interfaces.
  6. An Abstract class can has access modifiers like private, protected, internal with class members. But abstract members cannot have private access modifier.
  7. An Abstract class can has instance variables (like constants and fields).
  8. An abstract class can has constructors and destructor.
  9. An abstract method is implicitly a virtual method.
  10. Abstract properties behave like abstract methods.
  11. An abstract class cannot be inherited by structures.
  12. An abstract class cannot support multiple inheritance.

Common design guidelines for Abstract Class

  1. Don't define public constructors within abstract class. Since abstract class cannot be instantiate and constructors with public access modifiers provides visibility to the classes which can be instantiated.
  2. Define a protected or an internal constructor within an abstract class. Since a protected constructor allows the base class to do its own initialization when sub-classes are created and an internal constructor can be used to limit concrete implementations of the abstract class to the assembly which contains that class.

When to use

  1. Need to create multiple versions of your component since versioning is not a problem with abstract class. You can add properties or methods to an abstract class without breaking the code and all inheriting classes are automatically updated with the change.
  2. Need to to provide default behaviors as well as common behaviors that multiple derived classes can share and override.


No comments:

Post a Comment