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 Can a class be defined inside an interface? 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 "Can a class be defined inside an interface?" Watch "Can a class be defined inside an interface?" New topic
Author

Can a class be defined inside an interface?

Malar Ravi
Ranch Hand

Joined: Oct 15, 2001
Posts: 51
Can a class be defined inside an interface? If yes, could someone give me an example for that. I also need some good references where this topic is discussed in detail.
Thanks,
Malar.
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Yes you can have a class inside an interface
The class can even access the variable that's defined in the interface and the super interfaces.
interface I {
int a = 100;
}
interface J extends I {
int b = 1000;
void m();
class foo implements J { //static class is fine too
static void main(String args[]) {
System.out.println(a); //b is fine too
}
public void m() {}
}
U can search here in Ranch to find more discussion in this topic
But the question is.. why would some one want to insert an implementation in an interface? The idea of flexibility/OO ability will be lost, since all the classes which implements J interface will get this foo class overhead wheather we like it or not... May be there are some questions which may test more the OO fundamentals this way??? (i don't know)
Anyways Enjoy
Ragu
Malar Ravi
Ranch Hand

Joined: Oct 15, 2001
Posts: 51
Ragu, thankyou for your response. I have never come across a situation like this in my programming experience. But after taking some mock exams I realised I need to strengthen my OO fundamentals.
Thanks,
Malar.
Jeff Gaer
Ranch Hand

Joined: Jun 04, 2001
Posts: 99
I was going to ask if the access to the variables defined in the is because they are implicity static and final. Likewise would the the class would be static and final too? That would mean it could access variables in the interface ( static ) but not in the implementing class ( gosh I hope I don't see a question like this on the exam). I apologize if I am way off base here. I read the following in the java tutorial and that is how I interpreted it. PLEASE correct me if I am wrong.
The other thing that confuses me is that this states the methods defined in an interface are implicity abstract, so clearly I am missing something in the interpretation. Thanks in advance for any help of clarification.
from http://java.sun.com/docs/books/tutorial/java/interpack/createinterface.html
The Interface Body
The interface body contains method declarations for all the methods included in the interface. A method declaration within an interface is followed by a semicolon ( because an interface does not provide implementations for the methods declared within it. All methods declared in an interface are implicitly public and abstract.
An interface can contain constant declarations in addition to method declarations. All constant values defined in an interface are implicitly public, static, and final.

Sun Certified Java Programmer Java 2<P>Jeff Gaer
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
All classes defined in an interface are implicitly static.
That means it can access variables in the interface and only static variables in the implementing class.

The methods defined in an interface are implicity public and abstract, not static.

All constant values (fields) defined in an interface are implicitly public, static, and final.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Jeff,
Of course methods are abstract in Interface
It wont compile if i give {} inside an interface
And the variables are public static final.
We dont need to specify , coz they are given default
HIH
Ragu
Malar Ravi
Ranch Hand

Joined: Oct 15, 2001
Posts: 51
Hi Jeff,
I think the following code will answer your question.

Any class defined inside an interface is implicity static. In the above code aClass is a static inner class inside interface myInterface. So creating an instance of aClass is just like creating any other static inner class (or Top-level nested class). Since all the variables declared inside an interface are implicitly public static & final class aClass can access all member variables of myInterface.
You can refer JLS 9.1.3 for Interface Body and Member Declarations.
HTH,
Malar.
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Hi all'
Since a method is not static in an interface so it cannot be overidden to be static in the classes implementing that interface nor can the method have higher accessability like default,private and protected.
Am i right?
Regards
neha


nss
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
You cannot override a method to be static in any way!
JLS (8.4.6.2) speaks:

A compile-time error occurs if a static method hides an instance method.

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited November 26, 2001).]


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can a class be defined inside an interface?
 
Similar Threads
Inner class
Interfaces
class inside interface
Access modifer to interface
how to access class defined in an interface