Java — Marker Interfaces in Java | Code Factory

Custom Marker Interface

public interface Deletable {

}
public class Entity implements Deletable {
// implementation details
}
public class ShapeDao {    // other dao methods

public boolean delete(Object object) {
if (!(object instanceof Deletable)) {
return false;
}

// delete implementation details

return true;
}
}

Marker Interfaces vs. Annotations

public interface Shape {
double getArea();
double getCircumference();
}
public interface DeletableShape extends Shape {

}
public class Rectangle implements DeletableShape {
// implementation details
}

Marker Interfaces vs. Typical Interfaces

public class ShapeDao {    // other dao methods

public boolean delete(Object object) {
if (!(object instanceof Shape)) {
return false;
}

// delete implementation details

return true;
}
}
public boolean delete(Object object) {
if (!(object instanceof Shape || object instanceof Person)) {
return false;
}

// delete implementation details

return true;
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store