Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Sunday, 17 September 2017

Difference Between class and interface in java


Class vs Interface in Java

class vs interface in java

In the last post we have already discussed what is the difference between abstract class and interface in java but here we will learn what is the difference between class and interface in java in detail.

Let's start what is the differences between class and interface in java with examples.


Java Class

  • In java 'class' keyword is used to create a class.
  • We can create object or instance of class easily in java.
  • We cannot achieve multiple inheritance through class.
  • The data members of a class are not public, static, and final by default.
  • The methods of class are not public and abstract by default.
  • We can declare default and parameterized constructor in a class.
  • A class can implement interface.
  • In a class method can be final and static.
  • Here 'extends' keyword is used for inheritance in class.
  • In a class, we can use public, private and protected access specifiers with method.
  • We can declare main() method in class.
  • There only concrete methods are allowed i.e with method{} body.
  • A class can extend only one class but implement any number of interfaces.

Simple Java Class Example

class Students
{
String name;
int rollno;
Students(String name, int rollno)
{
this.name = name;
this.rollno = rollno;
System.out.println(name+" "+rollno);
}
static void run()
{
System.out.println("Students participates in race program");
}
public static void main(String... s)
{
Students ss = new Students("sachin", 12);
Students.run();
}
}

Output: sachin 12
             Students participates in race program


Java Interface

  • In java 'interface' keyword is used to create interface in java.
  • We can't create object of an interface.
  • We can achieve multiple inheritance through interface in java.
  • The data members of an interface are public, static, and final by default.
  • The methods of an interface are public and abstract by default.
  • We cannot declare any kind of java constructor in an interface.
  • A class can implement an interface but an interface cannot extend a class or implement a class.
  • In an interface, methods cannot be static or final.
  • Here 'implements' keyword is used for inheritance in an interface.
  • In an interface, there only public specifier is allowed.
  • We can't declare main() method in an interface.
  • There only abstract method(without body) is allowed in an interface but now in java 8 version we can keep non-abstract method(with body).
  • An interface can extend multiple interfaces but cannot implement any interface.



Simple Example of Java Interface

This is the simple interface program in java with output.

interface My
{
void show();
}
interface My2
{
void display();
}
class Test implements My,My2
{
public void show()
{
System.out.println("Interface My");

}
public void display()
{
System.out.println("Interface My2");
}
public static void main(String args[])
{
Test t =
new Test();

t.show();
t.display();
}
}

Output: Interface My
             Interface My2


Use of Interface in Java

  • So that we can achieve fully abstraction in java.
  • So that we can achieve multiple inheritance in java.

In this article, you have learned what is the difference between class and interface in java.

I hope, this interface vs class article will be useful to you.



Share:

2 comments:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email

Link

Translate