UrbanPro
true

Learn C# .NET from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Difference between Abstract Class and Interface

Deep Narayan Hore
07/01/2017 0 0
This is probably one of the most commonly asked questions in any job interview for .Net. Before we look into the differences between the two lets find out the main features.
 
Abstract Class
By definition abstract class must have one abstract method meaning the class must have one method with abstract keyword attached with it along with its declaration. other members of the class are optional. We can have multiple non abstract methods or fields in an abstract class. We can use the below code to discuss the features of an abstract class
 public abstract class car  
{
public car() // abstract class constructor
{
Console.WriteLine("Abstract Class constructor");
}
public int noofWheels = 4; // non abstract fields
public int speed = 60; // non abstract fields
public int getSpeed() // non abstract method
{
return speed;
}
public abstract void features(); // abstract method
}
public class Audi : car
{
public Audi()// inherited class constructor
{
Console.WriteLine("Audi Class constructor");
}
private string carType = "Audi";
public override void features() // abstract method overriding
{
Console.WriteLine(carType);
}
}
class Program
{
static void Main(string[] args)
{
Audi a = new Audi();
a.features();
Console.ReadLine();
}
}
OutPut
 
In the above example we can see the abstract class car with abstract method features, non abstract method getSpeed and fields noofWheels, speed. We have inherited the abstract car class in Audi class and over ridden the abstract method features(). In order to find the flow of execution we have written code blocks inside the constructor of both car and Audi class.
 
Interface
The interface has methods with only definition with out any declaration of methods, interface should not have any fields as well. In order to use we need to over ride all the methods while implementing the interface, meaning with in an interface all member methods are going to be abstract method. We can use the below code to discuss the features of an interface.
public interface Icar  
{
void features();
string manufacturer();
}
public class Audi : Icar
{
public Audi()// inherited class constructor
{
Console.WriteLine("Audi Class constructor");
}
private string carType = "Audi A8";
private string carManufacturer = "Audi";
public override void features()
{
Console.WriteLine(carType);
}
public string manufacturer()
{
return carManufacturer;
}
class Program
{
static void Main(string[] args)
{
Audi a = new Audi();
a.features();
Console.WriteLine(a.manufacturer());
Console.ReadLine();
}
}


Output
 
Differences
  1. Abstract Class: Non abstract methods are allowed with in the class Interface: All member methods are abstract
  2. Abstract Class: Both methods are fields are allowed with in an abstract class Interface: Only methods are allowed
  3. Abstract Class: Can't inherit multiple abstract classes together with in a class, thus abstract class doesn't allow multiple inheritance Interface: Can inherit multiple interfaces with in a class, multiple inheritance in C# can only achieved through Interface
  4. Abstract Class: Can have constructor in an abstract class Interface: Interface doesn't have constructor as its interface provides only contract with the inherited class not actual memory allocation takes place in interface.
  5. Abstract Class: Abstract class might not provide multiple inheritance but it doesn't affect the basic architecture of a design Interface: Interface provides multiple inheritance but its a weak form of multiple inheritance so in case of complex projects it could affect the architecture of the application.
0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Multi-core CPU utilization using Task Parallel Library in c#
Nowadays, we are using multiple core systems. We must write our .NET applications in such a way that we must utilise the complete computing power of the machine. The parallel task library(TPL) allows...

Extension Methods in C#
Extension methods enables you to add methods to existing types without creating a new derived type, recompiling or otherwise modifying the original type. Extension methods are special type of static methods,...

CLR [ Common Language Runtime ] and it's properties
CLR is one of the components of the .NET framework which provides an environment to execute the .NET code or the managed code. , CLR helps in converting the MSIL/CIL code into native code and running...

Steps to fill drop-down value from database to view page in ASP.Net MVC Application
In simple few steps you can fill the dynamic data from database to HTML page in MVC Application Write connection string in web config file Create table related getter setter in model Fetch data...

Software Development Training In Jaipur
Satyam Web Solution provides website designing &development and software designing &development training in Jaipur for various stream’s students. MCA 6 month Industrial Training/Internship B....
X

Looking for C# .NET Classes?

The best tutors for C# .NET Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C# .NET with the Best Tutors

The best Tutors for C# .NET Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more