What is interface in JAVA ? | Basic IT Topic

What is interface in JAVA ?

interface in java

 

Interface in JAVA:-

An interface in Java is very similar to an abstract class. Both cannot be instantiated.

It is similar to a class. It is a collection of abstract methods.

The interface in java is a mechanism to achieve abstraction.

Java Interface represents the IS-A relationship.

The benefit of using interfaces is that they simulate multiple inheritances.

Ex
---
interface Demo
{
}
abstract class Test
{
}
class MyClass
{
public stactc void main(Sytring[] args)
{
Test t1=new Test(); //cannot be instantiated
Demo d1=new Demo();//cannot be instantiated
}
}

Following are the difference between abstract class and interface

Any type of variable can be declared within an abstract class whereas variable within interface by default public and static and final.

Any type of method can be designed within an abstract class, whereas the method within interface by default public and abstract.

A constructor can be designed within an abstract class, an object is not required for an interface. So constructor design is not allowed.

The abstract class supports 0 to 100% abstraction whereas from the object point of view interface support 100% abstraction.

Java support abstraction in 3 different levels:-
100% abstraction by an interface
0-100% abstraction in an abstract class
0% abstraction in an abstract class

Program:-
interface Test
{
public static final int x=10;
int a=20;
void show();
public abstract void fun();
}
abstract class Demo implements Test
{
int y;
abstract void display();
Demo()
{
System.out.println("Demo class ctor..............");
}
public void show()
{
System.out.println("show()...........");
}

}
class MyClass extends Demo
{
public void fun()
{
System.out.println("fun()......");
}
MyClass()
{
System.out.print("MyClass ctor.........");
}
public void display()
{
System.out.println("display().........");
}

public static void main(String[] args) 
{
System.out.println(Test.x);
MyClass mc=new MyClass();
mc.show();
mc.fun();
mc.display(); 
}
}

O/P
----
10
Demo class ctor..............
MyClass ctor.........show()...........
fun()......
display().........

Example of multiple inheritance by using interface:-

interface A
{
void show();
}
interface B
{
void show();
}
class Test implements A,B
{
public void show()
{
System.out.println("show().....");
}
public static void main(String[] args) 
{
Test t1=new Test(); 
t1.show();
}
}
O/P
----
show().....

 

We can design method within the interface (1.8 JDK Onwards):-

Program:-

interface Test
{
static void show()
{
System.out.println("i am in show() method");
}
public static void main(String[] args) 
{
System.out.println("main ()...");
}
}

As we already know subclass object is also the object of super class but in case of interface, we cannot call the static method of an interface by subclass object.

Program:-

interface Test
{
static void show()
{
System.out.println("static show in Test class...");
}
int x=10;
}
interface Demo
{
static void show()
{
System.out.println("static show in Demo class...");
}
int x=10;
}
class MyClass
{
public static void main(String[] args) 
{
MyClass mc=new MyClass();
Test.show();
Demo.show();
//System.out.println(mc.x); //reference to x is ambiguty
System.out.println(Test.x);
System.out.println(Demo.x); 
}
}
O/P
----
static show in Test class...
static show in Demo class...
10
10

interface in java
basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community