• 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

inner class

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not understanding the concept of
1)how the abstact inner class is extended ? (or how it will work)
2) how the class defined in an interface will work?
any examples are appriciated
-thanks in advance
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony kunds:
I am not understanding the concept of
1)how the abstact inner class is extended ? (or how it will work)
2) how the class defined in an interface will work?
any examples are appriciated
-thanks in advance


They're used just about like any other class; you just have to refer to them a little differently. Take the following example:

As you can see, the classes nested in the interface Implementable behave just like any other class - you just have to refer to them by preceding their names with the name of the interface they're nested within.
I hope that helps,
Corey
 
Tony kunds
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi corey
thanks for the good example.
one question derived from the example.
interface members are by default abstract so defining abstract will cause compile time error.
why not abstract class cause compile time error
-thanks
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony kunds:
interface members are by default abstract so defining abstract will cause compile time error.


Interface members are not all implicitly abstract. Think about fields, for example. Granted, they're implicitly final and static, but they're definitely not abstract. Same goes for classes defined within an interface. They can have any of the modifiers attached to them, just as I showed in the above example.
Corey
 
Tony kunds
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks corey,
i got messed up & sorry for continuing
I just want to clear few things.
1)interface variables are public static and final by default
2)interface methods are public static abstract by default
3)classes inside interface are public and static by default.
If i am wrong in any thing please point in out.
I need your help.
thanks.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony kunds:

1)interface variables are public static and final by default
2)interface methods are public static abstract by default
3)classes inside interface are public and static by default.


The first one is true. The others need a little help.
Interface methods are implicitly public and abstract - they are not static. In fact, you can't have a static method in an interface. It is illegal for a method to be both abstract and static and, as all interface methods are inherently abstract, you can't have a static method in an interface.
Classes inside an interface are whatever you want them to be. They need not be public or static. Look back at the example I gave. I declared two classes within the interface and neither of them was static and neither of them was public.
Take a look at the JLS, §Interfaces for more details.
I hope that helps,
Corey
 
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:

I hope that helps,
Corey



Sorry for the question, a bit confused: it make sense to make for abstract class. But what's the meaning of abstract int? thanks!
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jackie Wang:
[QBit make sense to make for abstract class. But what's the meaning of abstract int?[/QB]


Look again! That's not an abstract int, it's an abstract method that returns an int. The name of the method is getZ.
Corey
 
Jackie Wang
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whoops, sorry for the silly question.
Thanks corey.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Classes nested within interfaces are always public, even this one
 
Tony kunds
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
confusion:

originally posted by CoreyMcGlone

Classes inside an interface are whatever you want them to be. They need not be public or static.
Look back at the example I gave. I declared two classes within the interface and neither of them was static and neither of them was public
originally posted by Jose Botella
Classes nested within interfaces are always public, even this one
code:

class MY {
private interface Inter
{ class Nested {}
}
}


I feel both are contradicting.
can Jose or corey explain me which is the correct one.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the members of an interface are public. I decompiled the nested class in the previous code to see that was public.
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A point to add:
Since all the methods of an interface are implicitly public, thus the classes which implements such interfaces should declare the methods implemented in them with the public modifier. Else, a compile error would occur.
Clement
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can we define a non static inner class in an Interface.A non static inner class requires the instance of enclosing class to be associated with it,but interface cant be instantiated.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Classes nested within an interface are not inner.
 
Tony kunds
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jose,
I read some where that classes inside interface are public and static.
Corey says they can be any access modifier.
and also the non static class provided by Corey is instantiated as we do with a static inner class.


Can I know what is the real modifier.Is it static or can be anything
Thanks
[ Adjusted spacing in code ]
[ May 09, 2002: Message edited by: Jessica Sant ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry - it would appear that I misspoke. I'm not really sure what I was thinking, but this section from the JLS sums it up pretty nicely. JLS, §9.5 Member Type Declarations:


Interfaces may contain member type declarations (�8.5). A member type declaration in an interface is implicitly static and public.


I'm sorry for the confusion. Classes defined within an interface are implicitly public and static. As they are static, they are not "inner classes," they are "top-level nested classes."
I hope that helps clear this up,
Corey
P.S.
Actually, the code I posted above proves that classes defined within an interface are implicitly static. Can you figure out how? I'm sorry for the confusion, I don't know what I was thinking. :roll:
[ May 09, 2002: Message edited by: Corey McGlone ]
 
Tony kunds
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Corey
 
reply
    Bookmark Topic Watch Topic
  • New Topic