Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Interface in Java 
 

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. 

                         

» View all related tutorials
Related Tags: c class io nested definition source while ini member e il it not init art ce in no part as

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

11 comments so far (
post your own) View All Comments Latest 10 Comments:

Hi,
Can i get a more detailed information for difference between Interface and abstract classes . Specially for point 4 with some coding example.
Thanks,
Jayant Singh

Posted by Jayant Singh on Monday, 12.15.08 @ 06:02am | #82779

Explain how run-time polymorphism can be achieved using interfaces with sample program?

Posted by kinjal on Tuesday, 09.23.08 @ 17:00pm | #80634

In package with out inheritance(extends) how we use seperate class and how we access in main class.

Posted by vinothini.E on Friday, 09.5.08 @ 18:39pm | #77263

JAVA CONCEPTS NOT CLEAR

Posted by AMIT on Tuesday, 08.26.08 @ 17:09pm | #75264

what is the use of inheritance AND WHEN TO USE THIS?
what i understood is while using inheritance we can reuse the code. please provide me more informaton regarding this

Posted by ramaraju on Thursday, 08.14.08 @ 08:22am | #72761

i am having doubt regarding util packages. and i want to know what is the difference between arraylist, vector and hash map , hashtable and iterator, list iterator, enumerators. where we will use and when to use this

Posted by ramaraju on Thursday, 08.14.08 @ 08:18am | #72760

i want to known the use of interface with example

Posted by rama raju on Tuesday, 08.12.08 @ 01:15am | #72351

what java is pure oop.

Posted by mohd daoud on Friday, 08.8.08 @ 12:46pm | #71764

i want to different between method overloading and method overriding? 3 to 4 differences giv me plzz send me my mail naveenk_raju@yahoo.co.in

Posted by naveen on Friday, 04.25.08 @ 17:47pm | #57852

i want to know, like if a subclass of a class implementing Serilizable interface, don't want to be serialize, so how can we prevent this Serialization....

Posted by Vishal on Wednesday, 02.20.08 @ 01:18am | #49130

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.