Sunday, February 4, 2024

What is the difference between abstract class and interface?

 Abstract classes and interfaces are two concepts in object-oriented programming that serve distinct purposes.

Abstract classes, also known as abstract base classes, are classes that cannot be instantiated directly. They can only be used as bases for other classes, providing a common ancestor for inheritance. Abstract classes define methods that must be implemented by any class that inherits from them. In other words, you'll need to write code to fill in the blanks when inheriting from an abstract class.

On the other hand, interfaces are collections of method signatures that declare how a class should behave. They serve as contracts between the code and the outside world. Interfaces cannot be instantiated directly, either. However, they can be implemented by any class that wants to interact with them. Think of interfaces like blueprints for your code's behavior.

To summarize: abstract classes define how a class should behave, while interfaces declare how a class should interact with other classes. 

Here's an example of how abstract classes and interfaces work in C#: 

Let's say you're building a library management system. You could use an abstract class to define the behavior of a book, like this:

public abstract class Book {

    public virtual string GetTitle() { return "The Great Gatsby"; }

    public virtual int GetPageCount() { return 250; }

    // Other methods that must be implemented by any inheriting class

}

Now, let's say you want to create a class called "FictionBook" that inherits from the abstract class "Book". Here's how you could do it:

public class FictionBook : Book {

    public override string GetTitle() { return "The Catcher in the Rye"; } // Implement this method

    public override int GetPageCount() { return 300; } // Implement this method

}

As you can see, by inheriting from the abstract class "Book", the class "FictionBook" is required to implement the methods declared in the abstract class. This way, all books will have a title and page count that can be used uniformly throughout your code. 

Now, let's say you want to create an interface called "PrintableBook". You could define it like this:

public interface PrintableBook {

    public virtual void Print() { } // Declare this method

}

Next, let's say you have a class called "Novel" that wants to be able to interact with the "PrintableBook" interface. Here's how you could do it:

public class Novel : IPrintableBook {

    public void Print() { } // Implement this method

}

By implementing the methods declared in the interface "IPrintableBook", the class "Novel" can interact with any class that implements that interface, like the "FictionBook" class we created earlier. 

No comments:

Post a Comment

What is DaemonSet in Kubernetes

 A DaemonSet is a type of controller object that ensures that a specific pod runs on each node in the cluster. DaemonSets are useful for dep...