Showing posts with label Interface. Show all posts
Showing posts with label Interface. Show all posts

What is Marker Interface in Java?

Marker interfaces are special interfaces in Java. Marker interface will not have any methods or member variables. Just declared with interface name and also called as Tag interface. 

Then what is the use of Marker interface in Java?
Normally interface used to tell compiler about the behavior of the class. Used to tag the implementation of class based on their purpose and behavior. Just used to "mark" the java class which support a certain capability.

Alternate for Marker or Tag interfaces?
Java 1.5 came up with Annotations as an alternate solution for Marker interfaces. Advantage of using annotations are just it makes loose coupling instead of implementing marker interfaces. 
By using Annotations we can easily specify special behavior to the class but it can't act as the super type of the class what we can achieve by Marker interface. 

Is there any Marker interfaces provided by Java?
Below are the list of marker interfaces provided by Java

  • Cloneable
  • Serializable
  • EventListener
  • RandomAccess
  • SingleThreadModel
  • Remote

Advantage of using Marker interface?
Apart from using Java built-in Marker interfaces, we can also create our own Marker interfaces for our application specific needs like classify code, logically to divide the code which does its own process at run-time. For example when we go for API or framework development Marker interface plays the important role like we have in Spring, Struts etc.,

Lets see simple example of using Marker interface in Java


public interface MyMarker {
 // Marker interface without any methods or variables 
}



public class MyMarkerTest implements MyMarker {
 
 public static void main(String[] args) {
  
  MyMarkerTest obj = new MyMarkerTest();
  
  if (obj instanceof MyMarker) {
   System.out.println("obj is an instance of MyMarker");
  } else {
   System.out.println("obj is NOT an instance of MyMarker");
  }
 }
}


OUTPUT:


obj is an instance of MyMarker




Actual use of interface in Java


We may come across with this interview question as "What is the actual use of interface in Java?". By this we may be thinking of, its an alternate for multiple interface which Java doesn't have and we can implement multiple interfaces in a Java class etc.,. Apart from these answers if we see in Object Oriented interface allows to use classes in different hierarchies by Polymorphism. Lets see simple example as how its works and interface used here



public interface Animal { 
 
 public int runningSpeed();
}

public class Horse implements Animal{

 @Override
 public int runningSpeed() {
  return 55;
 }
}

public class Zebra implements Animal{

 @Override
 public int runningSpeed() {
  return 40;
 }
}

public class AnimalsSpeed {
 
 public static void main(String[] args) {
  
  Animal a1 = new Horse();
  Animal a2 = new Zebra();
  
  System.out.println("Horse Speed "+a1.runningSpeed() + " mph");
  System.out.println("Zebra Speed "+a2.runningSpeed() + " mph");
  
 }
}

OUTPUT:

Horse Speed 55 mph
Zebra Speed 40 mph


If we see above example any number of classes, across class hierarchies could implement Animal interface in their own specific way. But still be used by some caller in a uniform way and by perspective of caller, it's just a Animal interface as we can see in AnimalsSpeed class. 

       Animal a1 = new Horse();
Animal a2 = new Zebra();

Welcome your comments for more use of interface in Java.




Difference between Abstract Class and Interface


This is one of the important interview question ever asked in Java interviews like, What is the the difference between Abstract class and Interface? When we need to use Abstract class and Interface? etc., First lets walk through what is Abstract Class and Interface. 

Abstract Class:


Abstract class is a class which declared as abstract, may or may not contain abstract methods. Abstract method is a method which wont't have any method definition. So in this case abstract class can have abstract methods as well as concrete methods. Once we say these answer few other questions which may asked from interviewer are,


Abstract class can have class variable or not?

Answer: Yes.

Abstract class can have multiple abstract methods and concrete methods? 

Answer: Yes, abstract class can have any no. of abstract and concrete methods.

Abstract class can extend other abstract class and can implement interfaces?

Answer: Yes, It can extend any class and can implement N no. of interfaces. 

Abstract class can also be a Final class?

Answer: No, it can either one only. 

Abstract class can be instantiated?

Answer: No, Abstract class can not be instantiated directly. But we can instantiate by using anonymous class. 

Main thing which we need to remember is, if a class contains any abstract method then compulsory class needs to be declared as abstract.



Interface:
Interfaces will be declared only with method declarations, there will not be method definition. In general all methods are implicitly abstract methods in Interface. Suppose if a Interface declared without any method declaration or variable, then its a Marker Interface. As same as Abstract class few other questions which may be on Interface are,

Interface can have variable or not?
Answer: Yes we can have but it should be Final variable. 

Interface can extend other class ?
Answer: No, Interface can extends only other interfaces.

How many Interfaces can be extended in a single Interface?
Answer: Interface can extends multiple Interfaces like below

public interface MyInterface  extends FirstInterface, SecondInterface {

public int val = 10;

public void myMethod();
}


Difference between Abstract class and Interface?

  • Abstract class can have Abstract method and as well as concrete methods. But Interface always should will have Abstract methods. 
  • Overriding all the Abstract method is not mandatory while we extends Abstract class. But if its a Final class then we need to Override all abstract methods. On other hand when we implements Interface we need to Override all abstract methods in our class. Only in case of Abstract class we don't need to provide implementation (Override) for all methods in Interface. 
  • Abstract class methods can have any access modifiers. But Interface methods should be always public. 
  • As we seen above Abstract class and extends only one class, where as Interface can extends multiple Interfaces. 
  • Abstract class can have Final and non-Final variables. Where as Interface can contain only Final variables. 
  • Abstract class can have Constructors, but Interface cannot have Constructor.
  • As by performance Abstract class will be faster than Interface since it consumes little more time on routing to Override methods. 
  • When we add any new methods in Abstract class then the base classes won't get affected. But in case of Interface if we add any method then all implemented classes will get affected and need to implemented those new methods also.

When to use Abstract class and Interface in Java?

Whenever we talk about When to use Abstract class and Interface in Java will lead to a multiple questions and answers. So in that case programmer need to decide to choice his best in his design stage. Lets see few choices of using Abstract class or Interface according to our needs,

  • Abstract class is more suited for code reuse like inheritance, where as Interfaces are better suited for Type declaration.
  • Using Interface will be suitable if we feel API will be stable for quite long time.
  • Abstract classes are better if we add any methods in our future, since it won't break the code. 
  • Abstract class will be better when we have common method definition and also non public methods and variables. 
  • Interface will be better when we need similar to multiple inheritance. 
  • Best suitable for Template pattern will be Abstract class declaration. 
  • Prefer Interface for providing more design flexibility.