Abstract Class & Interface

23:31


Introduction

It is very common question in interview Why we use Interface and Abstract Class. I'm trying to explain in which situation we have need to use Interface and Abstract Class.

What is Abstract Class?

An abstract class is a special class that contains both abstract and non-abstract members in it.

What is Abstract Method?

Abstract method is a method which must be contains abstract keyword. The implementation of abstract method is done by a derived class. When the abstract class inherits by the derived class, the derived class must implement the abstract methods using override keyword in it.
Properties Of Abstract Class:
I) Abstract class can contain abstract members as well as non-abstract members in it.
II) A class can only inherit from one abstract Class.
III) We cannot create object of an abstract class.
Example:
    public abstract class Mobile
    { 
        public abstract double price();
        public abstract int screenSize();
        public abstract string colors();
    }

What is Interface?

It is a class which only contains abstract members in it. These abstract members should be given the implementation under a child class of an interface. A class can be inherited from a class or from an interface.
Properties Of Interface:
I) Interface contains only abstract methods.
II) We cannot create object of an interface.
III) The Interface member is by default Public. So, no need to use the Public access specifier in the program.

When We Use Interface / Abstract Class

Lets create a simple application to explain why we need Interface where abstract class can do the same things except Multiple Inheritance.
At first we create a Mobile class in a console application. In this class we have create 3 method of a mobile common features. Class is below:
    public class Mobile
    {
        public string Calling()
        {
            return "Call Supported";
        }
        public string SendSMS()
        {
            return "SMS Supported";
        }
        public string Support_3G()
        {
            return "3G Supported";
        }
    }
Now we create two another class Nokia and Apple inherites from Mobile class.
    public class Apple : Mobile
    {
        static void Main(string[] args)
        {
            Apple app = new Apple();
            Console.WriteLine(app.Support_3G());
            Console.WriteLine(app.Calling());
            Console.WriteLine(app.SendSMS());
            Console.ReadLine();
        }
    }
Output:

    public class Nokia : Mobile
    {
        public string DiscountPrice()
        {
            return "20% discount on buying Nokia Mobile";
        }
        static void Main(string[] args)
        {
            Nokia nok = new Nokia();
            Console.WriteLine(nok.Support_3G());
            Console.WriteLine(nok.Calling());
            Console.WriteLine(nok.SendSMS());
            Console.WriteLine(nok.DiscountPrice());
            Console.ReadLine();
        }
    }
Output:

Now we need some methods which are common to both the classes but their implementation is different.
I) Price() Both have different
II) ScreenSize() Both screen size is different
III) Color() Both mobiles are different color.
Below three options that we resolved our problem:
I) We can use a Normal Calss
II) We can use Interface
III) We can use an Abstract Class
I) Normal Class: If we are taking class, then we can only write normal methods having common implementation there. But, this will not satisfy our requirement because we need separate implementations in both the classes.
II) Interface: Now we create an Interface to resolved our issue
    interface IMobile
    {
        double price();
        int screenSize();
        string colors();
    }
Now we inherite interface in our class like below:
    public class Nokia : Mobile, IMobile
    {
        public double price()
        {
            return 20000.00;
        }
        public int screenSize()
        {
            return 5;
        }
        public string colors()
        {
            return "Black";
        }
        public string DiscountPrice()
        {
            return "20% discount on buying Nokia Mobile";
        }
        static void Main(string[] args)
        {
            Nokia nok = new Nokia();
            Console.WriteLine(nok.Support_3G());
            Console.WriteLine(nok.Calling());
            Console.WriteLine(nok.SendSMS());
            Console.WriteLine(nok.DiscountPrice());
            Console.ReadLine();
        }
    }
Output:

    public class Apple : Mobile, IMobile
    {
        public double price()
        {
            return 90000.00;
        }
        public int screenSize()
        {
            return 5;
        }
        public string colors()
        {
            return "Gray";
        }
        static void Main(string[] args)
        {
            Apple app = new Apple();
            Console.WriteLine(app.Support_3G());
            Console.WriteLine(app.Calling());
            Console.WriteLine(app.SendSMS());
            Console.WriteLine(app.price());
            Console.WriteLine(app.screenSize());
            Console.WriteLine(app.colors());
            Console.ReadLine();
        }
    }
Output:

III) Use Abstract Class:
We have define Mobile as an Abstract Class and put all the common functionality in simple methods and all the methods whose implementation is different but name is same. Make them Abstract method.
    public abstract class Mobile
    {
        //--------------------------put all the common functions but diffrent implementation in abstract method.  
        public abstract double price();
        public abstract int screenSize();
        public abstract string colors();
        //--------------------------put all the common property in normal class  
        public string Calling()
        {
            return "Call Supported";
        }
        public string SendSMS()
        {
            return "SMS Supported";
        }
        public string Support_3G()
        {
            return "3G Supported";
        }
    }
Now our Nokia class will derived from Mobile abstract class.
    public class Nokia : Mobile
    {
        public string DiscountPrice()
        {
            return "20% discount on buying Nokia Mobile";
        }
        public override double price()
        {
            return 200.00;
        }
        public override int screenSize()
        {
            return 5;
        }
        public override string colors()
        {
            return "Red";
        }
        static void Main(string[] args)
        {
            Nokia nok = new Nokia();
            Console.WriteLine("-------Common property defined commonly in Mobile Class----------");
            Console.WriteLine(nok.Support_3G());
            Console.WriteLine(nok.Calling());
            Console.WriteLine(nok.SendSMS());
            Console.WriteLine("-------Own property defined in Nokia class------------");
            Console.WriteLine(nok.DiscountPrice());
            Console.WriteLine("-------Common method but implementation is diffrent defined in IMobile Interface------------");
            Console.WriteLine("Total ONRoad Price:" + nok.price());
            Console.WriteLine(nok.screenSize());
            Console.WriteLine(nok.colors());
            Console.ReadLine();
        }
    }
Output:

    public class Apple : Mobile
    {
        public override double price()
        {
            return 100.00;
        }
        public override int screenSize()
        {
            return 5;
        }
        public override string colors()
        {
            return "Blue";
        }
        static void Main(string[] args)
        {
            Apple app = new Apple();
            Console.WriteLine(app.Support_3G());
            Console.WriteLine(app.Calling());
            Console.WriteLine(app.SendSMS());
            
            Console.WriteLine("-------Common method but implementation is diffrent defined in IMobile Interface------------");
            Console.WriteLine("Total Price:" + app.price());
            Console.WriteLine(app.screenSize());
            Console.WriteLine(app.colors());
            Console.ReadLine();
        }
    }
Output:

Why Interface

In our daily programming, we are using a lot of interfaces knowingly or unknowingly. If we are using a List, Dictionary, or Hash Table etc in your project, then we are indirectly using interface in your project because all collections are derived from an Interface, IEnumerable. Here, I will discuss when can we use our interface in C#.
In C#, multiple inheritance is not supported. When there is a situation like multiple inheritance, use Interface.
Real World Example:
Now we have create a new class Mobile which contains a mobile common features.
    public class Mobile
    {
        public string Calling()
        {
            return "Call Supported";
        }
        public string SendSMS()
        {
            return "SMS Supported";
        }
        public string Support_3G()
        {
            return "3G Supported";
        }
    }
Now our Mobile class is derived into two other classes Nokia and Apple
    class Nokia : Mobile
    {
        static void Main(string[] args)
        {
            Nokia nok = new Nokia();
            Console.WriteLine(nok.Support_3G());
            Console.WriteLine(nok.Calling());
            Console.WriteLine(nok.SendSMS());
            Console.ReadLine();
        }
    }
    
    public class Apple : Mobile
    {
        static void Main(string[] args)
        {
            Apple app = new Apple();
            Console.WriteLine(app.Support_3G());
            Console.WriteLine(app.Calling());
            Console.WriteLine(app.SendSMS());
            Console.ReadLine();
        }
    }
Suppose a new features for the Apple mobile is Introduced called GPS which is not supported in Nokia mobile. Then, how can we implement and which way we can implement these?
There are four options below:
I) Go for a new class defining the GPS method and inherit it to the Apple Class.
II) Go for an abstract class and define GPS method and inherit it on Apple class and implement the GPS method there.
III) Directly create a method in Apple class and consume it.
IV) Go for Interface
I) Using normal class:
Create a new class name ExtraFeature
   class ExtraFeature 
   {  
       public void GPS()  
       {  
           Console.WriteLine("GPS supported");  
       }  
   } 
Now we inherit from ExtraFeature class.
    public class Mobile
    {
        public string Calling()
        {
            return "Call Supported";
        }
        public string SendSMS()
        {
            return "SMS Supported";
        }
        public string Support_3G()
        {
            return "3G Supported";
        }
    }
    class Nokia : Mobile
    {
        static void Main(string[] args)
        {
            Nokia nok = new Nokia();
            Console.WriteLine(nok.Support_3G());
            Console.WriteLine(nok.Calling());
            Console.WriteLine(nok.SendSMS());
            Console.ReadLine();
        }
    }
    public class Apple : Mobile, ExtraFeature
    {
        public void GPS()
        {
            Console.WriteLine("GPS Supported");
        }
        static void Main(string[] args)
        {
            Apple app = new Apple();
            Console.WriteLine(app.Support_3G());
            Console.WriteLine(app.Calling());
            Console.WriteLine(app.SendSMS());
            Console.ReadLine();
        }
    }
Now, inherit the Apple class from ExtraFeature. So, if we check this class, it was previously inherited from Mobile class and for now, again inherits from ExtraFeature class. So, it thrown the error because C# does not support multiple inheritance.
After run this application error is thrown below:

II) Using Abstract class
Now we create a new abstract class.
    public abstract class ExtraFeature
    {
        abstract public void GPS();
    }
So we try to inherit from abstract class.
    public class Mobile
    {
        public string Calling()
        {
            return "Call Supported";
        }
        public string SendSMS()
        {
            return "SMS Supported";
        }
        public string Support_3G()
        {
            return "3G Supported";
        }
    }
    class Nokia : Mobile
    {
        static void Main(string[] args)
        {
            Nokia nok = new Nokia();
            Console.WriteLine(nok.Support_3G());
            Console.WriteLine(nok.Calling());
            Console.WriteLine(nok.SendSMS());
            Console.ReadLine();
        }
    }
    public class Apple : Mobile, ExtraFeature
    {
        public override void GPS()
        {
            Console.WriteLine("GPS Supported");
        }
        static void Main(string[] args)
        {
            Apple app = new Apple();
            Console.WriteLine(app.Support_3G());
            Console.WriteLine(app.Calling());
            Console.WriteLine(app.SendSMS());
            Console.ReadLine();
        }
    }
After run the application the error is thrown below:

III) Use direct creating a method called GPS() inside Apple class
This is very simple way to use a unique method but it has a small problem. If for any reason we forget to create such common method, then it will not ask to write methods. Today, we are using one unique method so that is OK we can remember and write the method. Suppose, we have hundreds of such common methods and we forget to write 2 of them then it will run but not give the expected output, so we have to skip the way and go for the fourth one by defining interface.
IV) Create an Interface
We create an Interface below:
    interface ExtraFeature
    {
        void GPS();
    }
Now we inherit this from Apple class below:
    public class Mobile
    {
        public string Calling()
        {
            return "Call Supported";
        }
        public string SendSMS()
        {
            return "SMS Supported";
        }
        public string Support_3G()
        {
            return "3G Supported";
        }
    }
    class Nokia : Mobile
    {
        static void Main(string[] args)
        {
            Nokia nok = new Nokia();
            Console.WriteLine(nok.Support_3G());
            Console.WriteLine(nok.Calling());
            Console.WriteLine(nok.SendSMS());
            Console.ReadLine();
        }
    }
    public class Apple : Mobile, ExtraFeature
    {
        public void GPS()
        {
            Console.WriteLine("GPS Supported");
        }
        static void Main(string[] args)
        {
            Apple app = new Apple();
            Console.WriteLine(app.Support_3G());
            Console.WriteLine(app.Calling());
            Console.WriteLine(app.SendSMS());
            app.GPS();
            Console.ReadLine();
        }
    }
After run the application output is below:

Thus, it resolves our problem So, in this situation, we have to use Interface.

Download

You can download application zip file here - 109 KB

You Might Also Like

0 comments