Difference Between Class And Interface In Java

Today here we will tell the Difference Between Class And Interface In Java. In objects, we can inherit the classes and can create an Object of Class but it is not possible in the case of the interface.

What Is a Class?

Class and Interface

The class can be defined as a user-defined prototype required for creating the object. It is a collection of properties or methods that are there in all objects of that particular type of object. The class can be declared using a few things let’s discuss the things required.

we can inherit the class by using the keyword extends and can be inherited by another class or interface. And it can contain abstract functions.

And we can declare the variables as public, Private, Protected, or default which we can not see in the case of the interface. so let’s learn more about the class.

 

Access Modifier

It is a keyword that helps us limit the access of that class and checks on the reach of the class, We can give a Public, Protected or Static, etc.

Keyword

It required the keyword that is “Class” always used before declaring any class and we can write the name of the class that we want to use.

For Example

public class HelloClass {
  int x = 5;

  public static void main(String[] args) {
    HelloClass newObj = new HelloClass();
    System.out.println(newObj.x);
  }
}

Here we declared a class HelloClass with Public access modifier and it has an object as newObj.

What Is Interface

The interface is a blueprint of class which have all the Methods and Variables are static which means it follows all the properties of static function by default.

Although we can declare the class also as static their functions and variables also need to be declared as static then only it will be static but in the case of interface we do not require to declare anything as static by default it is always static.

There are some advantages of the interface over class like we can have multiple inheritances using an interface which is not possible in the case of classes. And by default all the variables and functions in the interface are public.

which means we can not declare any member as private.

Let’s take an example of an interface

interface Animal {
  public void animalSound(); // interface method (does not have a body)
  public void sleep(); // interface method (does not have a body)
}

// Pig "implements" the Animal interface
class Pig implements Animal {
  public void animalSound() {
    // The body of animalSound() is provided here
    System.out.println("The pig says: wee wee");
  }
  public void sleep() {
    // The body of sleep() is provided here
    System.out.println("Zzz");
  }
}

class Main {
  public static void main(String[] args) {
    Pig myPig = new Pig();  // Create a Pig object
    myPig.animalSound();
    myPig.sleep();
  }
}

As code is self-explanatory

So learn more about Classes and interface at: Difference between class and interference

and also visit JAVA TUTORIAL to learn more about java and to solve the problems faced during learning java.

Leave a Comment

%d bloggers like this: