+ 1

What is markup interface in java?

Explain with sample code the real purpose of markup interface.

31st Mar 2017, 8:04 PM
Sbk0103
Sbk0103 - avatar
5 Respuestas
+ 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); } } }
31st Mar 2017, 10:39 PM
Eligijus Silkartas
Eligijus Silkartas - avatar
+ 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
1st Apr 2017, 9:43 AM
Eligijus Silkartas
Eligijus Silkartas - avatar
+ 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"); } } }
1st Apr 2017, 3:42 PM
Eligijus Silkartas
Eligijus Silkartas - avatar
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?
1st Apr 2017, 9:22 AM
Sbk0103
Sbk0103 - avatar
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?
1st Apr 2017, 11:19 AM
Sbk0103
Sbk0103 - avatar