Sunday, August 12, 2018

In what scenarios will you use a abstract class and in what scenarios will you use a interface?

Ans:

If you want to increase re usability in inheritance then abstract classes are good. If you want implement or force some methods across classes must be for uniformity you can use a interface. So to increase  re usability via inheritance use abstract class as it is nothing but a base class and to force methods use interfaces.


Abstract Class:

When we have the requirement of a class that contains some common properties or methods with some common properties whose implementation is different for different classes, in that situation, it's better to use Abstract Class then Interface.




Example:

  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4.   
  5. namespace oops1  
  6. {  
  7.     public abstract  class Cars  
  8.     {  
  9.   
  10.         //put all the common functions but diffrent implementation in  abstract method.  
  11.         public abstract double price();  
  12.         public abstract int getTotalSeat();  
  13.         public abstract string colors();  
  14.   
  15.         //put all the common property in normal class  
  16.         public string Wheel()  
  17.         {  
  18.             return "4 wheeler";  
  19.   
  20.         }  
  21.         public string CheckAC()  
  22.         {  
  23.             return "AC is available";  
  24.         }  
  25.         public string CallFacility()  
  26.         {  
  27.             return "Call Facility  supported";  
  28.         } 
  29.    }
  30.  }


Important:


So, in those cases, we will use abstract class where we want to restrict the user from creating the object of parent class because by creating object of parent class, you can't call child class methods. So, the developer has to restrict accidental creation of parent class object by defining it as abstract class.

So, I think, in these ways, we can use abstract class in our real time project.




Interface:





Now, I will narrate a real time scenario where we can use interface. I will go for the same example that I have done earlier.
  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4.   
  5. namespace oops1  
  6. {  
  7.     public   class Cars  
  8.     {  
  9.   
  10.         public string Wheel()  
  11.         {  
  12.             return "4 wheeler";  
  13.   
  14.         }  
  15.         public string CheckAC()  
  16.         {  
  17.             return "AC is available";  
  18.         }  
  19.         public string CallFacility()  
  20.         {  
  21.             return "Call Facility  supported";  
  22.         }  
  23.          
  24.         
  25.   
  26.   
  27.   
  28.     }  
  29.   
  30.      
  31. }  
Now, here is my Toyota class which is derived from Cars.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace oops1  
  8. {  
  9.     public class Toyota : Cars  
  10.     {  
  11.           
  12.   
  13.         static void Main(string[] args)  
  14.         {  
  15.   
  16.   
  17.             Toyota Toy = new Toyota();  
  18.               
  19.              
  20.   
  21.             Console.WriteLine(Toy.CallFacility());  
  22.             Console.WriteLine(Toy.Wheel());  
  23.             Console.WriteLine(Toy.CheckAC());  
  24.              
  25.              
  26.             
  27.             Console.ReadLine();  
  28.   
  29.   
  30.         }  
  31.     }  
  32.   
  33.   
  34. }  
Here is my Hyundai class which is derived from Cars.
  1. using oops1;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.   
  8. namespace oops1  
  9. {  
  10.     public class Hyundai:Cars   
  11.     {  
  12.          
  13.   
  14.         static void Main(string[] args)  
  15.         {  
  16.             Hyundai dust = new Hyundai();  
  17.   
  18.             Console.WriteLine(dust.CallFacility());  
  19.             Console.WriteLine(dust.Wheel());  
  20.             Console.WriteLine(dust.CheckAC());  
  21.              
  22.   
  23.             Console.ReadLine();  
  24.   
  25.   
  26.         }  
  27.     }  
  28.   
  29.      
  30. }  
A new feature for the Hyundai car is Introduced called GPS which is not supported in Toyota cars. Then, how can we implement and which way we can implement these?

Here, we have certain options like
  • Go for a new class defining the GPS method and inherit it to the Hyundai Class.
  • Go for an abstract class and define GPS method and inherit it on Hyundai class and implement the GPS method there.
  • Directly create a method in Hyundai class and consume it.
  • Go for Interface
    .
    CASE 1 - By Using simple class
    Let's find what will happen if we use a class there, and declare a method as GPS and try to inherit in Hyundai class.
    Created a new class as "NewFeatures, as shown below.
    1. class NewFeatures 
    2.    {  
    3.        public void GPS()  
    4.        {  
    5.            Console.WriteLine("GPS supported");  
    6.        }  
    7.    }  
    Now, inherit the Hyundai class from NewFeatures. So, if we check this class, it was previously inherited from Cars class and for now, again inherits from NefFeatures class. So, it gives the following error when you try to run the program.
    1. using oops1;  
    2. using System;  
    3. using System.Collections.Generic;  
    4. using System.Linq;  
    5. using System.Text;  
    6. using System.Threading.Tasks;  
    7.   
    8. namespace oops1  
    9. {  
    10.     public class Hyundai:Cars,NewFeatures  
    11.     {  
    12.          
    13.   
    14.         static void Main(string[] args)  
    15.         {  
    16.             Hyundai hun = new Hyundai();  
    17.   
    18.             Console.WriteLine(hun.CallFacility());  
    19.             Console.WriteLine(hun.Wheel());  
    20.             Console.WriteLine(hun.CheckAC());  
    21.              
    22.   
    23.             Console.ReadLine();  
    24.   
    25.   
    26.         }  
    27.     }  
    28.   
    29.      
    30. }  
    Now, run the program and find out the error.



    This is simple because C# does not support multiple inheritance.
  • CASE 2 - By using Abstract class

    Now, go for abstract class and see what happens.
    1. public abstract class  NewFeatures  
    2.    {  
    3.       abstract public void GPS();  
    4.          
    5.    }  
    Now, let's try to inherit from abstract class.
    1. using oops1;  
    2. using System;  
    3. using System.Collections.Generic;  
    4. using System.Linq;  
    5. using System.Text;  
    6. using System.Threading.Tasks;  
    7.   
    8. namespace oops1  
    9. {  
    10.     public class Hyundai:Cars,NewFeatures  
    11.     {  
    12.        public  override  void GPS()  
    13.         {  
    14.             Console.WriteLine("GPS supported.");  
    15.   
    16.         }  
    17.   
    18.   
    19.         static void Main(string[] args)  
    20.         {  
    21.             Hyundai hun = new Hyundai();  
    22.   
    23.             Console.WriteLine(hun.CallFacility());  
    24.             Console.WriteLine(hun.Wheel());  
    25.             Console.WriteLine(hun.CheckAC());  
    26.              
    27.   
    28.             Console.ReadLine();  
    29.   
    30.   
    31.         }  
    32.     }  
    33.   
    34.      
    35. }  
    So, here is the error I got.

  • CASE 3 - Direct creating a method called GPS() inside Hyundai class
    This is very relevant 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.
  • CASE 4 - By defining Interface
    This is the most important case and we will see how it will solve our problem.Lets define  the  method in an Interface
    1. interface  INewFeatures  
    2.  {  
    3.     void GPS();  
    4.        
    5.  }  
    Now inherit the Interface from Cars and see without implementing the GPS() method.
    1. using oops1;  
    2. using System;  
    3. using System.Collections.Generic;  
    4. using System.Linq;  
    5. using System.Text;  
    6. using System.Threading.Tasks;  
    7.   
    8. namespace oops1  
    9. {  
    10.     public class Hyundai:Cars, INewFeatures  
    11.     {  
    12.   
    13.   
    14.         static void Main(string[] args)  
    15.         {  
    16.             Hyundai hun = new Hyundai();  
    17.   
    18.             Console.WriteLine(hun.CallFacility());  
    19.             Console.WriteLine(hun.Wheel());  
    20.             Console.WriteLine(hun.CheckAC());  
    21.              
    22.   
    23.             Console.ReadLine();  
    24.   
    25.   
    26.         }  
    27.     }  
    28.   
    29.      
    30. }  
    As we have not implemented the method it will show the following error as follow.

    So, the problems which happen in case 3 can be easily solved by using Interface. Now, let's implement the method and see will it solve the problem which arises in Case1 and case2 as follows.
    1. using oops1;  
    2. using System;  
    3. using System.Collections.Generic;  
    4. using System.Linq;  
    5. using System.Text;  
    6. using System.Threading.Tasks;  
    7.   
    8. namespace oops1  
    9. {  
    10.     public class Hyundai:Cars,INewFeatures  
    11.     {  
    12.         public  void GPS()  
    13.         {  
    14.             Console.WriteLine("GPS supported.");  
    15.   
    16.         }  
    17.   
    18.   
    19.         static void Main(string[] args)  
    20.         {  
    21.             Hyundai hun = new Hyundai();  
    22.   
    23.             Console.WriteLine(hun.CallFacility());  
    24.             Console.WriteLine(hun.Wheel());  
    25.             Console.WriteLine(hun.CheckAC());  
    26.             hun.GPS();  
    27.              
    28.   
    29.             Console.ReadLine();  
    30.   
    31.   
    32.         }  
    33.     }  
    34.   
    35.      
    36. }  


    Thus, it resolves the demerits of all the above 3 cases. So, in this situation, we have to use Interface. Here, I have shown you the sketch diagram.
In this way, we can u