Interfaces and Classes

Tom Meier 220 Reputation points
2024-04-12T16:19:35.9133333+00:00

Hi All.

I was trying to get some perspective on whats the difference between a concrete class and an interface type implementation of a class?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,938 questions
0 comments No comments
{count} votes

Accepted answer
  1. Marcin Policht 24,440 Reputation points MVP
    2024-04-12T17:16:55.92+00:00

    A concrete class is a class that provides a complete implementation of all its methods and properties. It can be instantiated directly to create objects. On the other hand, an interface type implementation of a class is a class that implements one or more interfaces. It defines the behavior specified by the interfaces it implements but may also provide additional methods and properties. While concrete classes define specific implementations, interface implementations define contracts that specify the behavior expected from classes that implement them. In essence, concrete classes provide actual functionality, while interface implementations define the structure for that functionality, allowing for greater flexibility and abstraction in programming.

    Here's an example in C# illustrating the difference between a concrete class and an interface type implementation of a class:

    // Define an interface
    public interface IAnimal
    {
        void Speak();
    }
    // Concrete class implementing the interface
    public class Dog : IAnimal
    {
        public void Speak()
        {
            Console.WriteLine("Woof!");
        }
        public void Fetch()
        {
            Console.WriteLine("Fetching the ball!");
        }
    }
    // Concrete class without implementing the interface
    public class Cat
    {
        public void Meow()
        {
            Console.WriteLine("Meow!");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Creating an object of the Dog class (concrete class implementing the interface)
            Dog dog = new Dog();
            dog.Speak(); // Output: Woof!
            dog.Fetch(); // Output: Fetching the ball!
            // Creating an object of the Cat class (concrete class without implementing the interface)
            Cat cat = new Cat();
            // cat.Speak(); // Error: 'Cat' does not contain a definition for 'Speak' and no extension method 'Speak' accepting a first argument of type 'Cat' could be found
            cat.Meow(); // Output: Meow!
        }
    }
    
    

    In this example:

    • IAnimal is an interface with a single method Speak().
    • Dog is a concrete class that implements the IAnimal interface and provides an implementation for the Speak() method. It also defines an additional method Fetch().
    • Cat is a concrete class without implementing the IAnimal interface.
    • When creating objects of the Dog class, we can call both the Speak() method (defined by the interface) and the Fetch() method (defined by the class). However, when creating objects of the Cat class, we can only call the Meow() method, as it does not implement the Speak() method defined by the IAnimal interface

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.