What is difference between class and interface in C#

Hi ,

I am a beginner in C# learning C# from wikitechy , i am not having a clear idea about class and interface.

Where and why i need to use interface ?

please clarify

 

Thanks in advance

Suresh Vijayan

Asked on January 28, 2019 in C#.
Add Comment


  • 23 Answer(s)
    Abstract Class Interface
    It contains both declaration and definition part. It contains only a declaration part.
    Multiple inheritance is not achieved by abstract class. Multiple inheritance is achieved by interface.
    It contain constructor. It does not contain constructor.
    It can contain static members. It does not contain static members.
    It can contain different types of access modifiers like public, private, protected etc. It only contains public access modifier because everything in the interface is public.
    The performance of an abstract class is fast. The performance of interface is slow because it requires time to search actual method in the corresponding class.
    It is used to implement the core identity of class. It is used to implement peripheral abilities of class.
    A class can only use one abstract class. A class can use multiple interface.
    If many implementations are of the same kind and use common behavior, then it is superior to use abstract class. If many implementations only share methods, then it is superior to use Interface.
    Abstract class can contain methods, fields, constants, etc. Interface can only contain methods .
    It can be fully, partially or not implemented. It should be fully implemented.
    Answered on January 28, 2019.
    Add Comment

    An interface is not a class. It is an entity that is defined by the word Interface. ... The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn't support multiple inheritance, interfaces are used to implement multiple inheritance.

    Answered on January 28, 2019.
    Add Comment

     Class :
    Class is a user-defined data type that allows grouping of data members, methods, properties, and events. A class is nothing but a blueprint that defines data and behaviour.

    Interface :
    An Interface contains only the signature of members: methods, properties, events or indexers. It does not contain the definition of these members. It is up to the class which is deriving this interface to write the definition of these member.

                  1.  A class can contain data members and methods with the complete definition. An interface contains the only signature of members .  

                  2.  A class can only be inherited from a single class but can be inherited from more than one interfaces.

                  3.  Interfaces are always implemented whereas classes are extended.  

                  4. Classes represent the “real object” and do all the work. Interfaces allow you to create a program that will manipulate the class in a                     predefined way.

    Answered on January 29, 2019.
    Add Comment

    As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class

    Answered on January 30, 2019.
    Add Comment

    A Class has both definition and an implementation whereas Interface only has a definition.

    A Class can be instantiated but an Interface cannot be instantiated You can create an instance of an Object that implements the Interface.

    A Class is a full body entity with members, methods along with there definition and implementation. An Interface is just a set of definition that you must implement in your Class inheriting that Interface.

    Answered on January 30, 2019.
    Add Comment

    class

    A Class is a specification of how to construct Objects from the same Class . It is a type of blueprint or prototype from which individual Objects are created. We can say a Class is a template that describes the kinds of state and behavior that Objects of its type support.

    What is an Object

     

    interface:

    it is one typeshow to create an Object, without caring about the specifics of how they do the things. An Interface is a reference type and it included only abstract members such as Events, Methods, Properties etc. and it has no implementations for any of its members.

    Answered on January 30, 2019.
    Add Comment

    Difference Between Interface and Abstract Class

    1. Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
    2. Variables declared in a Java interface is by default final. An  abstract class may contain non-final variables.
    3. Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
    4. Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
    5. An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
    6. A Java class can implement multiple interfaces but it can extend only one abstract class.
    7. Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
    8. In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.
    Answered on January 30, 2019.
    Add Comment

    A Class is a specification of how to construct Objects from the same Class. We can say a Class is a template that describes the kinds of state and behavior that Objects of its type support.
    An Interface provides a contract specifying how to create an Object, without caring about the specifics of how they do the things. An Interface is a reference type and it included only abstract members such as Events, Methods, Properties etc. and it has no implementations for any of its members.
    A Class has both definition and an implementation whereas Interface only has a definition

    piccosoft

    Answered on January 31, 2019.
    Add Comment
    • A Class has both definition and an implementation whereas Interface only has a definition.
    • A Class can be instantiated but an Interface cannot be instantiated You can create an instance of an Object that implements the Interface.
    • A Class is a full body entity with members, methods along with there definition and implementation. An Interface is just a set of definition that you must implement in your Class inheriting that Interface.
    Answered on January 31, 2019.
    Add Comment

    Interface:

    An interface provides a contract specifying how to talk to an object, but not the specifics of how that object handles that request (apart from parameters and return types etc)

    Class:

    A class (especially an abstract class) provides both the information on how to talk to an object, but in some cases the actual implementation (think overriding a method of a base class).

    Example Program:

    public interface ILog
    {
       void WriteMessage(string message);
    }
    
    public class ConsoleLogger : ILog
    {
       public void WriteMessage(string message)
       {
          Console.WriteLine(message);
       }
    }
    
    public class MessageBoxLogger : ILog
    {
       public void WriteMessage(string message)
       {
          MessageBox.Show(message);
       }
    }
    
    public void DoSomethingInteresting(ILog logger)
    {
       logger.WriteMessage("I'm doing something interesting!");
    }
    
    

    Because both ConsoleLogger and MessageBoxLogger implement the ILog interface (the WriteMessage method, any part of code can take an ILog without ever needing to know what it actually does, they only care that it does something – in this case, writing a log message.

    So in the code above, either a ConsoleLogger or MessageBoxLogger could be supplied to DoSomethingInteresting it doesn’t matter at all because ILog “knows” how to talk to that object.

    Answered on January 31, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.