0

Suppose I created marker interface kiran and How can I use this Interface. I know it is used by implements kiran but what is use of declaring marker interface.

public interface kiran { }
user207421
  • 305,947
  • 44
  • 307
  • 483
Ganesh Jadhao
  • 240
  • 1
  • 3
  • 11
  • What is the use of a non-user defined marker interface? – Boris the Spider Apr 22 '14 at 07:26
  • My question is If I create marker interface with name MyInterface then what is use of that interface or how can i use functionality of that MyInterface interface how jvm know what functionality add with this MyInterface interface – Ganesh Jadhao Apr 22 '14 at 07:30
  • The JVM doesn't add any functionality. You do, when you encounter an instance. – user207421 Apr 22 '14 at 07:31
  • Non user defined interface is used to inform something to jvm such as serializable tell jvm to serialize this class or member whatever – Ganesh Jadhao Apr 22 '14 at 07:34
  • I don't get answer which i want from What is the purpose of a marker interface? this Question – Ganesh Jadhao Apr 22 '14 at 07:37
  • @user3559324 That's not correct. The JVM doesn't care. There is code in `ObjectOutputStream` that cares. Just Java code, same as you can write yourself. If you're not getting the answer you want you need to tell us what kind of answer you want. You're probably wrong in wanting it. There is at least one correct answer here, and several more at the questions of which this is just yet another duplicate. – user207421 Apr 22 '14 at 07:37
  • I can create my own marker Interface but I don't Know how it will worked. I want to know Why we write our own Marker Interface in all answer they provide example of annotation or framework. I want to create my own interface and use in my program then how can i use that marker interface in my program – Ganesh Jadhao Apr 22 '14 at 07:45
  • What part of `instanceof kiran` don't you understand? It appears in [@PeterMmm's answer below](http://stackoverflow.com/a/23213118/207421), and the basic idea appears [here](http://stackoverflow.com/a/4540929/207421) too. – user207421 Apr 22 '14 at 07:54
  • ya thank you I got it – Ganesh Jadhao Apr 22 '14 at 08:35

1 Answers1

4

A common use case is to test if an object is marked with this interface:

if (obj instanceof kiran) {
    // do something
}
PeterMmm
  • 24,152
  • 13
  • 73
  • 111