Difference Between Class And Interface

This article explains the differences between classes and interfaces with some examples.

Introduction

 
This article explains the differences between classes and interfaces with some examples.
 

Difference between a class and an interface

  1. The basic difference is that a class has both a definition and an implementation whereas an interface only has a definition. Interfaces are actually implemented via a class. (Due to this an interface supports the concept of multiple inheritances.)
  2. Interfaces are developed in Java to implement the concept of multiple inheritance whereas classes are not used to implement multiple inheritance.
  3. No memory is allocate for the interfaces whereas the memory is allocated for the classes.
  4. Classes are templates for objects. The attributes of objects are defined by the components of the class, that describe the state and behaviour of objects. A class contains attributes, methods and events as its components. Interfaces contain only method prototypes, they don't provide an implementation of its methods, we can provide an implementation of its methods in our class, for that we can implement the interface. An interface is the source for polymorphism.
  5. Interfaces are always implemented whereas classes are always extended.
  6. The access specifiers private, public and protected are possible with classes. But interfaces can have only one specifier, public.

1. Class

 
In real life, we'll find various objects, all are of the same kind. Such as various cars that were built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that our car is an instance of the class of objects known as cars. It is a template for objects you will create or we can say it's a specification that we define and every object we define will follow that pattern. A class is the blueprint from which individual objects are created.
 
The following example shows the implementation of "Car".
  1. class Car   
  2. {  
  3.  int cadence = 11;  
  4.  int speed = 45;  
  5.  int gear = 57;  
  6.  void changeinCadence(int newCadence)   
  7.  {  
  8.   cadence = newCadence;  
  9.  }  
  10.  void changeinGear(int newValue)  
  11.  {  
  12.   gear = newGear;  
  13.  }  
  14.  void speedgoesUp(int incrementation)   
  15.  {  
  16.   speed = speed + incrementation;  
  17.  }  
  18.  void applyBrakes(int decrementation)   
  19.  {  
  20.   speed = speed - decrement;  
  21.  }  
  22.  void printStates()   
  23.  {  
  24.   System.out.println("cadence new:" + cadence + " speed max:" + speed + " gear new:" + gear);  
  25.  }  
  26. }   
Example
 
This example shows the structure of a class. In this class, we define a rectangle class consisting of width, height and depth and in another class (RectangleDemo) we define its value.
  1. class Rectangle   
  2. {  
  3.  double width;  
  4.  double height;  
  5.  double depth;  
  6. }  
  7. class RectangleDemo   
  8. {  
  9.  public static void main(String args[])  
  10.  {  
  11.   Rectangle myrect = new Rectangle();  
  12.   double volume;  
  13.   myrect.width = 10;  
  14.   myrect.height = 15;  
  15.   myrect.depth = 15;  
  16.   volume = myrect.width * myrect.height * myrect.depth;  
  17.   System.out.println("Volume is " + volume);  
  18.  }  
  19. }   
Output
 
 Fig-1.jpg

2. Interface

 
Interfaces are the interaction of objects with the outside world through the methods that they expose. Methods form the object's interface with the outside world; the buttons on the front of your television set. In simple words, an interface is a collection of abstract methods. A class implements many interfaces at a time. An interface is not a class. Writing an interface is similar to writing a class, but they are two various concepts. A class describes the attributes and behaviours of an object. An interface contains behaviours that a class implements.
 
How to declare an interface in Java
 
The interface keyword is used to declare an interface.
 
Example
  1. public interface Animal  
  2. {  
  3.     public void animaleat();  
  4.     public void animaltravel();  
  5. }   
How to implement an interface in Java?
 
A class uses the "implements" keyword to implement an interface. The implements keyword appears in the class declaration following the "extends" portion of the declaration. When a class inherits an interface it needs to modify all objects of the interface.
  1. public class MammalInterface extends Animal   
  2. {  
  3.  public void animaleat()   
  4.  {  
  5.   System.out.println("Mammal start eating");  
  6.  }  
  7.  public void animaltravel()   
  8.  {  
  9.   System.out.println("Mammal start traveling");  
  10.  }  
  11.  public int noOfLegs()   
  12.  {  
  13.   return 2;  
  14.  }  
  15.  public static void main(String args[])   
  16.  {  
  17.   MammalInterface maml = new MammalInterface();  
  18.   maml.animaleat();  
  19.   maml.animaltravel();  
  20.  }  
  21. }   
Output
 
Fig-2.jpg