Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Master-java Interface in java with example

Related Tutorials


 
 

Share on Google+Share on Google+

Interface in java with example

Advertisement
We are going to discuss about Interface in Java. Interface is blueprint of a class. Interface is a collection of abstract methods. Interface has only static constant and abstract method. We can only implement a class and extended it by other interface.

We are going to discuss about Interface in Java. Interface is blueprint of a class. Interface is a collection of abstract methods. Interface has only static constant and abstract method. We can only implement a class and extended it by other interface. We can extend one or more other interfaces but cannot implement them. Java interface is nothing but is a empty collection, implementation and constructs variables than static and final declaration. We can declare all method in interface abstract and public. Multiple inheritance allows extending interface.

Why use interface in java?

  • We use itto achieve complete abstraction.
  • The interface, we can support the functionality of the multiple inheritance.
  • we can use it to achieve loose coupling.
  • We can implement all method heading listed in interface.
  • We can take the interface check, that implements a empty method message().

How to create interface in java program: Following is the code of interface in java program. We have created interface method in java using "interface" keyword.

package Interface;

interface Demo {
     void call();
   
      }
interface Demo1{
	 void run();
 }
 
class CallTest implements Demo{
    public void call() {
		System.out.println("Hello java");
		
	}
}
 class RunTest implements Demo1{
	 public void run(){
		 System.out.println("Hello interface");
	 }
 }
 
 public class interfaceDemo{
	 public static void main(String[] args) {
		Demo demo = new CallTest();
		Demo1 demo1 = new RunTest();
	      demo.call();
	      demo1.run();
	}
 }

OutPut:-

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 17, 2013

Related Tutorials

Discuss: Interface in java with example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com