Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Tuesday, 29 October 2013

What is an Interface in Java - Marker and Nested Interfaces

An interface is a mechanism to obtain full abstraction in java, interfaces can contain constants, nested types and abstract methods with no body or implementation at all. Interfaces can not be instantiated on their own so must be implemented by a class or extended by some other interface to be used.


Why to use interfaces in Java

Interfaces plays a very important role in java programming, some of the main advantages of using interfaces in java are listed below.

1) Interfaces are used to obtain full abstraction in java code.
2) Interfaces can be used to obtain multiple inheritance in java.
3) Interfaces are used to acquire loose coupling of objects in a java application.


Class – Interface Relationship in Java

An interface have abstract methods in it and can not be instantiated, to make an interface useful one of the below statement should be followed:

1 - The interface itself must be implemented by a non-abstract class.
2- A child interface in the last of interface hierarchy must be implemented by a non-abstract class.
3- If an abstract class implements the interface, the abstract class itself must be extended by a non-abstract class.


How to create an Interface in Java

In java interfaces are created using ‘interface’ keyword, and we can have only default or public or abstract modifiers with them.
abstract interface interfaceName {
 public void unImplemented();
}

interface interfaceName {
 public void unImplemented();
}

interface interfaceName {
 public void unImplemented();
}


Methods and Variables declared in an Interface

Interfaces contain only abstract methods in them, all methods in an interface are public abstract by default no matters if we declare them abstract or not.

Interfaces can not contain instance variables in them, all variables in interfaces are static final by default no matters if we declare them static or final or not. Interface variables are static because Java interfaces cannot be instantiated in their own; the value of the variable must be assigned in a static context in which no instance exists. The final modifier ensures the value assigned to the interface variable is a true constant that cannot be re-assigned by program code.
public interface Human {

 /* all variables in an interface are public static final by default */
 int a = 10;
 static int b = 20;
 static final int c = 30;

 /* all methods in an interface are public abstract by default */
 void getAge();

 public String getName();

 public abstract void getGender();

}
/**
 * @author Nagesh Chauhan
 * 
 */
class Man implements Human {

 @Override
 public void getAge() {
  // TODO Auto-generated method stub

 }

 @Override
 public String getName() {
  // TODO Auto-generated method stub
  return null;
 }

 @Override
 public void getGender() {
  // TODO Auto-generated method stub

 }

}


If a class implements an interface, it must implement all methods of interface or the implementing class must be declared abstract. Now the class that extends the abstract class have to implement all unimplemented methods of the interface that is being implemented by the abstract class.
interface interface1 {
 public void unImplemented1();
}

class implementInterfaces implements interface1{

 @Override
 public void unImplemented1() {
  // TODO Auto-generated method stub 
 }
}

abstract class implInterface implements interface1{
 
}

class impl extends implInterface{

 @Override
 public void unImplemented1() {
  // TODO Auto-generated method stub
  
 }
}


Multiple inheritance using Interfaces in Java

Java does not allow a class to extend more than one class, hence multiple inheritance is not possible. On the other hand interfaces are not a part of class hierarchy in java so can be used to obtain multiple inheritance upto some extent.
interface interface1 {
 public void unImplemented1();
}

interface interface2 {
 public void unImplemented2();
}

class implementInterfaces implements interface1, interface2{

 @Override
 public void unImplemented2() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void unImplemented1() {
  // TODO Auto-generated method stub
  
 }
 
}


Interfaces can extend any number of other interfaces.

A class implementing an interface must implement all its methods and methods of extended interfaces of the implementing interface if any.
/*
 * an interface can extend any number of other interfaces
 */
interface interface3 extends interface1, interface2 {
 public void unImplemented3();
}

/*
 * a class implementing an interface must implement all its methods and methods
 * of extended interfaces of the implementing interface if any.
 */
class implInterface implements interface3 {

 @Override
 public void unImplemented1() {
  // TODO Auto-generated method stub

 }

 @Override
 public void unImplemented2() {
  // TODO Auto-generated method stub

 }

 @Override
 public void unImplemented3() {
  // TODO Auto-generated method stub

 }
}


What is a marker or tagged interface in Java

A marker or tagged interface is one that does not have any method in it, in java Clonable, Serializable and Remote ..etc Interfaces are marker interface, they are just used as a marker that tells the JVM to perform some important tasks. Although we can create marker interfaces but they have no use because marker interfaces are intended to give JVM instructions to perform something and this can not be done this way.
public interface Human {

}


What are nested Interfaces

In java it is possible to create an interface inside a class or an another interface, these interfaces are called nested interfaces.

Interface inside another interface

If an interface is declared inside another interface, the inner interface can not be accesses directly. Inner interface must be declared public if these reside in another interface. Nested interfaces are declared static implicitly.

If a class implements outer interface, than it is not necessary to implement inner interface methods. Same if the class implements inner interface , to implement outer interface methods are not necessary. However it is also possible to implement both outer and nested interfaces together in a single class.
public interface Human {

 public void getGender();

 interface innerInterface {
  public void getName();
 }
}

/*
 * If a class implements outer interface, than it is not necessary to implement
 * inner interface methods.
 */
class Man implements Human {

 @Override
 public void getGender() {
  // TODO Auto-generated method stub
 }
}

/*
 * Same if the class implements inner interface , to implement outer interface
 * methods are not necessary.
 */
class Women implements Human.innerInterface {

 @Override
 public void getName() {
  // TODO Auto-generated method stub

 }
}

/*
 * However it is also possible to implement both outer and nested interfaces
 * together in a single class.
 */
class Middle implements Human.innerInterface, Human {

 @Override
 public void getGender() {
  // TODO Auto-generated method stub

 }

 @Override
 public void getName() {
  // TODO Auto-generated method stub

 }
}


Interface inside a class

It is also possible to declare an interface inside an class, in this case nexted interfaces can be accesed with outer class name and are declared public static by default.
public class Fruit {
 interface Apple{
  public void getName();
 }
}

class Impl implements Fruit.Apple{

 @Override
 public void getName() {
  // TODO Auto-generated method stub
  
 }
 
}


Is it possible to declare an class inside an Interface

Yes, in java we can declare an interface inside a class. There is no issue in doing this. In this case JVM make the inner class Static bydefault. In this case inner classes and interfaces can be extended and implemented as showing below.
public interface Human {

 public class Fruit {
  interface Apple{
   public void getName();
  }
 }
}

class Orange implements Human.Fruit.Apple{

 @Override
 public void getName() {
  // TODO Auto-generated method stub
  
 }
 
}

class banana extends Human.Fruit{
 
}


Here we are done with 'Interface in Java - What are Marker and Nested Interfaces'. In our upcoming blogs we will see more about Java Programming and other opensource technologies.








Thanks for reading !
Being Java Guys Team




0 comments:

Post a Comment

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft