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 All Objects always extend Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "All Objects always extend "Object"" Watch "All Objects always extend "Object"" New topic
Author

All Objects always extend "Object"

Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
Here is a question from Exam Cram Chapter 5:
Question # 8:
Which of the following statemets is true:
a. An inner class can have the same name as its enclosing class.
b. An instance of a non-static inner class always has an associated instance of the enclosing class.
c. An anonymous inner class is always assumed to extend Object.
My answers where b and c.
Where as the answer given in the book is only b.
The explanation given in the book:
"Only answer b is correct. Answer a is incorrect because inner classes are prohibited from having the same name as the enclosing class. Answer c is incorrect because an anonymous inner class can extend any class; however, the declaration syntax does not use the words "extends"."
My Point:
"All Classes in Java are always assumed to be derived from Object class i.e. they are assumed to be extending the Object class."
So, when the author mentions Object do I assume it is not java.lang.Object?
Brian
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
What answer c means is that an anonymous class cannot extend anything else than clas Object, which is clearly wrong...
See the discussion posted yesterday about the same topic: http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014904


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
Thanks for the link Valentin. You wrote something in this thread:
"All classes extend Object anyway"
I do not know if I am fighting with my English knowledge over here.
One more thing:
"What answer c means is that an anonymous class cannot extend anything else than clas Object"
I do not see the word "only" in the choice c.
Had it been worded as:
"An anonymous inner class is always assumed to extend Object only"
then I can understand author's and your point.
Brian
[ February 22, 2002: Message edited by: Brian Lugo ]
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
i thought like you, till i read one of Valentin excellent explantions.
a class extends Object only if it doesnt exetend any other class. BUT , it always inherit object in some way up the chain.
so, for example my class called MyClass can extend Frame, but Frame extends Object so down the chain i inherited Object as well, although i dont extend it directly.
to sum it up, all classes u write in java will inherit Object in some way, either by directly extending it or by another class u inherit from that extends it. (thus the rule for not allowing multiple inheritence in java is conserved).
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
then again, i may have got it wrong completly !
in that case i apologize. (we will wait to see if someone corrects me).
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
Thats' a good explanation Roy:
My point is:
is inheritance == extending?
I have read everywhere that all classes extend/inherit Object class. If we were to dissect the problem a bit more:
The author also mentions the word "assumed" and does not put it as:
"An anonymous inner class has to explicity extend Object" OR
"An anonymous inner class is always assumed to extend Object only"
I am paying very close attention to the word "assumed".
So I am dealing with the thin line of :
1. Does "assuming one class extends another" means " one class is implicitly extending/inheriting another class".
I dunno, I hope I don't get wierd questions like this.
Brian
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Here is how I understand the wording of this question:
"An anonymous inner class is always assumed to extend Object."
means that an anonymous class always extends Object directly and not through some other way up the hierarchy. But that's me, I may be wrong...
Holmes Wong
Ranch Hand

Joined: Feb 18, 2002
Posts: 163
I think ans c's wording is not clear enough, it shoud changed to:
An anonymous inner class is always assumed to extend Object directly.
Then c is wrong.
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
That's a good explanation Valentin, and I think when some one says that a Object x extends Object y, I tend to think in broader terms:
1. Does y extend x directly OR
2. Does y extend x through hierarchy.
I don't know if you have noticed in a lot of books it is mentioned:
"All classes (implicitly) extend/inherit Object (i.e. java.lang.Object) class."
I guess its just semantics. I don't think I can tell you that your way of thinking is wrong and vice versa.
Anyways, this was a good discussion no hard feelings for anybody,
Brian
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
Okay folks I finally got my RHE book yesterday and this is what the authors have to say (on page 248) in the section "The Object Class":
"The Object class is the ultimate ancestor of all Java classes. If a class does not contain the "extends" keyword in its declaration, the compiler builds a class that extends directly from Object.
I guess that says it all ...
And here is a code that I took from Thinking In Java by Bruce Eckel and modified it a bit:

It compiles and runs fine, I can access the getClass() method of Object class.
PS - I also realize that I should have titled this thread: "All Classes always extend "Object" Class" - Sorry for the confusion
[ February 26, 2002: Message edited by: Brian Lugo ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Well, it says a lot but it doesn't say it all
Going back to the original question, here are a few examples in which C is not a correct answer:
JButton b = new JButton("test");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
Toolkit.getDefaultToolKit().beep();
}
});

In this example, we have an anonymous inner class that implicitly extends Object and implements the ActionListener interface.

But here's a different case:

b.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me)
{
Toolkit.getDefaultToolKit().beep();
}
});
Now we have an anonymous class that implicitly extends MouseAdapter, a class! It does NOT extend Object, directly or indirectly, although Object is an ancestor of this anonymous class.


Rob
SCJP 1.4
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
Rob,
If I write the following code, what does it do?:

I believe the anonymous class still extends the Object class directly in this case. The compiler even generates a .class file for the anonymous inner class over here.
Brian
PS - Or maybe the compiler must be explicitly extending it when it sees the keyword "this"?
[ February 26, 2002: Message edited by: Brian Lugo ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Here's the declaration of WindowAdapter from the JavaDocs:
public abstract class WindowAdapter
extends Object
implements WindowListener
It's a class. So your anonymous class is extending WindowAdapter, not Object.
your anonymous class IS a WindowAdapter.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Brian,
the generated class file (using javap) for the anonymous inner class is

As you can see it extends java.awt.event.WindowAdapter and not Object directly...
[ February 26, 2002: Message edited by: Valentin Crettaz ]
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
Thats pretty cool Valentin!
How can I see/edit the generated class file the way you just did it?
Brian
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Just compile your source with javac and then run javap with the class file like this:
javap Someclass (without the ".class" suffix)
you also have different switches you can use like -c -l -s etc...
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
Okay thats good to know!
Thanks Valentin!
Although I need to correct Rob's statement:
"Now we have an anonymous class that implicitly extends MouseAdapter, a class! It does NOT extend Object, directly or indirectly, although Object is an ancestor of this anonymous class."
In this case Object is being extended indirectly
through MouseAdapter class.
Going back to our problem:
Is'nt it true:
You can only extend one object at a time?
and when you are already extending an object the compiler is not going to put additional class (i.e. java.lang.Object) in the extends clause.
In the original question the option c was:
c. An anonymous inner class is always assumed to extend Object.
Do I infer from this statement that the author is implicitly saying that "an anonymous inner class" means that : "it could be extending another class"? (Well this is again true if we are assuming that the author is talking about direct extension and not in-direct extension)
Brian
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
extends has a specific meaning in java. When you say class B extends A, it means that A is a direct superclass of B.
If now C extends B, you cannot say C extends A. That is incorrect terminology. You can say C inherits from A, or A is an ancestor superclass.
SO, if Class A extends some class other than Object, it is not correct to say that A extends Object.
When we talk about explicit/implicit extension, we are talking about whether or not the keyword extends is explicit or implicitly used.
For example:
public class Foo {}
Here, Foo extends Object implicitly, because in the absence of any explicit superclass, the compiler assumes you are extending Object.
If you then write
public class Bar extends Foo {}
Then Bar extends Foo, NOT Object. Also, Bar extends Foo explicitly because we inlcude the keyword extends.
Contrast this with an anonymous class declaration:
Bar anonBar = new Bar(){};
anonBar now contains a reference to an anonymous class you declared on the right; this class implicitly extends Bar, because
1. The anonymous class is a direct subclass of Bar
2. The keyword extends is not used here. It is implied.
[ February 26, 2002: Message edited by: Rob Ross ]
[ February 26, 2002: Message edited by: Rob Ross ]
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
Rob,
I understand whay you mean, this is the way I see it:
class A{}
class B extends A {}
class C extends B{}
A is extending java.lang.Object
B is extending A
C is extending B
Rule: You can only extend one class.
Now can I say that C is extending A and "assume" it is extending java.lang.Object (indirectly) too ? or
can I say that B is extending A and "assume" it is extending java.lang.Object (indirectly) too?
I think all this implies is:
When you say that a class B is extending A and given the fact that A is extending Object can you say B also extends Object?
I guess the difference lies in interpreting the word "extend", you can think of it as if we are talking generally or we are talking about it strictly in terms of java keyword "extend".
So, now I can see how the confusion stems from these types of questions.
Also, the author just mentions "an anonymous inner class" the first thought that comes to your mind is a "plain or top-level/base" anonymous inner class and not an anonymous inner class that is extending some other class.
Brian
[ February 26, 2002: Message edited by: Brian Lugo ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
To sum up a little bit, remember that:
- implicit means that the compiler does some job for you (i.e. add "extends java.lang.Object", create the default no-arg constructor, etc.)
- explicit means you do all that by yourself, that is you "explicitely" specify what class your class is extending.
- A extends B means that A is a direct subclass of B. But then, if B extends C, A does not extend C, A inherits from C. I know, it's picky, but by using a good terminology you help yourself and the others and you learn faster as well since you don't waste time trying to clear up your question several times.
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
I think I agree with you Valentin and maybe now I understand author's view point in this question.
If the author would have asked:
"An anonymous inner class, that does not have keyword extends OR is not extending any class explicitly, is always assumed to extend Object."
then this would be true.
And infact this would be true for any class not only Anonymous class.
But when the author does not mention what type of anonymous inner class he is talking, it is incorrect to assume that that anonymous inner class is just the base class and is not the derived class.
Thanks Valentin and Rob: this question is funky.
I have to now program my mind with the following terminology to clear up the confusion:
1. extends
2. inherits
Brian
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: All Objects always extend "Object"
 
Similar Threads
Anonymous class....
inner class mock exam question
inner class java test question
Anonymous class
Exam Q on Bill Brogden site