Difference between Interface vs Class vs Abstract class

Q) What is Interface?

An Interface provides a contract specifying how to create an Object, without caring about the specifics of how they do the things and it has no implementations for any of its members.

An interface is a specification for a set of class members not an implementation. An Interface is a reference type and it contains only abstract members such as Methods, Properties etc. It contain only declaration for its members and implementation defined as separate entities from classes. It can’t contain constants, data fields, constructors, destructors and static members and all the member declarations inside interface are implicitly public.

You can think of an interface as an abstract class that contains only pure virtual functions.The implementation of the methods is done in the class that implements the interface.

*** When a Class implements an Interface, It is 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 Student {

void getInfo();

}

class School : Student {

// Explicit interface member implementation:

void Student.getInfo() {

// Method implementation.

}

static void Main() {

// Declare an interface instance.

Student obj = new School();

// Call the member.

obj.getInfo();

}

}

Q) What is 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

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();
}
 }

Difference between the Interface and Class

  1. 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. i.e A Class has both definition and an implementation whereas Interface only has a definition.
  2. A Class can be instantiated but an Interface cannot be instantiated. You can create an instance of an Object that implements the Interface.
  3. 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.

 

Difference between Abstract Class and Interface

  1. An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance.                                                                                                                                                   Thus a class may inherit several interfaces but only one abstract class.
  2. An interface is an empty shell, just only the signatures of the methods. The methods do not contain anything. The interface can’t do anything. It’s just a pattern.                                                                   An Abstract class is a class which will contains both definition and implementation in it.
  3. Abstract classes can have consts, members and defined methods, whereas interfaces can only have consts and methods.
  4. Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes.                                                           But all methods of an interface must be defined as public.
  5. A child class can define abstract methods with the same or less restrictive visibility.                                              Whereas a class implementing an interface must define the methods with the exact same visibility.
  6. If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.                                                      But if we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.

Q) Can we have Multiple inheritance in Objective C or Swift ?

Ans: In .Net (Microsoft .Net does not support multiple inheritance. The biggest problem with multiple inheritance is that it allows for ambiguity when the compiler needs to find the correct implementation of a virtual method. Microsoft introduce Interface as a solution for multiple inheritance. Microsoft .NET allows you to have a single class in the list of parents , therefore supporting single inheritance, but you are free to implement as many interfaces as you wish.)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s