Differences between Class and Interface in Java that will help you to understand that, how both contained variables and methods are different from each other into class and interface section in Java. They seem similar syntactically. Even though both containing variables and methods, but they are completely different from plenty of aspects.

Differences between Class and Interface in Java                                                   

Class:

It is a user-defined prototype or blueprint through which objects are made. A class represents the method or set of properties that are similar to all the objects of the same type. In other words, class declaration can consider the below components:

  1. Modifiers:– A class has default access or it can be public.
  2. Class Name:- Name must start with an initial letter.
  3. Superclass (if any):- In case if there is any class’s parent (superclass), preceded through keyword extends. Only a class can extend (subclass) one parent. 
  4. Interface (If any):- If there is any comma (,) separated list of interfaces apply by the class that should be preceded through keyword implements. 
  5. Body :- Class body surrounded through { } braces.

Initializing new objects are the main uses of constructors. Fields are variables which give its object and the state of the class, methods are mainly used to implement its objects and the behavior of the class.

Example

// Java program to demonstrate Class
 public class Dog {
     // Instance Variables
     String prog;
     String dogbreed;
     String color;
int age;
// Constructor Declaration of Class

public Dog(String prog, String dogbreed, int age, String color)
{
   this.prog = prog;
   this.dogbreed = dogbreed;
   this.age = age;
   this.color = color;
}

// method 1
public String getName()
{
   return prog;
}

// method 2
public String getBreed()
{
   return dogbreed;
}

// method 3
public int getAge()
{
   return age;
}

// method 4
public String getColor()
{
   return color;
}

//Override
public String toString()
{
   return (“Hi my name is “
           + this.getName()
           + “.\nMy breed, age and color are “
           + this.getBreed() + “, “
           + this.getAge() + “, “
           + this.getColor());
}

public static void main(String[] args)
{
   Dog tuffy = new Dog(“tuffy”, “papillon”, 5, “white”);
   System.out.println(tuffy.toString());
}
}

Result:

Hi, my name is Tuffy.

My dog breed, age, and color are papillon, 5, white

Interface:- 

Just like the class, an interface can have variables and methods. However, methods declared in the interface are by default abstract {only method signature, there would be no body}

  • The interface shows that what are the things must do by class, not how. This is a blueprint for the class.
  • The interface is about the abilities just like a player could be an interface and anyone class implementing that player should have a caliber to {or must implement} move(). So in this way, interface specifies the set of methods that class has to implement. 
  • In case, if a class implements the interface and doesn’t give method bodies for all the functions which are specified in interface, In that case, a class should be declared abstract.

Differences between Class and Interface in Java

Syntax :-

interface {

    // declare constants section

    // also shows methods that abstract 

    //default.

}

Example:

 // Java program to demonstrate

// working of interface.

import java.io.*;

// A simple interface

interface in1 {

    // public, static and final

    final int abc = 10;

    // public and abstract

    void display();

}

// A class that implements the interface.

class testClass implements in1 {

    // Implementing the capabilities of

    // interface.

    public void display()

    {

       System.out.println(“Geek”);

    }

    // Driver Code

    public static void main(String[] args)

    {

       testClass tt = new testClass();

       tt.display();

       System.out.println(abc);

    }

}

Result:

Geek

10

Differences between Class and Interface in Java

Differences between Interface and Class:

Class Interface
Keyword is mainly used to generate a class is ‘class’ Keyword is mainly used to generate an interface is ‘interface’
Classes doesn’t support multiple inheritance. it support’s multiple inheritance.
Class could be inherit another class Interface can’t  inherit any class.
Class can be inherited through another class by using the ‘extends‘ keyword. Interface can be inherited through a class by using ‘implements’ keyword and this can be inherited through an interface by using the ‘extends’ keyword.
Class can keep the constructors. Interface can’t contain the constructors.
Class can’t contain abstract methods. Interface can only contain abstract methods.
Methods and variables in  class can be declared by using any access specifier (default, public, private, protected) Methods and all the variables in the interface are declared as public.
In the class variable can be final, static or neither. All the variables are final and static.

Differences between Class and Interface in Java

To know about the latest updates of PHP you can also visit here :

  • DROPDOWN MENU NOT BEING DISPLAYED ON THE FIRST CLICK AFTER BEING CREATED DYNAMICALLY
  • HOW TO COPY-PASTE HTML TO SUMMER NOTE
  • WHY DOES NOT THE ANIMATION SPEED PARAMETER TAKE MY VARIABLE JQUERY
  • CANNOT ABLE TO FETCH DATA FROM AJAX TO THE PHP PAGE
  • SET DATE AND TIME IN JQUERY DATE AND TIME PICKER WITH SELENIUM
0
0
0
Share 0
Tweet 0
Pin it 0
0 Shares:
Share 0
Share 0
Tweet 0
Share 0
Share 0
Share 0
Share 0
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Dix Breves Rencontres Ebook Gratuit

Pour votre bien-être et pour arriver à vos fins, rencontres gratuites – https://www.paparencontres.fr/rencontres-gratuites/ – il est indispensable de…
Read More

How to spam protection in WordPress

How to spam projection in WordPress? It is most used feature of Preventing WordPress comment spam which is an ongoing regular process that can be done in 2 ways