Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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 a class?
5.2.
What is an interface?
5.3.
What is difference between class and interface?
5.4.
Which is better interface or class?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Difference between Class and Interface

Introduction

When we listen the name class and interface it looks like they are same. But there are lots of differences between these two. A class is a blueprint for objects, while an interface declares a set of methods that a class must implement.

difference-between-class-and-interface

In this article, we will discuss the difference between class and interface. We will also look into some examples to understand the same. In the end, we will also see a comparison table to understand the difference between class and interface more clearly.

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


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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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


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

Implementation Class can provide both variables and methods. An interface can only define method signatures.
Object Creation For object creation, a class can be instantiated directly using ‘new’ keyword. For object creation, an interface cannot be instantiated directly.
Access modifiers Class has access modifiers for variables/ methods. An interface can have public access modifiers for methods.
Inheritance We can extend a class. It can inherit other classes. We can extend multiple interfaces, i.e., Interface supports multiple inheritances.
Constructors A class can contain constructors. An interface does not have a constructor.
Variables In a class variables can be static, final, or neither. All variables are static and final.
Abstract methods A 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 a class?

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.

What is an interface?

We can refer to an interface as a collection of abstract methods that can be implemented by a class, which defines a set of methods implemented by any class that wants to use an interface.

What is difference between class and interface?

A class is a blueprint for objects, while an interface declares a set of methods that a class must implement.

Which is better interface or class?

It depends on the scenario. Use a class when you need a blueprint for objects with shared behavior, and an interface when you want to ensure a certain set of methods is implemented across different classes.

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, Coding Ninjas Studio.

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

Happy coding!

Previous article
ConcurrentModificationException
Next article
Extending Interfaces
Live masterclass