Java Interface-10 Programs
All details Java through Program
JAVA INTERFACE:
To declare an interface, use the interface keyword. It is used to provide total abstraction. That means all the methods in an interface are declared with an empty body and are public and all fields are public, static, and final by default. A class that implements an interface must implement all the methods declared in the interface. To implement interface use implements keyword.
Syntax:
interface {// declare constant fields// declare methods that abstract// by default.}
Why do we use an Interface?
It is used to achieve total abstraction.
Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances.
It is also used to achieve loose coupling.
Interfaces are used to implement abstraction. So the question arises why use interfaces when we have abstract classes?
PROGRAM 1:Interface in java example.
CODE:
// Java program to demonstrate working of
// interface
import java.io.*;
// A simple interface
interface In1 {
// public, static and final
final int a = 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(“Somnath”);
}
// Driver Code
public static void main(String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(a);
}
}
OUTPUT:
Somnath10
PROGRAM 2:Create printable interface and implement in A6 class and then print text.
CODE:
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println(“Hello”);}
public static void main(String args[]){
A6 obj = new A6();
obj.print();
}
}
OUTPUT:
Hello
PROGRAM 3:Create one Drawable class and use then reactangle and Circle class.
CODE:
//Interface declaration: by first user
interface Drawable{
void draw();
}
//Implementation: by second user
class Rectangle implements Drawable{
public void draw(){System.out.println(“drawing rectangle”);}
}
class Circle implements Drawable{
public void draw(){System.out.println(“drawing circle”);}
}
//Using interface: by third user
class TestInterface1{
public static void main(String args[]){
Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable()
d.draw();
}}
OUTPUT:
drawing circle
PROGRAM 4:Different bank rate of interest example access through interface bank.
CODE:
interface Bank{
float rateOfInterest();
}
class SBI implements Bank{
public float rateOfInterest(){return 9.15f;}
}
class PNB implements Bank{
public float rateOfInterest(){return 9.7f;}
}
class TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println(“ROI: “+b.rateOfInterest());
}}
OUTPUT:
ROI: 9.15
Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance.
PROGRAM 5: Multiple inheritance in Java by interface example.
Here I have created two interface and on class .so nnow I can use both interface property.
CODE:
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println(“Hello”);}
public void show(){System.out.println(“Welcome”);}
public static void main(String args[]){
A7 obj = new A7();
obj.print();
obj.show();
}
}
OUTPUT:
Output:HelloWelcome
PROGRAM 6: Multiple inheritance in Java by interface example no two.
CODE:
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class TestInterface4 implements Showable{
public void print(){System.out.println(“Hello”);}
public void show(){System.out.println(“Welcome”);}
public static void main(String args[]){
TestInterface4 obj = new TestInterface4();
obj.print();
obj.show();
}
}
OUTPUT:
HelloWelcome
Java 8 Default Method in Interface
Since Java 8, we can have method body in interface. But we need to make it default method. Let’s see an example:
PROGRAM 7: Default Method in Interface acessign class interface method using class
File: TestInterfaceDefault.java
CODE:
interface Drawable{
void draw();
default void msg(){System.out.println(“default method”);}
}
class Rectangle implements Drawable{
public void draw(){System.out.println(“drawing rectangle”);}
}
class TestInterfaceDefault{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
d.msg();
}}
OUTPUT:
drawing rectangledefault method
Java 8 Static Method in Interface
Since Java 8, we can have static method in interface. Let’s see an example:
Program 8: Java 8 Static Method in Interface
example
File: TestInterfaceStatic.java
CODE:
interface Drawable{
void draw();
static int cube(int x){return x*x*x;}
}
class Rectangle implements Drawable{
public void draw(){System.out.println(“drawing rectangle”);}
}
class TestInterfaceStatic{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
System.out.println(Drawable.cube(3));
}}
OUTPUT:
drawing rectangle27
Java Nested Interface
An interface, i.e., declared within another interface or class, is known as a nested interface. The nested interfaces are used to group related interfaces so that they can be easy to maintain. The nested interface must be referred to by the outer interface or class. It can’t be accessed directly.
Syntax of nested interface which is declared within the interface
interface interface_name{
…
interface nested_interface_name{
…
}
}
Syntax of nested interface which is declared within the class
class class_name{
…
interface nested_interface_name{
…
}
}
Example of nested interface which is declared within the interface
In this example, we will learn how to declare the nested interface and how we can access it
PROGRAM 9: nested interface which is declared within the interface
Here I have used two inteface showable and inside its Message
CODE:
interface Showable{
void show();
interface Message{
void msg();
}
}
class TestNestedInterface1 implements Showable.Message{
public void msg(){System.out.println(“Hello nested interface”);}
public static void main(String args[]){
Showable.Message message=new TestNestedInterface1();//upcasting here
message.msg();
}
}
OUTPUT:
hello nested interface
Example of nested interface which is declared within the class
Let’s see how we can define an interface inside the class and how we can access it.
PROGRAM 10: Example of nested interface which is declared within the class
CODE:
class A{
interface Message{
void msg();
}
}
class TestNestedInterface2 implements A.Message{
public void msg(){System.out.println(“Hello nested interface”);}
public static void main(String args[]){
A.Message message=new TestNestedInterface2();//upcasting here
message.msg();
}
}OUTPUT:
Hello nested interface
1. Prior to JDK 8, the interface could not define the implementation. We can now add default implementation for interface methods. This default implementation has a special use and does not affect the intention behind interfaces.
PROGRAM 11:
CODE:
// Java program to show that interfaces can
// have methods from JDK 1.8 onwards
interface In1
{
final int a = 10;
default void display()
{
System.out.println(“hello”);
}
}
// A class that implements the interface.
class TestClass implements In1
{
// Driver Code
public static void main (String[] args)
{
TestClass t = new TestClass();
t.display();
}
}
OUTPUT:
hello
2. Another feature that was added in JDK 8 is that we can now define static methods in interfaces that can be called independently without an object. Note: these methods are not inherited.
PROGRAM 12:
CODE:
// Java Program to show that interfaces can
// have methods from JDK 1.8 onwards
interface In1
{
final int a = 10;
static void display()
{
System.out.println(“hello”);
}
}
// A class that implements the interface.
class TestClass implements In1
{
// Driver Code
public static void main (String[] args)
{
In1.display();
}
}
OUTPUT:
hello
PROGRAM 13: Java program to demonstrate the real-world example of Interfaces
CODE:
Filename:Main.java
// Java program to demonstrate the
// real-world example of Interfaces
import java.io.*;
interface Vehicle {
// all are the abstract methods.
void changeGear(int a);
void speedUp(int a);
void applyBrakes(int a);
}
class Bicycle implements Vehicle{
int speed;
int gear;
// to change gear
@Override
public void changeGear(int newGear){
gear = newGear;
}
// to increase speed
@Override
public void speedUp(int increment){
speed = speed + increment;
}
// to decrease speed
@Override
public void applyBrakes(int decrement){
speed = speed — decrement;
}
public void printStates() {
System.out.println(“speed: “ + speed
+ “ gear: “ + gear);
}
}
class Bike implements Vehicle {
int speed;
int gear;
// to change gear
@Override
public void changeGear(int newGear){
gear = newGear;
}
// to increase speed
@Override
public void speedUp(int increment){
speed = speed + increment;
}
// to decrease speed
@Override
public void applyBrakes(int decrement){
speed = speed — decrement;
}
public void printStates() {
System.out.println(“speed: “ + speed
+ “ gear: “ + gear);
}
}
class Main {
public static void main (String[] args) {
// creating an inatance of Bicycle
// doing some operations
Bicycle bicycle = new Bicycle();
bicycle.changeGear(2);
bicycle.speedUp(3);
bicycle.applyBrakes(1);
System.out.println(“Bicycle present state :”);
bicycle.printStates();
// creating instance of the bike.
Bike bike = new Bike();
bike.changeGear(1);
bike.speedUp(4);
bike.applyBrakes(3);
System.out.println(“Bike present state :”);
bike.printStates();
}
}
OUTPUT:
Bicycle present state :speed: 2 gear: 2Bike present state :speed: 1 gear: 1
Thank you.