Core Java Tutorials


Introduction

Java Hello World

Java Compilation

JDK New Features

JVM Architecture

Java OOPs Concepts

Java Package

Array

Exception Handling

String

Immutable Class

File Handling

Serialization

MultiThreading

Volatile

Concurrent Package

Collection

Collection Internal

Generics

Cloning

Reflection In Java

Annotation

Class Loader

Java Inner classes

Garbage Collector

JDBC

References In Java

Heap Dump & Thread Dump

Interface In Java

Interfaces In Java are contract between two applications/modules which allow communication among them .
interface-in-java


How to declaring an Interface in Java

In Java you can declare an interface similar to class using interface keyword . Here is a Java interface example

public interface class Agile  {
	
	//methods declarations and variables  
}

interface-in-java How to use an interface in Java?

In below example we have designed an interface IFrogDao and a concrete class FrogDao by implementing the same interface IFrogDao as shown in below example.

public  class Frog  {
	private String name;
	private String color;
	private float price;

	public Frog()  {
		this("Moto","Green",400F);
	}
	
	public Frog(String name)  {
		this(name,"Green",400F);
	}

	public Frog(String name, String color, float price) {
		this.name = name;
		this.color = color;
		this.price = price;
	}

	@Override
	public String toString() {
		return "Frog [name=" + name + ", 
		color=" + color + ", price=" + price	+ "]";
	}
}
interface-in-java-example

Output :

Frog [name=Killy, color=Green, price=400.0] is persisted into the db
Frog [name=killy, color=Green, price=400.0]

Important points for interface in java

 interface keyword is used to create an interface in java.
 Interface in java can't be instantiated.
 reference of Interface can be created.
 Interface are implemeneted by the class.
 One interface extends other interface
 In Interface all the methods are public and abstract by default
 In Interface all the variables are public, static and final by default
 Interface does not have constructor