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

Monday, 31 July 2017

Interface Programs In Java

Java Interface

implements in java

Java interface is a blueprint of a class and all the data members of an interface are public, static, and final by default and all the methods are public and abstract by default.

It is used to achieve fully abstraction and multiple inheritance in java.

We cannot create an object of an interface just like an abstract class but there are many differences between abstract class and interface in java.

So let's start with simple interface program in java with the output.


Java Interface Example

interface Teacher
{
void show();//compiler will add public abstract internally
}
class Student implements Teacher
{
public void show()
{
System.out.println("Teacher teach all the students");
}
public static void main(String args[])
{
Student s = new Student();
s.show();
}
}

Output : Teacher teach all the students


Java Interface Example 2

By this example, we can achieve multiple inheritance through the interface. The specific implementation of run() and laugh() method is provided by it child class i.e ThirdPerson class. And work() is a personal method of child class i.e ThirdPerson class. 

interface FirstPerson
{
void run();
}
interface SecondPerson
{
void laugh();
}
class ThirdPerson implements FirstPerson,SecondPerson
{
public void run()
{
System.out.println("Person first is running");
}
public void laugh()
{
System.out.println("Person second is laughing");
}
void work()
{
System.out.println("Person third doing hard work");
}
public static void main(String args[])
{
ThirdPerson t = new ThirdPerson();
t.run();
t.laugh();
t.work();
}
}

Output : Person first is running
              Person second is laughing
              Person third doing hard work

So the main use of interface in java is to achieve fully abstraction and multiple inheritance easily.


Java Interface Example - With Default Method

The default method is the new feature of java 8 version. By the help of this default method, we can keep method with body i.e non-abstract method in an interface.

interface Employee
{
public default void work()
{
System.out.println("Employee work as software developer");
}
void manage();//abstract method
}
class Employee1 implements Employee
{
public void manage()
{
System.out.println("Employee manage other departments also");
}
public static void main(String args[])
{
Employee1 e = new Employee1();
e.work();
e.manage();
}
}

Output : Employee work as software developer
              Employee manage other departments also


Java Interface Example - One interface extends another Interface

interface First
{
void show();
}
interface Second extends First
{
void display();
}
class Test implements Second
{
public void show()
{
System.out.println("hello, how are you");
}
public void display()
{
System.out.println("hello, i am fine");
}
public static void main(String... s)
{
Test t = new Test();
t.show();
t.display();
}
}

Output : hello, how are you
              hello, i am fine

Java Interface Example - With Abstract Class

interface First
{
void show();
void display();
void get();
}
abstract class Simple implements First
{
abstract void run();
public void show()
{
System.out.println("hi show");
}
public void display()
{
System.out.println("hi display");
}
}
class Test extends Simple
{
public void get()
{
System.out.println("hi get");
}
void run()
{
System.out.println("hi run");
}
public static void main(String args[])
{
Test t = new Test();
t.show();
t.display();
t.get();
t.run();
}
}

Output : hi show
              hi display
              hi get
              hi run

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate