• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can a class be defined inside an interface?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic