Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Object arguement type casted to another Class type in a method  RSS feed

 
Alex Williams
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello fellow ranchers,

I have a quick question if you don't mind concerning recieving a object instance as an Object and then casting it to another object type, an example of it is below. The question is, if I recieve an instance as a Object type in a method of another instance, I would have to know the instance class type in order to recast it appropiately say using instanceof. But the burning question is, if I recieve an instance in a method of another instance as an Object type arguement, would that only mean that the argument is an Object type pointer reference to a specific class instance whereby the data encapsulated in the instance will remain? As far as my test below, it should - but I would just like to confirm this.

In addition, the proper way I guess to use the instance once recieved is to use the instanceof operator against known classes in order to use it's members. Please let me know if I going the right way or if I'm a complete idiot.

Just one last question, is there a better way of generalizing your program without having multiple instanceof checks? Would reflection be more appropiate? Sorry for all the questions, I just really need to get a handle on this for the Sun programmer exam. I have read multiple books from Sam's to Orielly, but I just don't find it clear cut in the examples and text.

Thanks a million for any help!

public class ClassA
{
public int a = 5;
//...
public static void main(String[] args)
{
ClassA A = new ClassA();
A.setA();
ClassB B = new ClassB(A);
}

public void setA()
{
a = 55;
}

}

class ClassB
{

public int b;

ClassB(){}

ClassB(Object o)
{
if (o instanceof ClassA)
{
ClassA a = (ClassA) o;
this.b = a.a;
System.out.println(b);
}

}
}
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a reason why you have to pass is as an Object reference? can you write a constructer that takes an A? in other words, instead of (pseudo code)


do this


if the two routines do a lot of the same stuff, but just one or two things a little different, abstract out the common stuff into a new method (which you'd probably make private).

just my 2 cents at a very early morning hour...
 
Alex Williams
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Fred!

Yes overloading would work very well, but I really wanted to understand how casting a instance to an Object type and back affected the original instance -- as in does the instance reinitialize to it's default state or does it persist as it was prior to casting it to an Object - which leads me to one last question - if I accept an instance as an Object type argument, does that imply implicit casting? If so, an explicit cast to an Object type and back would still keep member state as it was prior to casting because I again assume, that the cast (imp/exp) to Object is simply a reference pointer to the original instance class type.

Thanks!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alex Williams:
does the instance reinitialize to it's default state or does it persist as it was prior to casting it to an Object


Imagine that a variable that holds an object is like a pet's leash; the pet would be the object.

Imagine you're out for a lovely outing with your pet alligator.

Now imagine you're walking down the street and you see your friend who is blind.

Your friend is the Java compiler.

You hand your friend the leash. Your friend has no idea what's on the other end. It's just a generic leash. Could be anything: a dog, a cat, whatever.

That's like a variable of type "Object". It could be pointing to anything.

Now, you tell your blind friend "On the other end of the leash is my pet alligator." Now, the friend can, should, and will treat the leash differently, knowing what's on the other end.

You've just cast the leash to an Alligator leash.

The reason this is actually quite a good analogy is that both in this story, and in Java, the cast has absolutely no effect besides the transfer of knowledge from you to your friend the compiler. It doesn't change the leash or the alligator in any way, shape, or form. You're just telling the compiler what's on the other end of the leash.


if I accept an instance as an Object type argument, does that imply implicit casting?


Yes, in the sense that I can pass any reference type and it will compile.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,

that's just a brilliant analogy. i've never heard it before. the alligator really doesn't care if it has an alligator leash, a reptile leash, a pet leash... in fact, it doesn't even KNOW what kind of leash it has.

I used to always try and use some kind of rolodex analogy, where each card tells you where to find a person, but it fails quickly when you get to reference types.

i love this one!!!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fred rosenberger:

that's just a brilliant analogy.


Thanks! I rather like it too.
 
Alex Williams
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great work fellow ranchers, great work! Thanks a million for your help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!