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

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....

What are various validator in ASP.NET 2.0 and features common in all validation control in ASP.NET
Control to validate an error message. Are common feature in all the controls.Various validators are requiredfeild validator ,compare validator ,regular expression validator,custom validator,range validator...

Service-Level Agreement (SLA) Definition
A service-level agreement (SLA) is a contract between a service provider and its internal or external customers that documents what services the provider will furnish. SLAs originated with network service...
M

Mohammad Shafi

0 0
0

What is the need for Restful Service in MVC Core?
EXPLANATIONRestful service makes access to client information send to the server by JSON request that reduces several round trips from the server and hence optimises the speed. HTML framework works the...

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
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