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



Home Java Master-java Interface in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Interface in Java

Advertisement
In this section we will learn about Interface and Marker Interfaces in Java.

Interface in Java

     

In this section we will learn about Interface and Marker Interfaces in Java. This tutorial will clarify your questions "What is marker Interface?" and "Why to use Marker Interface?" and "difference between abstract class and the interface".

Interface

In general, interface is the way just to say something to a media by using another media. Let's take the general life example. TV Remote is the interface because it is the medium to give the command to a TV in order to change the channels or to ON/OFF the TV. Electric switch is also the interface's example.

But in java programming language interface is nothing but the collection of methods with empty implementations and constants variables ( variables with static and final declarations ). All the methods in an interface are "public and abstract" by default. Since interfaces are abstract in nature so they can not be directly instantiated. To define the methods of an interface the keyword "implements" is used. Interfaces are similar to abstract classes but the major difference between these two is that interface have all the methods abstract while in case of abstract classes must have at least one abstract method. Interface combines the two functionality (template and multiple inheritance) of C++ language into one (in itself). 

Interface Definition

visibility mode interface interfaceName{
  constant variable declarations
  abstract method declarations
}
e.g.
public interface RacingCar{
  public void startcar (int Obj)
  public void changegear (int Obj)
  public void incrrace (int Obj);
  public void stopcar (int Obj);
}

Marker Interface

In java language programming, interfaces with no methods are known as marker interfaces. Marker interfaces are Serializable, Clonable, SingleThreadModel, Event listener. Marker Interfaces are implemented by the classes or their super classes in order to add some functionality.

e.g.  Suppose you want to persist (save) the state of an object then you have to implement the Serializable interface otherwise the compiler will throw an error. To make more clearly understand the concept of marker interface you should go through one more example.  

Suppose the interface Clonable is neither implemented by a class named Myclass nor it's any super class, then a call to the method clone() on Myclass's object will give an error. This means, to add this functionality one should implement the Clonable interface. While the Clonable is an empty interface but it provides an important functionality.

Difference between Interfaces and abstract classes

Some important difference between Interface and abstract classes are given here

  Features    Interface    Abstract Class
  Methods An interface contains all the methods with empty implementation. An abstract class must have at least one method with empty implementation.    
   Variables The variables in interfaces are final and static. Abstract classes may contain both instance as well as static variables.
  Multiple   Inheritance  In java multiple inheritance is achieved by using the interface (by implementing more than one interface at a time) Abstract classes does not provide this functionality.
 Additional Functions   If we add a method to an interface then we will have to implement this interface by any class.. In Abstract classes we can add a method with default implementation and then we can use it by extending the abstract class. 
Advertisements

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: June 11, 2007

Related Tutorials

Discuss: Interface in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:12
merlin
October 5, 2011
interface

what is use of marker interface eventhoough it is not having any methods inside it?
toybabu
December 9, 2011
java

need more and more examples
Vilas Dandge
January 17, 2012
To give the all type of java program to us

This web is very help full to me.
suhl@dipta
May 29, 2012
marker interface

but some marker interface has methods,like Runnable interface. So my question is why marker interface provided by java?
Venkat Pagadala
June 10, 2012
Abstract Class

Abstract Class need not have any abstract methods. Please correct the first row third column saying "An abstract class must have at least one method with empty implementation".
praga
June 19, 2012
Use of interface and abstract

Y we need to declare abstract and interface before class? instead we can directly declare methods,arguments,etc as public elsewhere in program and inherit by extend...!!!! Also what is the utmost considerable advantage of interface over abstract or viceversa..? pls make me to understand
sanjay
August 22, 2012
Difference between run and start method in threads

Please let me know Difference between run and start method in threads
harish
October 17, 2012
learning

Thank you for providing this concepts
unis ahmad
November 24, 2012
abstract method

it is not mandatory for abstract class to have atleast one abstract method.please correct the mistake.
DMCA.com