Introduction
This is now an important question asked by an interviewer in the interview. I have found many solutions on the Internet regarding "When to use interface and abstract method?". Lately, I thought about writing an article on simplifying my experience and what I learned. If you check both the things, they seem to be very similar, which makes a confusion in the mind while answering these questions.
Here, in this article, I will try to explain these things.
Abstract Class
Before giving the introduction about abstract class, I will try to explain the abstract method, first.
Abstract Method
A method without any particular body is known as an abstract method. These methods contain only declaration of the method.To declare an abstract method, we have to use abstract modifier on the method.The class, under which these abstract methods are defined, is referred to as an abstract class, and this also has to be declared using abstract modifier.The implementation of abstract method is done by a derived class. When the abstract class inherits the derived class, the derived class must implement the abstract methods using override keyword in it.
This is now an important question asked by an interviewer in the interview. I have found many solutions on the Internet regarding "When to use interface and abstract method?". Lately, I thought about writing an article on simplifying my experience and what I learned. If you check both the things, they seem to be very similar, which makes a confusion in the mind while answering these questions.
Here, in this article, I will try to explain these things.
Abstract Class
Before giving the introduction about abstract class, I will try to explain the abstract method, first.
Abstract Method
A method without any particular body is known as an abstract method. These methods contain only declaration of the method.To declare an abstract method, we have to use abstract modifier on the method.The class, under which these abstract methods are defined, is referred to as an abstract class, and this also has to be declared using abstract modifier.The implementation of abstract method is done by a derived class. When the abstract class inherits the derived class, the derived class must implement the abstract methods using override keyword in it.
Abstract Class An abstract class is a special class that contains both abstract and non-abstract members in it.
Example
public abstract class Cars
{
//Abstract Methods
public abstract double price();
public abstract int getTotalSeat();
}
Here are some points regarding abstract class.
- Abstract class can contain abstract members as well as non-abstract members in it.
- A class can only inherit from one abstract Class.
- We cannot create object of an abstract class.
It is also user defined type like 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.
Points to remember
- Interface contains only abstract methods.
- We cannot create object of an interface.
- The default scope for a member in Interface is Public. So, no need to use the Public access specifier in the program.
From both the definitions, it gets concluded that,
Figure 1
Figure 2
Now, I have class like Hyundai and Toyota which are derived from a parent class called Car. Here, I have the car methods as follow:
public class CarsHere, I have 2 types of cars which are inherited from Cars.
{
public string Wheel()
{
return "4 wheeler";
}
public string CheckAC()
{
return "AC is available";
}
public string CallFacility()
{
return "Call Facility supported";
}
}
using oops1;Now, it runs as expected and gives the following output .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace oops1
{
public class Hyundai : Cars
{
static void Main(string[] args)
{
Hyundai dust = new Hyundai();
Console.WriteLine(dust.CallFacility());
Console.WriteLine(dust.Wheel());
Console.WriteLine(dust.CheckAC());
Console.ReadLine();
}
}
}
Similarly, as Toyota is a car and it also inherits from Cars class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace oops1
{
public class Toyota : Cars
{
public string DiscountPrice()
{
return "20% discount on buying Toyoya Cars";
}
static void Main(string[] args)
{
Toyota Toy = new Toyota();
Console.WriteLine(Toy.CallFacility());
Console.WriteLine(Toy.Wheel());
Console.WriteLine(Toy.CheckAC());
Console.WriteLine(Toy.DiscountPrice());
Console.ReadLine();
}
}
}
We need some methods which are common to both the classes but their implementation is different.
- PRICE()- Both have price but different price.
- TotalSeat()- Both have total seats but different no. of seats.
- colors()- Both cars are of different colour.
So, here are the options for implementing these methods in both classes.
- Can I go for a normal class?
- Should I use interface here?
- Can I use an abstract 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. Thus, we
will not go forward with Class.
interface IExtraNow, let me first inherit the Toyota class from Cars class and IExtra interface class to achieve our goal.
{
double price();
int getTotalSeat();
string colors();
}
The code is given below.
namespace oops1
{
public class Toyota : Cars,IExtra
{
public string DiscountPrice()
{
return "20% discount on buying Toyoya Cars";
}
public double price()
{
return 1000000.00;
}
public int getTotalSeat()
{
return 5;
}
public string colors()
{
return "Red";
}
static void Main(string[] args)
{
Toyota Toy = new Toyota();
Console.WriteLine(Toy.CallFacility());
Console.WriteLine(Toy.Wheel());
Console.WriteLine(Toy.CheckAC());
Console.WriteLine(Toy.DiscountPrice());
Console.WriteLine("Total ONRoad Price:"+ Toy.price());
Console.WriteLine(Toy.getTotalSeat());
Console.WriteLine(Toy.colors());
Console.ReadLine();
}
}
So, the sketch diagram of this implementation will be like this.
Abstract Class
Now, we will see here how we can solve our problem using Abstract Class.
1. Define an abstract class as Car.
2. Put all the common functionality in simple methods and all the methods whose implementation is different but name is same. Make them Abstract method.
Here is my Car abstract class.
using System;Now, here is my Toyota class which is derived from Cars abstract class.
using System.Collections;
using System.Collections.Generic;
namespace oops1
{
public abstract class Cars
{
//put all the common functions but diffrent implementation in abstract method.
public abstract double price();
public abstract int getTotalSeat();
public abstract string colors();
//put all the common property in normal class
public string Wheel()
{
return "4 wheeler";
}
public string CheckAC()
{
return "AC is available";
}
public string CallFacility()
{
return "Call Facility supported";
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace oops1
{
public class Toyota : Cars
{
public string DiscountPrice()
{
return "20% discount on buying Toyoya Cars";
}
public override double price()
{
return 1000000.00;
}
public override int getTotalSeat()
{
return 5;
}
public override string colors()
{
return "Red";
}
static void Main(string[] args)
{
Toyota Toy = new Toyota();
Console.WriteLine("-------Common property defined commonly in Cars Class----------");
Console.WriteLine(Toy.CallFacility());
Console.WriteLine(Toy.Wheel());
Console.WriteLine(Toy.CheckAC());
Console.WriteLine("-------Own property defined in Toyota class------------");
Console.WriteLine(Toy.DiscountPrice());
Console.WriteLine("-------Common method but implementation is diffrent defined in IExtra Interface------------");
Console.WriteLine("Total ONRoad Price:"+ Toy.price());
Console.WriteLine(Toy.getTotalSeat());
Console.WriteLine(Toy.colors());
Console.ReadLine();
}
}
}
And thus, the result will be the same.
Conclusion
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.
Abstract classes provide you the flexibility to have certain concrete methods and some other methods that the derived classes should implement. On the other hand, if you use interfaces, you would need to implement all the methods in the class that extends the interface. An abstract class is a good choice if you have plans for future expansion.
Conclusion
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.
Abstract classes provide you the flexibility to have certain concrete methods and some other methods that the derived classes should implement. On the other hand, if you use interfaces, you would need to implement all the methods in the class that extends the interface. An abstract class is a good choice if you have plans for future expansion.
Now, I will explain the second use of Abstract Class here.
Imagine,
we have taken a normal parent class as Cars where we have only defined
the common methods. And, my Toyota class is derived from the Cars class.
This will look like the below code.
public class CarsAnd, here is the Toyota class after inheriting from Cars.
{
public string Wheel()
{
return "4 wheeler";
}
public string CheckAC()
{
return "AC is available";
}
public string CallFacility()
{
return "Call Facility supported";
}
}
using System;Here, users know only the Toyota class, its methods, and properties. The user can access the Cars class property and method by creating the object of Toyota class because it is a child class.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace oops1
{
public class Toyota : Cars
{
public string DiscountPrice()
{
return "20% discount on buying Toyoya Cars";
}
static void Main(string[] args)
{
Console.ReadLine();
}
}
}
Now, the main demerit of this kind of Implementation is that it allows the user to create the object of the Parent class (which the user should not be aware of and this is not his strategy). Let's see what happens when the user creates the object.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace oops1
{
public class Toyota : Cars
{
public string DiscountPrice()
{
return "20% discount on buying Toyoya Cars";
}
static void Main(string[] args)
{
//Toyota Toy = new Toyota();
Cars ca = new Cars();
Console.WriteLine("-------Common property defined commonly in Cars Class----------");
Console.WriteLine(ca.CallFacility());
Console.WriteLine(ca.Wheel());
Console.WriteLine(ca.CheckAC());
Console.WriteLine("-------Own property defined in Toyota class------------");
Console.WriteLine(ca.DiscountPrice());
Console.ReadLine();
}
}
}
As we know, we won't be able to access the child class methods using parent class object, this Cars object will not access the DiscountPrice() Method because
it is a child class method. This implementation will not satisfy our
requirement. So, even if the user will create an object of Parent class,
we need to check them because using that object, he can't able to
access the child class method.
So, the main problem we face here is,
As the user is unaware of parent class, he is able to create the object of parent class and unable to access the child class method using parent class. So, to restrict this, we have to do the following things-
- We should not give the permission to create the object of parent class in child class
- We can only allow the user to create the child class object to access both parent class methods and child class methods.
Simple to do this Create the base class as an Abstract class and that will solve our problems as follow.
using System;
using System.Collections;
using System.Collections.Generic;
namespace oops1
{
public abstract class Cars
{
public string Wheel()
{
return "4 wheeler";
}
public string CheckAC()
{
return "AC is available";
}
public string CallFacility()
{
return "Call Facility supported";
}
}
}
- Even if you want to create the object of parent class, it shows the following error.
So, this will solve our problem.
Conclusion
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.
When To Use Interface In Real Time Project?
INTERFACE
It is also user-defined type like a class which only contains abstract members in it and these abstract members should be given implementation under a child class of an interface. A class can inherit from a class or from an interface.
It is also user-defined type like a class which only contains abstract members in it and these abstract members should be given implementation under a child class of an interface. A class can inherit from a class or from an interface.
interface I1
{
void MethodToImplement();
}
In your daily
programming, you are using a lot of interfaces knowingly or unknowingly.
If you are using a List, Dictionary, or Hash Table etc in your project,
then you are indirectly using interface in your project because all
collections are derived from an Interface, IEnumerable.
Here, I will not discuss about the uses of Interface. I will discuss when can we use our interface in C#.
Before explaining in details, I will ask you to focus on the following points.
Here, I will not discuss about the uses of Interface. I will discuss when can we use our interface in C#.
Before explaining in details, I will ask you to focus on the following points.
- In C++, we have a concept of Multiple Inheritance where we find a serious problem called Diamond Problem.
- Thus, in C#, multiple inheritance is not supported. When there is a situation like multiple inheritance, use Interface.
The solution of the multiple inheritance can be provided by Interface. So, we can do this example using Interface as follows. In this way, the diamond problem of Inheritance is solved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OOPs2
{
interface Demo1
{
void print();
}
interface Demo2
{
void print();
}
class Program:Demo1,Demo2
{
void Demo1.print()
{
Console.WriteLine("Debendra");
}
void Demo2.print()
{
Console.WriteLine("Debendra");
}
static void Main(string[] args)
{
Program p = new Program();
((Demo2)p).print();
Console.ReadLine();
}
}
}
using System;Now, here is my Toyota class which is derived from Cars.
using System.Collections;
using System.Collections.Generic;
namespace oops1
{
public class Cars
{
public string Wheel()
{
return "4 wheeler";
}
public string CheckAC()
{
return "AC is available";
}
public string CallFacility()
{
return "Call Facility supported";
}
}
}
using System;Here is my Hyundai class which is derived from Cars.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace oops1
{
public class Toyota : Cars
{
static void Main(string[] args)
{
Toyota Toy = new Toyota();
Console.WriteLine(Toy.CallFacility());
Console.WriteLine(Toy.Wheel());
Console.WriteLine(Toy.CheckAC());
Console.ReadLine();
}
}
}
using oops1;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?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace oops1
{
public class Hyundai:Cars
{
static void Main(string[] args)
{
Hyundai dust = new Hyundai();
Console.WriteLine(dust.CallFacility());
Console.WriteLine(dust.Wheel());
Console.WriteLine(dust.CheckAC());
Console.ReadLine();
}
}
}
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.
class NewFeaturesNow, 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.
{
public void GPS()
{
Console.WriteLine("GPS supported");
}
}
using oops1;Now, run the program and find out the error.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace oops1
{
public class Hyundai:Cars,NewFeatures
{
static void Main(string[] args)
{
Hyundai hun = new Hyundai();
Console.WriteLine(hun.CallFacility());
Console.WriteLine(hun.Wheel());
Console.WriteLine(hun.CheckAC());
Console.ReadLine();
}
}
}
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.
public abstract class NewFeaturesNow, let's try to inherit from abstract class.
{
abstract public void GPS();
}
using oops1;So, here is the error I got.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace oops1
{
public class Hyundai:Cars,NewFeatures
{
public override void GPS()
{
Console.WriteLine("GPS supported.");
}
static void Main(string[] args)
{
Hyundai hun = new Hyundai();
Console.WriteLine(hun.CallFacility());
Console.WriteLine(hun.Wheel());
Console.WriteLine(hun.CheckAC());
Console.ReadLine();
}
}
}
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
interface INewFeaturesNow inherit the Interface from Cars and see without implementing the GPS() method.
{
void GPS();
}
using oops1;As we have not implemented the method it will show the following error as follow.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace oops1
{
public class Hyundai:Cars, INewFeatures
{
static void Main(string[] args)
{
Hyundai hun = new Hyundai();
Console.WriteLine(hun.CallFacility());
Console.WriteLine(hun.Wheel());
Console.WriteLine(hun.CheckAC());
Console.ReadLine();
}
}
}
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.
using oops1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace oops1
{
public class Hyundai:Cars,INewFeatures
{
public void GPS()
{
Console.WriteLine("GPS supported.");
}
static void Main(string[] args)
{
Hyundai hun = new Hyundai();
Console.WriteLine(hun.CallFacility());
Console.WriteLine(hun.Wheel());
Console.WriteLine(hun.CheckAC());
hun.GPS();
Console.ReadLine();
}
}
}
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 use abstract class and interface in our project depending on the condition.
Hope this article gives you an idea about when to use these 2 important things.If you have any suggestions, please feel free to comment so that I can include that in this article.
SOURCE: http://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/