aspose file tools*
The moose likes Java in General and the fly likes Objects (please can someone clear this up) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Objects (please can someone clear this up)" Watch "Objects (please can someone clear this up)" New topic
Author

Objects (please can someone clear this up)

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi,

I'm pretty good at java, got (SCJP,SCWCD),
but I don't fully understand objects.

For example...(suppose class BBB extends AAA)

This is what I think, but might be mixed up:
Calling new BBB() creates a new object in memory,
this object has type BBB, and is an instance of BBB.

bbb is a reference, this is of type AAA, not sure if its an instance of anything.

Then I guess bbb is pointed towards the object in memory.

Can someone clarify precisly, but clearly what happens.
Thanks
Mark Dexter
Ranch Hand

Joined: Jun 03, 2007
Posts: 34
Hi. I'm not an expert, but here is my understanding. As far as the local code is concerned, I think that

AAA bbb=new BBB();

is identical to

BBB bbb=new BBB();

I don't think there is any difference in the actual object bbb. It is still type BBB.

In the book Effective Java, Josh Bloch recommends using interfaces when possible as types instead of classes (e.g., List l = new ArrayList(); instead of ArrayList l = new ArrayList() .

But this is because it makes surrounding code more flexible. So if this was a method parameter, and it's type was List, you could change the internal implementation in the method to some other class that supports the List interface without causing any changes in the API.

So, in your example, if this was a method parameter, then the first form would be more flexible in the sense that any subclass of AAA could be substituted in the internal code without changing the API.

Hope this helps. Mark Dexter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38784
    
  23
Originally posted by Mark Dexter:
Hi. I'm not an expert, but here is my understanding. As far as the local code is concerned, I think that

AAA bbb=new BBB();

is identical to

BBB bbb=new BBB();




No, it isn't identical at all. What it means is that you can have several objects all in subclasses on AAA, and only use the methods which are inherited from AAA. It means you can have a load of different AAA objects and they can behave differently. Imagine a Vehicle class, subclasses Van and Car.
Similarly, b1.foo() does whatever foo does in BBB, c1.foo() does whatever foo does in CCC and c2.baz() does whatever baz does.

Since c1 was declared as an AAA, and AAA doesn't have a baz() method, c1.baz() will throw a compiler error.
Mark Dexter
Ranch Hand

Joined: Jun 03, 2007
Posts: 34
My bad. Thanks for the clarification. Mark Dexter
Mark Dexter
Ranch Hand

Joined: Jun 03, 2007
Posts: 34
Thanks again. Here is where I went wrong. I did the following.

Object o = new String("test");
String s = (String) o;

At this point, if you do (o == s) it is true, and if you inspect the two objects (e.g., in a debugger), they both look the same (because o and s are pointing to the same object on the heap?)

But, as you say, if you try to do a String method like

int a = o.length();

it doesn't work, because o doesn't know about the String methods. Thanks again.

you get an error, since the
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38784
    
  23
It is a little confusing about casting, because you say "casting" for casting primitive types and "casting" for casting objets, it sounds as though the two were the same process. They aren't.
You cast primitives and change their type or change their memory requirements.You have changed the number d from 64 bits to 32 bits, and you have also changed its value, from 123.45 (approx) to 123 (exactly). You can do that sort of casting with any kinds of primitives except booleans, although some primitive casts are unnecessary.

Object casting does not change the value of an object or its fields, nor does it change its type.
You can upcast, going from the more specific to the more general, for exampleUpcasting usually works, but loses specificity; you can only use the methods of Object.
Downcasting is fraught with hazard. ExampleYou are not altering the type from Object to String, you are reminding the runtime that it already is a String, and telling it you wish to use the String methods on it. If you make the tiniest error in this sort of cast, your JVM will throw a tantrum, only it calls it a ClassCastException.

Try this, which I have written on the spur of the moment:-See whether it compiles, and then what happens if you run it.

BTW: Don't say new String("Campbell") unless you specifically want two Strings with the content Campbell. Say Object o = "Campbell";.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks for the replies, but I'm not sure still.

Basically when you make an object:



You have a reference 'aaa' which points to the object in memory.

Is 'aaa' an actual object(kinda confusing, since we treat it like one in the code) or is just a reference, like an adresss?
and does 'aaa' have a type?

And for the object in memory,
Does this have a type, as well as being an instance of something?

I think I will only understand it properly if I know the mechanics of how it works, but most books don't tell you this, they just show you how to do it.

Thanks for any help.
Mark Dexter
Ranch Hand

Joined: Jun 03, 2007
Posts: 34
The Head First Java books uses the analogy of a remote control, which I have found helpful (when I'm thinking straight). Think of each button on the remote as a method in the class.

In the line:

AAA bbb = new BBB();

the object created on the heap is a BBB object, but the reference bbb is a remote control that only has the buttons (methods) from AAA. So even though the new BBB() object could potentially do all of the BBB methods (e.g., if it were cast to a BBB), the bbb reference only has AAA's methods. Does that help? Mark
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks mark, helps a little.

So if you have AAA bbb=new BBB();

If the bbb reference only has AAA's methods, how come I can call
BBB's methods on bbb?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by colin shuker:
Thanks mark, helps a little.

So if you have AAA bbb=new BBB();

If the bbb reference only has AAA's methods, how come I can call
BBB's methods on bbb?


Going with the remote control analogy, casing like

(BBB) bbb

basically means "give me a remote control that has all the buttons for BBB objects, and works on the same object the remote control "bbb" is controlling.

When the code is executed, the JVM checks whether the object "bbb" controls actually is compatible to a "BBB" remote control. If it is, all is well, if it isn't, it throws a ClassCastException.

Does that help?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Ok, so in this case, the BBB remote is compatible because it extends the AAA remote thus giving bbb the controls of both?
Mark Dexter
Ranch Hand

Joined: Jun 03, 2007
Posts: 34
Hi Colin. In your example

AAA bbb=new BBB();

are you SURE you can use ALL of BBB's methods (and not just AAA's) In you example, does BBB add one or more methods to AAA?

If you go

Object s = "test";

you can only see the Object methods on s, not the String methods. For example:

Object s = "test";
int i = s.length();

gives a "The method length() is undefined for the type Object" message.

Maybe this is causing some of the confusion? HTH. Mark
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by colin shuker:
Ok, so in this case, the BBB remote is compatible because it extends the AAA remote


The point is that the *object* is compatible to the BBB remote, because it is an instance of BBB. The object aaa is controlling knows how to react to every single button on the BBB remote, therefore casting to BBB is allowed.

Hope that helps.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi, the last post confused me more, I thought 'bbb' could only use buttons on AAA's remote.

The object aaa is controlling knows how to react to every single button on the BBB remote


Anyway...
I tried the example with String and Object, and that works(couldn't use the length() method), only works when I cast it down to a String.
But when I do the same thing with AAA and BBB classes, I am alllowed to use the print3() method of BBB.

Code is below:

Which gives...
BBB - print1
BBB - print2
BBB - print3
y=4

So thats more confusing :roll:
Any thoughts? Thanks
[ July 02, 2007: Message edited by: colin shuker ]
Mark Dexter
Ranch Hand

Joined: Jun 03, 2007
Posts: 34
Hi. I think for your example you wanted to put:

AAA bbb = new BBB();

If you make that change, it won't compile because the line

bbb.print3();

won't work. HTH. Mark Dexter
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Yes, that was a dumb error, I forgot to change it back.

In fact, I only just added the print3() method,
before I was just experimenting with overriding a method from AAA,
and implementing a method from AAA,
(I didn't actually use a brand new method like print3() in BBB).


so using AAA bbb=new BBB(),
if I remove the bbb.print3() line,
there is no problem and it out puts...

BBB - print1
BBB - print2
y=4

I think I sorta get it now...
the print1 and print2 methods are buttons on AAA's remote control.
So the overriding/implemented methods in BBB, can be called.

But print3 is not on AAA's remote, so it doesn't know what it is.
So the only way to get bbb.print3() to work is...

1. Add this method to AAA, so you override it, or implement it in BBB.
2. Cast bbb to (BBB)bbb, so bbb is then a remote control with BBB's buttons.

Have I got it?
Thanks
[ July 02, 2007: Message edited by: colin shuker ]
Mark Dexter
Ranch Hand

Joined: Jun 03, 2007
Posts: 34
Hi Colin. Sounds good to me. Mark
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks,

Theres another thing I'm unsure about,
in a C# ebook, Director class extends Employee class

I know the code isn't java, but it should be obvious what it means:


On the line denoted ***, is it necessary to do that cast?
I don't see the point of it, since myEmployee should be of type Employee.

Can anyone understand?
Thanks
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The important part here is the call to the DoSomething method. Calls to overloaded (in contrast to overridden) methods are resolved at compile time, based on the type of the "remote control" used as a parameter (in contrast to the type of the actual object that is controlled).

So

doSometing((Director) myObject)

will definitely call a different method than

doSomething((Employee) myObject)

even for myObject pointing to the same actual object.

Does that help?
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
That makes sense, but were not passing them into the DoSomething method like that, were using references:



My point is that myEmployee is already of type Employee, and if we
remove the cast:

The programs output is exactly the same.

To summarise, when we call the DoSomething method the second time,
there are 2 ways to do it.
1.

2.(like you demonstrated)


My main point is, when we use option 1, is the extra cast on myDirector needed?

Thanks
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Ah, I see. No, that cast is not necessary.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects (please can someone clear this up)