This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner class" Watch "inner class" New topic
Author

inner class

Tony kunds
Greenhorn

Joined: Apr 25, 2002
Posts: 25
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
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Tony kunds
Greenhorn

Joined: Apr 25, 2002
Posts: 25
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

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Apr 25, 2002
Posts: 25
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

Joined: Dec 20, 2001
Posts: 3271
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
Jackie Wang
Ranch Hand

Joined: Apr 18, 2002
Posts: 315
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

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Apr 18, 2002
Posts: 315
whoops, sorry for the silly question.
Thanks corey.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Classes nested within interfaces are always public, even this one


SCJP2. Please Indent your code using UBB Code
Tony kunds
Greenhorn

Joined: Apr 25, 2002
Posts: 25
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

Joined: Jul 03, 2001
Posts: 2120
All the members of an interface are public. I decompiled the nested class in the previous code to see that was public.
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
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


best regards,<br />Steven<br />SCJP, SCEA
Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93
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

Joined: Jul 03, 2001
Posts: 2120
Classes nested within an interface are not inner.
Tony kunds
Greenhorn

Joined: Apr 25, 2002
Posts: 25
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

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Apr 25, 2002
Posts: 25
Thanks Corey
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: inner class
 
Similar Threads
Inner class
inner class
Non static inner classes
assign outer class variable a value from inner class
how to subclass a inner class ?