• Post Reply Bookmark Topic Watch Topic
  • New Topic

classes inside an interface  RSS feed

 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai my code looks something like this.

interface B {

class C{

public void main(){System.out.println("hey");}

}

}





public class A implements B{

public static void main(String args[]){

C c =new C();
c.main();

}
}


Now why does java permit usage of classes inside an interface, thought the methods inside an interface can't actually use it???

Is there any specific usage for this!!!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Kumarr:

Now why does java permit usage of classes inside an interface, thought the methods inside an interface can't actually use it???


They can "use" it as argument and return types. See http://java.sun.com/j2se/1.4.2/docs/api/java/util/Map.Entry.html for an example (actually of a nested interface, but it's close enough... ).
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any specific usage for this!!!

It can be useful when you are implementing the Null Object pattern, as explained by Robert C. Martin in Agile Software Development: Principles, Patterns, and Practices

You can download the source code here

Look for the Employee.java file inside Null Object folder, to see how is it used.
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On second thought:
Assume that the methods defined in the interface throw some custom exception, and that exception will be thrown ONLY by the classes that implement that interface.
In such a case you can declare the custom exception class with in the interface, just for convenience, to show that they go together.
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey I happened to see the code Employee.jav´┐Ż.

Look at the overridden part:

public static final Employee NULLER = new Employee()
{
public void isTimeToPay(Date payDate)
{
System.out.println(false);
}

public void pay()
{
}
};

Now why is the "NULLER" object marked static, though only a class which implements the interface is going to use the NULLER object.

And The class that implements the Employee Interface inturns has access to the NULLER object even if it is not going to be marked static.

Any particular reason for marking it static.



Hey your second thought suggestion on Exceptions was valid and gud one. I never thought about it.
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All fields in an interface are implicitly public, static, and final. So, even if you don't declare it as static, it will be treated as static.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Kumarr:
Now why is the "NULLER" object marked static, though only a class which implements the interface is going to use the NULLER object.


How do you come to the conclusion that only classes which implement the interface will reference the Null Object???
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
Amidst confusion I was finally able to get hold of inner classses concept.
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only the interface class is implicitly static and final.
However the methods are not.
Try the code below.

I believe it's because a final class cannot be sub-classed, we cannot override the methods.
and ... Just because we cannot override a method doesn't mean it is final??

Is my understanding correct or Am I confused again??




package certification;
import java.util.Date;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Field;

interface Employee
{
public void isTimeToPay(Date payDate);

public void pay();

public final Employee NULLER = new Employee()
{
public void isTimeToPay(Date payDate)
{
System.out.println(false);
}

public void pay()
{
}
};
}




public class A implements Employee{

public void pay()
{ }
public void isTimeToPay(Date payDate)
{
System.out.println(false);
}


public static void main(String gopal[]) throws Exception{
A a =new A();

if(a.equals(NULLER))
{
System.out.println("jumbo");
}
Method[] methods = Employee.class.getMethods();
Field field[]=Employee.class.getFields();
boolean isFinal_pay = Modifier.isFinal(methods[0].getModifiers());
System.out.println(methods[0]);
boolean isFinal_isTimeToPay = Modifier.isFinal(methods[1].getModifiers());
System.out.println(methods[1]);

System.out.println("pay method is final: " + isFinal_pay);
System.out.println("isTimeToPay method is final: " + isFinal_isTimeToPay);

boolean isStatic= Modifier.isStatic(field[0].getModifiers());
System.out.println(field[0]);
System.out.println("Nuller object is static: " + isStatic);



}

}
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OOps!!
Sorry answer posted in wrong column.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!