Table of contents
1.
Introduction
2.
Classes in Java
2.1.
Properties of a Class
2.2.
Java
3.
Interface in Java
3.1.
Properties of an Interface
3.2.
Java
4.
Differences Between a Class and an Interface
5.
Frequently Asked Questions
5.1.
What is the difference between type, interface, and class?
5.2.
What is an interface in Java?
5.3.
Can an interface have a class in Java?
6.
Conclusion
Last Updated: Nov 16, 2024
Easy

Difference between Class and Interface

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Object-Oriented Programming (OOP), two fundamental building blocks shape the architecture of our code: Classes and Interfaces. While both serve as blueprints for creating organized and maintainable code, they each bring distinct capabilities and serve different purposes in software design. For developers navigating through Java, C#, or any other OOP language, understanding the key differences between classes and interfaces is crucial for making informed design decisions. In this blog, we will explore these essential OOP concepts, helping you grasp when and why to use each one, ultimately enabling you to write more elegant and flexible code.

difference-between-class-and-interface

Check out this article - Duck Number in Java

Classes in Java

A class is a user-defined data type. It is a blueprint or template which is used for creating objects. Each object has some properties and methods. A class encapsulates the data and behavior of an object into a single entity. This property makes a class easy to use and maintain.

Properties of a Class

  • Encapsulation: It is a mechanism that encapsulates related data and behavior into a single unit. It helps in maintaining the security and integrity of code
     
  • Inheritance: A class can inherit properties and methods from another class
     
  • Polymorphism: It can have multiple methods with the same name but different implementations and parameters. Polymorphism allows more versatility in the code
     
  • Constructors: They are used to initialize objects of the class. A class can have one or more than one constructor
     
  • Access modifiers: The access modifier determines the class's or its members' visibility. It can be public, protected, or private
     

Syntax:

<access_modifier> class <class_name>{
 	//fields
 	<access_modifer> <data_type> <field_name>;

 	//constructors
 	<access_modifer>  <class_name> ([parameters]){
 	//constructor body
}

 //methods
 <access_modifer> <return_type> <method name> ([parameters]){
  	//method body
 }
}

 

  • The access modifier determines the visibility of the class or its members. It can be public, protected, or private
     
  • Here <class_name> is the name of the class
     
  • The fields hold the data of the class
     
  • The constructor method initializes the object of the class when it is created
     

Example: 

  • Java

Java

public class Main {
   static class NinjaStudent {
       private String name;
       private int age;

       public NinjaStudent(String name, int age) {
           this.name = name;
           this.age = age;
       }

       public void Ninja() {
           System.out.println("This is a Ninja function");
       }

       public void NinjaAction() {
           System.out.println("This is NinjaAction function");
       }
   }

   public static void main(String[] args) {
       NinjaStudent myNinjaStudent = new NinjaStudent("Ninja1", 17);
       myNinjaStudent.Ninja();
       myNinjaStudent.NinjaAction();
   }
}
You can also try this code with Online Java Compiler
Run Code


Output

output

Explanation

In this example, we have a NinjaStudent class with two properties, name, age, and two methods, Ninja() and NinjaAction(). We then created a new NinjaStudent object called Ninja() and NinjaAction() methods.

Also see,  Eclipse ide for Java Developers

Interface in Java

An interface as a collection of abstract methods that can be implemented by a class. It only specifies the method signature(name, parameters, and return type) but not its implementation. The specified methods are then implemented in a class. Therefore an interface can have methods and variables. However, by default, the methods declared in an interface are abstract.

Properties of an Interface

  • Abstract method: An interface helps in declaring one or more than one abstract methods. These methods have no implementation. The class that implements an interface must have implementations of all the declared methods in an interface
     
  • Multiple Inheritance: An interface can extend multiple interfaces. This property allows it to inherit properties and methods from multiple sources
     
  • Method Signatures: An interface contains method signatures. These are just declarations of methods without implementation
     
  • Access Modifiers: The access modifier determines the visibility of the class or its members. It can be public, protected, or private
     

Syntax:

<access_modifer> interface <interface_name> {
        //method signatures

        <access_modifier> <return_type> <method_name> ([parameters]);
}

 

  • The access modifier determines the visibility of the class or its members. It can be public, protected, or private
     
  • The <interface_name> is the name of the interface
     
  • Method signatures are the declarations of methods that must be implemented by any class that uses the interface
     
  • The method signatures do not have a method body. They are only declarations of methods that must be implemented by a class that uses the interface
     

Example: 

  • Java

Java

public class Main {
   public static void main(String[] args) {
       NinjaStudent myNinja1 = new Ninja1("Ninja1", 15);
       myNinja1.NinjaGoals();
       myNinja1.NinjaSkills();
   }

   public interface NinjaStudent {
       public void NinjaGoals();
       public void NinjaSkills();
   }

   public static class Ninja1 implements NinjaStudent {
       private String name;
       private int age;

       public Ninja1(String name, int age) {
           this.name = name;
           this.age = age;
       }

       @Override
       public void NinjaGoals() {
           System.out.println("These are ninja's goals");
       }

       @Override
       public void NinjaSkills() {
           System.out.println("These are ninja's skills");
       }
   }
}
You can also try this code with Online Java Compiler
Run Code


Output

output

Explanation

We have created a NinjaStudent interface that defines two methods NinjaSkills() and NinjaGoals(). We then create a Ninja1 class that implements the NinjaStudent interface and provides its implementation for the two methods. We then created a new Ninja1 object using the NinjaStudent interface and called its NinjaSkills() and NinjaGoals() methods.


Also see, Java Ioexception

Differences Between a Class and an Interface

This comparison table shows the difference between class and interface.

Basis

Class

Interface

ImplementationClass can provide both variables and methods.An interface can only define method signatures.
Object CreationFor object creation, a class can be instantiated directly using ‘new’ keyword.For object creation, an interface cannot be instantiated directly.
Access modifiersClass has access modifiers for variables/ methods.An interface can have public access modifiers for methods.
InheritanceWe can extend a class. It can inherit other classes.We can extend multiple interfaces, i.e., Interface supports multiple inheritances.
ConstructorsA class can contain constructors.An interface does not have a constructor.
VariablesIn a class variables can be static, final, or neither.All variables are static and final.
Abstract methodsA class cannot contain abstract methods.An interface contains abstract methods.

Also see, Difference Between Rank and Dense Rank

Read more, how to run java program

Frequently Asked Questions

What is the difference between type, interface, and class?

A type defines a variable's nature, an interface specifies method contracts without implementation, and a class implements methods and provides object structure.

What is an interface in Java?

An interface in Java is a blueprint defining abstract methods and constants, allowing classes to implement and ensure specific behaviors without inheritance.

Can an interface have a class in Java?

No, an interface cannot have a class, but it can have nested static classes for utility purposes without affecting the interface's abstract nature.

Conclusion

This article discussed some of the differences between class and interface by briefly discussing their concept, syntax, and properties. We also looked into some examples to understand the same. Understanding the distinctions between classes and interfaces is crucial for effective object-oriented programming. While classes provide a blueprint for objects with shared attributes and behavior, interfaces enforce a specific set of methods that classes must implement. You can read more such articles on our platform, Code360.

You will find straightforward explanations of almost every topic on this platform. So take your coding journey to the next level using Code360.

Live masterclass