Introduction
This article explains the differences between classes and interfaces with some examples.
Difference between a class and an interface
- 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.)
- Interfaces are developed in Java to implement the concept of multiple inheritance whereas classes are not used to implement multiple inheritance.
- No memory is allocate for the interfaces whereas the memory is allocated for the classes.
- 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.
- Interfaces are always implemented whereas classes are always extended.
- 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".
class Car
{
int cadence = 11;
int speed = 45;
int gear = 57;
void changeinCadence(int newCadence)
{
cadence = newCadence;
}
void changeinGear(int newValue)
{
gear = newGear;
}
void speedgoesUp(int incrementation)
{
speed = speed+incrementation;
}
void applyBrakes(int decrementation)
{
speed = speed - decrement;
}
void printStates()
{
System.out.println("cadence new:" + cadence + " speed max:" + speed + " gear new:" + gear);
}
}
Example
This examples 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.
class Rectangle
{
double width;
double height;
double depth;
}
// This class declares an object of type Rectangle.
class RectangleDemo
{
public static void main(String args[])
{
Rectangle myrect=new Rectangle();
double volume;
// assign values to myrect's instance variables
myrect.width = 10;
myrect.height = 15;
myrect.depth = 15;
// compute volume of rectangle
volume=myrect.width*myrect.height*myrect.depth;
System.out.println("Volume is " + volume);
}
}
Output
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 interface in Java
The interface keyword is used to declare an interface.
Example
public interface Animal
{
public void animaleat();
public void animaltravel();
}
How to implement 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.
public class MammalInterface extends Animal
{
public void animaleat()
{
System.out.println("Mammal start eating");
}
public void animaltravel()
{
System.out.println("Mammal start traveling");
}
public int noOfLegs()
{
return 2;
}
public static void main(String args[])
{
MammalInterface maml = new MammalInterface();
maml.animaleat();
maml.animaltravel();
}
}
Output