top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

what is the difference between Class Vs Interface in C#?

+1 vote
55 views

Need to know class definition? Interface definition ? and distinguish between to of them???

posted Dec 3, 2014 by Karthi Kumar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes
 
Best answer

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.

Object
In Object Oriented Programming (OOP) terms, we can say that a Toyota car is an instance of the Class of Objects known as Car. A Class is like the blueprint for a Car. Using this blueprint, you can build as many Cars as you like. Each car you build is an Object of the Class Car. Each Car also has a number, of course. If you want to tell someone which is your car, you give them the number for identifying it. This number is called Object's reference .

Object Oriented Programming

public class Car
{
    String tyres; //Property
    String color; //Property
    void driving() //Method
    {
        //method implementation
    }
    void reverse() //Method
    {
        //method implementation
    }
    static void Main()
    {
        // Declare an instance.
        Car toyota = new Car();
        // Call the member.
        toyota.reverse();
    }
}

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

When a Class implements an Interface are forced by the compiler to write the methods and Properties that the Interface has defined. That is, if you implement an Interface in your Class you have to declare all of the Interface's Events, Methods, Properties in your Class. An Interface can have only Abstract methods and Constants , which are always implicitly public, static, and final.

interface Car
{
    void getModelNo();
}
class Toyota : Car
{
    // Explicit interface member implementation:
    void Car.getModelNo()
    {
        // Method implementation.
    }
    static void Main()
    {
        // Declare an interface instance.
        Car obj = new Toyota();
        // Call the member.
        obj.getModelNo();
    }
}

difference between the Class and Interface
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.

answer Dec 3, 2014 by Shivaranjini
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...