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

What Is An Array? Explain Its Type.
Array: Array is a collection of similar type of data in an array varaiable. Array start with Zero Index and maximum length of array is total element - 1. i.e., Suppose n is the length of array, total element...
N

.Net is not tough just it requir skill build
Hello friends which one is better java or .net ? Well you need to know both to make a right full argument. My advice would be pick any one get skilled on that and then learn second one. I believe by...

Object Initializers And Collection Initializers: A Syntactic Sugar In C# 3.0
i. Introduction: Object initializers and Collection initializers are part of C# 3.0.These two concepts add a flexibility, readability, and maintainability in C#. As we are C# developers, we should know...

Best way to learn any software Course
Hi First conform whether you are learning from a real time consultant. Get some Case Studies from the consultant and try to complete with the help of google not with consultant. Because in real time same situation will arise. Thank you

Garbage collector [ GC ] in .NET framework
Garbage collector is responsible for the "automatic memory management - AMM" in .NET framework. Garbage collector is one of the properties of .NET framework components CLR . Why there is a need of "Memory...
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