*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assigning object references of different types 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 "Assigning object references of different types" Watch "Assigning object references of different types" New topic
Author

Assigning object references of different types

WiLL Tao
Ranch Hand

Joined: Dec 14, 2001
Posts: 56
I run the code, and i know the result but confused.
i think c is the child class of b,maybe have more addition details,so assgin c to b should cause error and b assign c should run fine.
but when i complie this code and run, the result is opposition. anyone can explain this?
Thanks!
----------------
class base
{
}
public class child extends base
{
public static void main(String[] args)
{
base b = new base();
child c = new child();
b=c;
c=b; //error,but i think c is have all details of b and should ok
}
}
------------------------
[ March 20, 2002: Message edited by: WiLL Tao ]

Thank Folks who <b>Make Sense</b> here.<br />SCJP Platform 2
chafule razgul
Ranch Hand

Joined: Feb 09, 2002
Posts: 63
Here's one rudimentry explanation.
Say there is a parent class P and a child class C
now consider pp, an instance of P, and cc, an instance of C.
In the assignment pp = cc, the compiler knows cc ISA pp (if you are not sure about this, then search for OO documents on inheritance and polymorphism) Hence it lets this happen.
In the assignment cc = pp however, pp is not a type of cc, rather the other way round. The compiler checks this by looking at the inheritance hierarchy, and since the inheritance tree is as such:
P+
|
|__C
an instance of C is also an instance of P, but not the other way round, hence you end up with the result you see.
HTH
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

Let me try -
This is how it works. There's a principle called "principle of substitution" in OOPs terminology, which states that - You should be able to use B (subclass) wherever A (superclass) can be used.
Apply this principle in your case and you can see that the subclass (Child) can have more functionality in it apart from the base class functionality, so it can be used for base class (b=c), but *NOT* otherwise. Because base class may not have all of its subclass' functionality, maknig c=b inappropriate.
In other words "Subclass is-a Superclass, but Superclass is NOT a Baseclass."
Of course, you can always do c=(Child)b; by explicit casting, but if at Runtime b is not actually Child, it will throw a ClassCastException.
HTH,
- Manish
[ March 20, 2002: Message edited by: Manish Hatwalne ]
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

OK !
Here is a better analogy, in plain english.
Car (Subclass) is a Vehicle (SDuperclass), but Vehicle is not a Car.
So Vehicle = Car makes sense, but Car = Vehicle doesn't make sense here. Hence...
HTH,
- Manish
WiLL Tao
Ranch Hand

Joined: Dec 14, 2001
Posts: 56
Thank for your help
John Spindler
Greenhorn

Joined: Feb 12, 2002
Posts: 28
Ok now that's a "is a" relationship correct? now can you explain a "has a"?
thanx!
Anup Engineer
Ranch Hand

Joined: Mar 04, 2002
Posts: 48
Let me try..
say you have a class called Car.
and your BMW class that extends Car.
here its an "is a" relationship. Because BMW is a car.
Now, your BMW has a mirror.
class BMW{
private mirror m;
}
Here its a "has a" relationship. Because the BMW object when created will have a mirror object within itself.
I dont get to do this often(the explanation), i am generally at the other end
HTH.


Anup Engineer
John Spindler
Greenhorn

Joined: Feb 12, 2002
Posts: 28
I think I'm really confused! hehe
A Parent class has child classes and these child classes have an "is-a" relationship to there Parent right? As in Manishes example a Vehicle "is-a" Car? no right? a Car "is-a" Vehicle? and a Car
"has-a" mirror (as Anup would say)
So then wouldn't this be valid:
Child = Parent?
Because Child "is-a" Parent through inheritance right?
But :
Parent = Child?
Is not correct right? Becuase Parent is not a Child.
Help me if I'm totally astray with this. I am studying OO and am trying to get a good grasp on all this.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
ok let's put it this way:
You have a superclass Vehicle.
You have 3 subclasses of Vehicle named Car, Boat and Plane.
The three subclasses are siblings, they have nothing in common except the fact that they are all Vehicle. Car is a Vehicle, Boat is a Vehicle and Plane is a Vehicle.
Now you can reference any subclass object with a variable of the superclass type, like this:
Vehicle c = new Car();
Vehicle b = new Boat();
Vehicle p = new Plane();
the is_a relationship goes from right to left.
You cannot reference a Plane object with a variable of type Car because a Car is not a Plane, that is you cannot do the following:
Car wrong = new Plane();
Nor can you do reference a Vehicle with a Car reference variable:
Vehicle v = new Plane();
Car wrong = v; //compiler error
because the variable v (of type vehicle) is referencing an object of type Plane which cannot be referenced by a variable of type Car, which makes sense. Even if you put a cast
Car wrong = (Car)v;
this isn't gonna work at runtime because it is impossible to cast a Plane to a Car.
Remember that a reference variable of the type of the superclass can always reference objects of the subclasses, but not the other way around.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assigning object references of different types
 
Similar Threads
ClassCastError vs. Compilation Error
Problem with instanceof
a question about default constructor
Interface instance?
[Java Programmer Certification Mock Exam (Dan Chisholm) wrong solution..