What are the differences between a class and an interface in Java?

Java 8ProgrammingObject Oriented Programming

This article will help you understand what differences are there between a Class and an Interface in Java.

First let’s talk about Objects.

OBJECT

In Object Oriented Programming (OOP), the attempts are made to break a problem into some component, called Objects. They are the basic elements of Object Oriented system and are also known as the entities of the OOP. The set of related objects may exchange data and information. They may interact with each other. Thus,

Object is a unique entity, which contains data and functions (characteristics and behavior) together in an Object Oriented Programming (OOP) Language.

CLASS

Class is a group of similar types of objects, which possesses same attributes and behavior. It also defines the abstract characteristics of an object, including characteristics (its attributes, fields or properties) and the behavior (the things it can do, or methods, operations or features). One can say that a class is a blueprint that describes the nature of an object.

For example, let us consider ‘Rainbow’ as a class, where its different colors like violet, indigo, green etc. can be referred as its objects. Each object has some characteristics and behaviors, which are defined for the class ‘Rainbow’.

Before talking about Interface, we must understand concepts of abstraction and inheritance in Java.

ABSTRACTION

In real life situation, you might have noticed that we need not require to know the details of technologies to operate any device. For example, you have a car. In order to drive it, you are only aware of some essential external features of the car, such as Steering, Accelerator, Clutch, Brake and Gears.

Have you ever thought what change would have taken place in the engine of a car? How does the gear box manage to change the speed of a car just by changing the gear? There are innumerable questions that come to our mind. But, the answer is simply No.

It simply indicates while driving a car, you only need to know the essential features without knowing the details of internal mechanism of the system. This act of driving a car is termed as Abstraction.

Thus, abstraction refers to an act of representing essential features without including background details.

INHERITANCE

This term is derived from biology. The literal meaning of inheritance is acquired characteristics. You would have studied in biology that the characteristics are transferred to the children from their parents. Similarly an object of a class acquired some properties from the objects of another class.

Hence, Inheritance is the process by which objects of one class can link and share some common properties of objects from another class. Here, each derived class shares common characteristics with the class from where it is derived.

INTERFACE

Similar to an object, an Interface is a blueprint of a class. It consists of static constants and abstract methods. It is a mechanism to achieve abstraction and multiple inheritance in Java. It is declared using the interface keyword. It provides total abstraction, meaning all methods in an interface must be declared with empty body, and all fields must be public, static and final by default.

Syntax

interface <interface_name> {
   // constant fields declaration
   // abstract methods declaration
   // by default
}

RELATIONSHIP BETWEEN CLASS AND INTERFACE

A class can extend another class, an interface can extend another interface, but a class implements an interface.

Example 1

Inheritance using Class

class Student { // parent class declaration float fees = 300000; // parent class variable } public class ClassInheritance extends Student { // child class inheriting Student class declaration public static void main(String[] args) { // main function declaration ClassInheritance ci = new ClassInheritance(); // child class object creation System.out.println("Student Fees = " + ci.fees); // calling parent class variable using child class's object } }

Output

Student Fees = 300000.0

Example 2

Inheritance using Interface

interface printable { // interface declaration void print(); // initializing function declared in class } public class InterfaceInheritance implements printable { // child class implementing interface 'printable' declared public void print(){ // print function declared to print output System.out.println("Hello Readers"); // printing output } public static void main(String[] args) { // main function declaration InterfaceInheritance obj = new InterfaceInheritance(); // creating object of class obj.print(); // function calling using class object } }

Output

Hello Readers

The difference between Class and Interface are listed below

CLASS INTERFACE
The ‘class’ keyword is used to create a class. The ‘interface’ keyword is used to create an interface.
An object of a class can be created. An object of an interface cannot be created.
Class doesn’t support multiple inheritance. Interface supports multiple inheritance.
A class can inherit another class. An Interface cannot inherit a class.
A class can be inherited by another class using the keyword ‘extends’. An Interface can be inherited by a class using the keyword ‘implements’ and it can be inherited by another interface using the keyword ‘extends’.
A class can contain constructors. An Interface cannot contain constructors.
It cannot contain abstract methods. It consists of abstract methods only.
Variables and methods can be declared using any specifiers like public, protected, default, private. Variables and methods are declared as public only.
raja
Updated on 05-Sep-2022 10:36:13

Advertisements