The Artima Developer Community
Sponsored Link

Design Forum
java.lang.Object is a class?

7 replies on 1 page. Most recent reply: Mar 30, 2003 6:58 AM by Ronald Tetsuo Miura

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
Raj Sesetti

Posts: 3
Nickname: sesetti
Registered: Jan, 2003

java.lang.Object is a class? Posted: Jan 20, 2003 1:58 PM
Reply to this message Reply
Advertisement
Why should the "root" be a concrete object when most of its methods are more fitting to make it an interface?

Also, it's surprising to see that "String" class is seen in the definition of "Object" class. With "String" as a subclass of "Object", the basic tenet of inheritance is broken. How would a class the existence of its subclass?

Any thoughts?


Tj

Posts: 7
Nickname: tj
Registered: Jan, 2003

Re: java.lang.Object is a class? Posted: Jan 27, 2003 11:05 PM
Reply to this message Reply
> Why should the "root" be a concrete object when most of
> its methods are more fitting to make it an interface?

So you don't have to reimplement every single interface method when you just want to make a class.


> Also, it's surprising to see that "String" class is seen
> in the definition of "Object" class. With "String" as a
> subclass of "Object", the basic tenet of inheritance is
> broken. How would a class the existence of its subclass?

Java can deal with these circular references. If you think about it, what if you had a factory class that could return subclasses of itself, depending on what it decides you need?

Admittedly, it does look strange. Recursion tends to look that way. And it's often not the best design. But in the real world, don't parents sometimes ask their children to do little tasks for the house?

Raj Sesetti

Posts: 3
Nickname: sesetti
Registered: Jan, 2003

Re: java.lang.Object is a class? Posted: Jan 28, 2003 3:16 PM
Reply to this message Reply
Exactly. It is bad design. (Or is there a compelling design reason why it was done?)

I understand java can handle circular references.

But if you are writing java.lang.Object class, how do you even know if there is a String subclass?.

I guess it would have been cleaner (from design perspective):

1) if Object is an interface

2) if every class implicitly implements this interface

3) (Note this->) if there is an "ObjectPeer" implementation that is attached this interface during JVM startup. This ObjectPeer implementation is specific to JVM vendor.
(Remember ButtonPeer?)

4) if toString() method is done away with.

Tj

Posts: 7
Nickname: tj
Registered: Jan, 2003

Re: java.lang.Object is a class? Posted: Jan 28, 2003 4:38 PM
Reply to this message Reply
I do agree it sort of sticks out. I should read that bible of OOP by Meyer; I'm sure people have talked about this. But we /do/ know that the String class must exist, otherwise the code won't compile. The Object class is dependent on the String class' existence to compile. So many design mistakes of this kind are not allowed to occur.

But I should get more experience with other, more powerful OOP languages. It is an interesting issue, and your ObjectPeer idea might be a good insight.

Joan Friedman

Posts: 6
Nickname: joan
Registered: Jan, 2003

Re: java.lang.Object is a class? Posted: Jan 30, 2003 2:59 PM
Reply to this message Reply
How would the ObjectPeer do anything but duplicate the work of the current Object class?

Karthicraja

Posts: 3
Nickname: alice
Registered: May, 2002

Re: java.lang.Object is a class? Posted: Jan 31, 2003 11:02 PM
Reply to this message Reply
I think Java is considering String as a primitive data type in the usage though this is implemented using class. May be this is the reason they didn't mind it.

Raj Sesetti

Posts: 3
Nickname: sesetti
Registered: Jan, 2003

Re: java.lang.Object is a class? Posted: Feb 7, 2003 10:00 AM
Reply to this message Reply
> How would the ObjectPeer do anything but duplicate the
> work of the current Object class?

You are right. But, at least, you need not compromise Object interface to do other work ( e.g., getting object description from toString() method ).

I think what Karthikraja wrote makes some sense. To make it easy, String is treated as an almost-basic datatype. Yet, I still wonder why they didn't choose char[] or something like that.

Ronald Tetsuo Miura

Posts: 3
Nickname: tetsuo
Registered: Mar, 2003

Re: java.lang.Object is a class? Posted: Mar 30, 2003 6:58 AM
Reply to this message Reply
> Yet, I still wonder why they didn't choose char[] or
> something like that.

Maybe to make programming easier.
If Object was an interface, we would to:
- implement its methods in every class, or
- extend explicitly a concrete class that implements that interface
And it wouldn't guarantee that every class is subclass of Object.
Another option would be extending implicitly a concrete class that implements that interface (that would be the same as extending the current Object class)

And, if the "String toString()" was "char[] toCharArray()" instead, would you be happy having to write "System.out.println(new String(obj.toCharArray()))" instead of "System.out.println(obj)"? (if you follow the rule of not making any attribute public, it would be "System.getOutputStream().println(new String(obj.toCharArray()))"

Bad design isn't the design that doesn't follow the OO rules strictly, is the design that doesn't achieve what it is intended to. A good design should do what it is intended to do, in a clean and simple way. In the case of APIs, it should ease programming.

Flat View: This topic has 7 replies on 1 page
Topic: Frames - How can horizontal Scroll Previous Topic   Next Topic Topic: Please help me with a program

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use