Friday, May 12, 2023

Difference between abstract class and interface

 Difference between abstract class and interface in C#


In C#, both abstract classes and interfaces provide a way to define a set of rules for how classes can interact with each other, but there are some key differences between them:


1. Implementation: An abstract class can have both abstract and non-abstract methods, while an interface can only define method signatures. This means that an abstract class can provide some implementation details, while an interface only defines a contract for how classes should behave.


2. Inheritance: A class can only inherit from one abstract class, but it can implement multiple interfaces. This means that interfaces provide a more flexible way to define behavior across multiple classes.


3. Accessibility: Members of an interface are public by default, while members of an abstract class can have any access modifier. This means that an abstract class can have members that are only visible within the class hierarchy, while an interface only defines public members.


4. Purpose: An abstract class is typically used to provide a base class for a set of related classes, while an interface is used to define a contract for how unrelated classes should behave. In other words, an abstract class defines a common set of behaviors for a family of related classes, while an interface defines a common set of behaviors for unrelated classes.


Here's an example of an abstract class:



public abstract class Shape

{

    public abstract double GetArea();


    public void PrintDetails()

    {

        Console.WriteLine($"Area: {GetArea()}");

    }

}


In this example, the "Shape" abstract class defines an abstract method called "GetArea", which must be implemented by any class that derives from the "Shape" class. The class also has a non-abstract method called "PrintDetails", which can be used by any derived class. The class hierarchy might look like this:


public class Rectangle : Shape

{

    public double Width { get; set; }

    public double Height { get; set; }


    public override double GetArea()

    {

        return Width * Height;

    }

}


public class Circle : Shape

{

    public double Radius { get; set; }


    public override double GetArea()

    {

        return Math.PI * Radius * Radius;

    }

}


In this example, both the "Rectangle" and "Circle" classes derive from the "Shape" abstract class and implement the "GetArea" method in their own way.


Here's an example of an interface:



public interface IShape

{

    double GetArea();

}


In this example, the "IShape" interface defines a method called "GetArea", which must be implemented by any class that implements the "IShape" interface. Classes might implement the interface like this:


public class Rectangle : IShape

{

    public double Width { get; set; }

    public double Height { get; set; }


    public double GetArea()

    {

        return Width * Height;

    }

}


public class Circle : IShape

{

    public double Radius { get; set; }


    public double GetArea()

    {

        return Math.PI * Radius * Radius;

    }

}



In this example, both the "Rectangle" and "Circle" classes implement the "IShape" interface and provide their own implementation for the "GetArea" method.

No comments:

Post a Comment

If you have any query kindly let me know.

Blazor drawback| drawback of blazor| Disadvantage of blazor in c#

  Blazor drawback| drawback of blazor| Disadvantage of blazor in c# While Blazor offers many advantages, it also has a few drawbacks to cons...