+ 1
What is markup interface in java?
Explain with sample code the real purpose of markup interface.
5 Antworten
+ 5
It's an interface that you can use to mark a class to achieve special functionallity. For example, if you want to clone an object, you can use Cloneable interface. All it does is marks the class to support cloning.
You can copy the code below and see how it works
*****************
// without Cloneable interface, clone method would
// cause an exception
class MyClass implements Cloneable {
// override clone method from Object class
@Override
public Object clone() throws CloneNotSupportedException {
// return Object reference
return super.clone();
}
}
public class Main {
public static void main(String[] args) {
try {
MyClass o1 = new MyClass();
MyClass o2 = o1.clone();
} catch(CloneNotSupportedException ex) {
System.out.println(ex);
}
}
}
+ 2
Exactly, markup interface has no fields or methods. It is empty and only useful for marking classes, hence it's name - markup (or marker) interface
+ 2
Yes, you can create markup interface just like any other:
public interface Markup {
}
And now you can use this to mark any class you want. Cloneable, Serializable are defined in java.lang, so they are included in your programs.
The reason they work is because of instanceof operator, for example:
public class Test implements Markup {
public static void main(String[] args) {
Test ob = new Test();
if(ob instanceof Markup) {
System.out.println("ob is marked");
}
else {
System.out.println("ob is not marked");
}
}
}
0
Thanks for nyc explaination.. but whats is the real structure or mark up interface.. i mean usually interface has some methods and fields.. as such how does markup interface looks?
0
Can we create user defined markup interface ?.. Where does clonable interface resides.. As such i have learn other interfaces like.. serializable and remote.. so where does these all are defined and and how they works without any code inside them?