Results 1 to 7 of 7
  1. #1
    jperson is offline Member
    Join Date
    Jan 2010
    Posts
    61
    Rep Power
    0

    Default marker interface

    What is a marker interface?
    please give me an small example of marker class and it's benfits ?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,534
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jperson View Post
    What is a marker interface?
    please give me an small example of marker class and it's benfits ?
    A marker interface is an interface that doesn't declare any methods; the almost canonical example is the Serializable interface, i.e. if a class implements that marker interface it is considered to be Serializable.

    kind regards,

    Jos

  3. #3
    jperson is offline Member
    Join Date
    Jan 2010
    Posts
    61
    Rep Power
    0

    Default

    do you mean that marker class ia a class with no body like this

    class A{

    }

    so how it work or what's it's benfits (please show me a small example , i'm new to java)?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,534
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jperson View Post
    do you mean that marker class ia a class with no body like this

    class A{

    }

    so how it work or what's it's benfits (please show me a small example , i'm new to java)?
    No. it's like an interface:

    Java Code:
    public interface Marker { 
    }
    Every class that implements it is a Marker:

    Java Code:
    class Foo implements Marker {
       ...
    }
    ...and if I only want Markers somewhere I can do:

    Java Code:
    List<Marker> list= new ArrayList<Marker>();
    list.add(new Foo()); // succeeds
    list.add(new Object()); // fails
    ...
    kind regards,

    Jos

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,454
    Rep Power
    25

    Default

    Its used to give a type to a class without any other baggage.
    Other programs can then test if a class is of that type. JosAH's example of Serializable.

  6. #6
    Hema26 is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    Marker Interface is interface without any method. we tell JVM to treat the class extending this interface differently.
    Hema26

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default

    Quote Originally Posted by Hema26 View Post
    Marker Interface is interface without any method.
    As already said.

    we tell JVM to treat the class extending this interface differently.
    Hema26
    No we don't.

    db

Similar Threads

  1. Use of marker interfaces...
    By skarthikeyan in forum Introductions
    Replies: 0
    Last Post: 03-26-2009, 06:36 AM
  2. TODO marker implementation sample
    By SSG_JJV in forum Eclipse
    Replies: 1
    Last Post: 02-20-2009, 04:50 AM
  3. use of marker interface
    By venkatallu in forum New To Java
    Replies: 1
    Last Post: 09-10-2008, 03:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •