Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assigning object references of different types

 
WiLL Tao
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
chafule razgul
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2591
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2591
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank for your help
 
John Spindler
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok now that's a "is a" relationship correct? now can you explain a "has a"?
thanx!
 
Anup Engineer
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Spindler
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic