Markers interface in java

Marker Interfaces in Java?

Serializable as an Annotation is nonsense and @NonNull as an Interface is nonsense. I would say: Annotations are Markers + Metadata. BTW: Forefunner of Annotations was XDoclet, born in Javadoc, killed by Annotations.

@MichałLepczyński 2. Making Serializable as annotation instead of interface is nonsense because existing code rely on the interface, wont compile and will throw CNFE. Vice versa old runtimes would throw a NSE.

10 Answers 10

  1. Is the definition of a marker interface mentioned above in 1st point wrong? — It is correct in the parts that (1) a marker interface must be empty, and (2) implementing it is meant to imply some special treatment of the implementing class. The part that is incorrect is that it implies that JVM or the compiler would treat the objects of that class differently: you are correct in observing that it is the code of Java class library that treats these objects as cloneable, serializable, etc. It has nothing to do with the compiler or the JVM.
  2. instead of using the instanceOf operator why can’t the method be something like writeObject(Serializable) so that there is a compile-time type checking — This lets you avoid polluting your code with the name of the marker interface when a «plain Object » is needed. For example, if you make a class that needs to be serializable, and has object members, you would be forced to either do casting or make your objects Serializable at compile time. This is inconvenient, because the interface is devoid of any functionality.
  3. How Annotations are better than Marker Interfaces? — They let you achieve the same purpose of conveying metadata about the class to its consumers without creating a separate type for it. Annotations are more powerful, too, letting programmers pass more sophisticated information to classes that «consume» it.
Читайте также:  скрыть блок средствами HTML , CSS, JS и Bootstrap на BlogGood.ru

The way I always understood it is that Annotations are a kind of ‘Marker Interfaces 2.0’: Serializable exists since Java 1.1, Annotations were added in 1.5

and because writeObject is private, that means that for example a class doesn’t have to call the superclass implementation

One thing to keep in mind is that annotations were added to the language some years after the standard library was originally designed. If annotations had been in the language from the start, it is doubtful that Serializable would have been an interface, it probably would have been an annotation.

Well, in the case of Cloneable it is not clear whether it’s a library or JVM treatment of the instance that is altered.

«[. ]without creating a separate type for it.» — I’d say that this is precisely what separates them: A marker interface does introduce a type, while annotations (kind of) does’t.

It is not possible to enforce Serializable on writeObject because children of non-serializable class can be serializable, but their instances may be upcasted back to the parent class. As a result, holding a reference to something non-serializable (like Object ) does not mean that the referred instance cannot be really serialized. For instance in

 Object x = "abc"; if (x instanceof Serializable)

the parent class ( Object ) is not serializable and would be initialised using its parameter-less constructor. The value referenced by x , String , is serializable and the conditional statement would run.

I have made a simple demonstration to resolve doubt no 1 and 2 :

We will be having Movable interface which will be implemented by MobilePhone.java Class and one more class LandlinePhone.java which do NOT implement Movable interface

package com; public interface Movable

LandLinePhone.java and MobilePhone.java

 package com; class LandLinePhone < // more code here >class MobilePhone implements Movable < // more code here >

Our Custom Exception Class : package com;

public class NotMovableException extends Exception < private static final long serialVersionUID = 1L; @Override public String getMessage() < return "this object is not movable"; >// more code here > 

Our Test class : TestMArkerInterface.java

package com; public class TestMarkerInterface < public static void main(String[] args) throws NotMovableException < MobilePhone mobilePhone = new MobilePhone(); LandLinePhone landLinePhone = new LandLinePhone(); TestMarkerInterface.goTravel(mobilePhone); TestMarkerInterface.goTravel(landLinePhone); >public static void goTravel(Object o) throws NotMovableException < if (!(o instanceof Movable)) < System.out.println("you cannot use :" + o.getClass().getName() + " while travelling"); throw new NotMovableException(); >System.out.println("you can use :" + o.getClass().getName() + " while travelling"); >> 

Now when we execute main class :

you can use :com.MobilePhone while travelling you cannot use :com.LandLinePhone while travelling Exception in thread "main" com.NotMovableException: this object is not movable at com.TestMarkerInterface.goTravel(TestMarkerInterface.java:22) at com.TestMarkerInterface.main(TestMarkerInterface.java:14) 

So which ever class implements marker interface Movable will pass the test else error message will be displayed.

This is the way instanceOf operator check is done for Serializable, Cloneable etc

A marker interface in Java is an interface with no fields or methods. Put more simply, an empty interface in Java is called a marker interface. Examples of marker interfaces are the Serializable , Cloneable and Remote interfaces. These are used to indicate some information to compilers or JVMs. So if the JVM sees that a class is Serializable , it can do some special operation on it. Similarly, if the JVM sees some class is implementing Cloneable , it can perform some operations to support cloning. The same is true for RMI and the Remote interface. So in short, a marker interface indicates a signal or a command to the compiler or JVM.

The above started out as a copy of a blog post but has been lightly edited for grammar.

It’s alright to copy but please mention the source too : javarevisited.blogspot.com/2012/01/… . Also it will be nice if you don’t copy paste the spelling errors too. 🙂

a/ A marker interface as its name suggest exists only to notify anything that knows about it that a class declares something. The anything can be the JDK classes for the Serializable interface, or any class you write yoursel for a custom one.

b/ If it is a marker interface, it should not imply the existence of any method — it would be better to include the implied method in the interface. But you can decide to design it as you want if you know why you need it

c/ There is little difference between an empty interface and an annotation that uses no value or parameter. But the difference is there : an annotation can declare a list of keys/values that will be accessible at run time.

  1. It has nothing to do (necessarily) with the JVM and the compilers, it has something to do with any code which is interested in and is testing for a given marker interface.
  2. It’s a design decision and it’s done for a good reason. See the answer from Audrius Meškauskas.
  3. With respect to this particular topic, I don’t think it’s a matter of being better or worse. The marker interface is doing what it’s supposed to do just fine.

Can you add a link to «the answer from Audrius Meškauskas»? I don’t see anything on this page with that name.

a. I have always seen them as a design pattern and nothing JVM-Special I have used that pattern in several situations.

c. I beleive that using Annotations to mark something is a better solution then using marker interfaces. Simply because Interfaces are in the first place aimed to define common interfaces of Types / Classes. They are part of the class-hierachy.

Annotations are aimed to provide Meta-Informations to Code, and I think that marker are meta-informations. So they are exactly for that use-case.

The main purpose of marker interfaces is to create special types where types themselves have no behavior of their own.

public interface MarkerEntity < >public boolean save(Object object) throws InvalidEntityFoundException < if(!(object instanceof MarkerEntity)) < throw new InvalidEntityFoundException("Invalid Entity Found, can't be saved); >return db.save(object); > 

Here save method makes sure that only the objects of classes that implement the MarkerEntity interface are saved, for other types InvalidEntityFoundException is thrown. So here MarkerEntity marker interface is defining a type that adds special behavior to the classes implementing it.

Though annotations can also used now to mark classes for some special treatments but marker annotations are replacement for naming pattern not for Marker interfaces.

But marker annotations can’t fully replace the marker interfaces because; marker interfaces are used to define type (as already explained above) where as marker annotations do not.

+1 for showing that its actually used as a mere «safety» hook that a programmer has to explicitly say it can be saved to example a database.

I would argue first-off that Serializable and Cloneable are bad examples of marker interfaces. Sure, they’re interfaces with methods, but they imply methods, such as writeObject(ObjectOutputStream) . (The compiler will create a writeObject(ObjectOutputStream) method for you if you don’t override it, and all objects already have clone() , but the compiler will again create a real clone() method for you but with caveats. Both of these are weird edge cases that really aren’t good design examples.)

Marker interfaces are generally used for one of two purposes:

1) As a shortcut to avoid an excessively long type, which can happen with lots of generics. For instance, say you have this method signature:

public void doSomething(Foobar>>)

That’s messy and annoying to type, and more importantly, difficult to understand. Consider this instead:

public interface Widget extends Foobar>>

Then your method looks like this:

public void doSomething(Widget widget)

Not only is it clearer, but you can now Javadoc the Widget interface, and it’s also easier to search for all occurrences in your code of Widget.

2) Marker interfaces can also be used as a way around Java’s lack of intersection types. With a marker interface, you can require something to be of two different types, such as in a method signature. Say you have some interface Widget in your application, like we described above. If you have a method that requires a Widget that also happens to let you iterate over it (it’s contrived, but work with me here), your only good solution is to create a marker interface that extends both interfaces:

public interface IterableWidget extends Iterable, Widget
public void doSomething(IterableWidget widget) < for (String s : widget) < . >> 

Источник

Markers interface in java

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Оцените статью