File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner interface.........help me Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner interface.........help me" Watch "inner interface.........help me" New topic
Author

inner interface.........help me

anilkuj
Greenhorn

Joined: Sep 18, 2000
Posts: 22
interface a{
void method();
public interface b{
void method2();
}
}
public class MyClass implements a{
public void method()
{System.out.println("ha");}
public static void main(String[] arg){
MyClass mc=new MyClass();
mc.method();
}
}

what is happening how am i able to instantiate an object of class
MyClass eventhough i haven't given the body of the method in the inner interface , i know that inner interfaces are implicitly static but still that doesn't explain my question
help me soon...............
ramani2020
Greenhorn

Joined: Sep 14, 2000
Posts: 28
anil,you are implementing interface a, not interface b.
the inner interface just happens to be a member interface of the outer one.
so the compiler results are right..
am I correct? more inputs from somebody are welcome.
Ramani.
anilkuj
Greenhorn

Joined: Sep 18, 2000
Posts: 22
hi ramani!
interface a
{
void method();
public interface b
{
void method2();
}
}
public class myclass implements a.b
{
public void method2()
{
System.out.println("ha");
}
public static void main(String[] arg)
{
myclass mc=new myclass();
mc.method2();
}
}
i am sorry that was not clear, checkout the above code here i am implementing the inner interface its still working and one more doubt how am i able to access an interface like a.b as if it was a class , pls explain
anybody explain me this ...............
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi anil,
In your second code you are not implementing interfaces a and b. You used dot-notation a.b and the code compiles fine as you implemented method2() in myclass.
If the code is changed to

the compiler produces errors.
I'm not sure exactly why a.b does not produce an error...possibly because b does not directly depend on a.
Anyone know for sure?


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
vidya
Ranch Hand

Joined: Aug 08, 2000
Posts: 47
hi ..
The methods in an interface are implicitly public and abstract,and the fields in an interface are implicitly public,static,and final.I guess its member interfaces are static too, that is why you are able to access them directly using the dot notation.
Anil, I think that is why the compiler doesnt give an error .
Ajith, please confirm...
regards,
Vidya.
anilkuj
Greenhorn

Joined: Sep 18, 2000
Posts: 22
interface a
{
void method();
public interface b
{
void method2();
}
}
public class myclass implements a
{
public void method()
{
System.out.println("ha");
}
public static void main(String[] arg)
{
myclass mc=new myclass();
mc.method();
}
}

yes vidya
that's right and also that any inner interface are implicitly static so i could call a.b but what puzzled me is that when i implement a then what happens to b , even if i don't
give the body for method2().................pls clear my doubt
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I think declaring a interface within an enclosing interfaces simply provides an additional way to segregate things. for e.g
the inner interface will have to be accessed with OutInterface.InnerInterface. It will always have to accessed with respect to its enclosing interface.
So, when we say we a class is implement OuterInterface, it does not mean that it has to implement OutInterface.InnerInterface.
But a class would implement InnerInetrface as
class A implements OutInterface.InnerInterface
If we think on similar lines of static inner classes, then this would also make some sense.
What does everybody say to this?
-sampaths77
ramani2020
Greenhorn

Joined: Sep 14, 2000
Posts: 28
I think Sampath is right.
JLS experts, do come up...
I have this doubt on interfaces...
interface abcd
{
void method() throws Exception ;
}
class ABCD implements abcd
{
public void method(){}
}
the above code,compiles.I thought method() in class ABCD should also throw Exception .
could somebody tell why this is legal?
thanks,
Ramani.
anilkuj
Greenhorn

Joined: Sep 18, 2000
Posts: 22
hi Ramani,
This is from JLS 2.0,
9.4.1 Inheritance and Overriding
If the interface declares a method, then the declaration of that method is said to override any and all methods with the same signature in the superinterfaces of the interface.
If a method declaration in an interface overrides the declaration of a method in another interface, a compile-time error occurs if the methods have different return types or if one has a return type and the other is void. Moreover, a method declaration must not have a throws clause that conflicts (�8.4.4) with that of any method that it overrides; otherwise, a compile-time error occurs.
So according to that when the method is overriden it can always drop the exception without any problem.I hope that clears your problem
See U
Anil
Rahul
Greenhorn

Joined: Sep 22, 2000
Posts: 6
Originally posted by anilkuj:
interface a{
void method();
public interface b{
void method2();
}
}
public class MyClass implements a{
public void method()
{System.out.println("ha");}
public static void main(String[] arg){
MyClass mc=new MyClass();
mc.method();
}
}

what is happening how am i able to instantiate an object of class
MyClass eventhough i haven't given the body of the method in the inner interface , i know that inner interfaces are implicitly static but still that doesn't explain my question
help me soon...............


Njoy
Rahul
Greenhorn

Joined: Sep 22, 2000
Posts: 6
Anil
Try out this
When u implement both a and a.b
U need to implement both the
abstract methods
That means, u are implementing only
the interface a and NOT b

interface a{
void method();
public interface b{
void method2();
}
}
public class MyClass implements a, a.b{
public void method2() { }
public void method()
{System.out.println("ha");}
public static void main(String[] arg){
MyClass mc=new MyClass();
mc.method();
}
}
This works fine
If u comment now any of the methods
u will get an error message
Hope this clears ur doubt
anilkuj
Greenhorn

Joined: Sep 18, 2000
Posts: 22
hi rahul
what u say is perfectly right what doubt i still have is when i implement a interface why do i need not give any body for b which is inside b is it b'coz b is static implicitly but still that doesn't explain why i need not give any body for amethod inside b.
Thanx
aNiL
anilkuj
Greenhorn

Joined: Sep 18, 2000
Posts: 22
sorry in the above reply i made a mistake which i have corrected and shown in quotes below
hi rahul
what u say is perfectly right what doubt i still have is when i implement a interface why do i need not give any body "for method in b which is inside a" is it b'coz b is static implicitly but still that doesn't explain why i need not give any body for amethod inside b.
Thanx
aNiL
anilkuj
Greenhorn

Joined: Sep 18, 2000
Posts: 22
does that mean that the inner interface is independent of outer interface except for the accessibility
aNiL
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Anil,
I think that must be what is happening; although I can't find anything in the JLS to confirm it. The Java Programming Language: 2nd Edition says the following:

A static nested class [interface] acts just like any top-level class except that its name and accessiblity are defined by its enclosing class.

Which implies that a nested interface is not dependent on the enclosing class as it is always treated as a 'top-level interface'. And that seems to be what is happening in the examples.
What do you think?
anilkuj
Greenhorn

Joined: Sep 18, 2000
Posts: 22
yeah you must be right even i feel thatis the only possibility
Thanx
aNiL
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inner interface.........help me
 
Similar Threads
asking about interface 1
interface vs inner classes
Can a class be declared inside a interface?
Can I override an interface method in the class ?
asking about interfaces