ClassA hi = new ClassB()

What does it mean when you initialize an object of one type, and then declare it as another type? I don't get what this does or what the purpose is.As an exampleClassA hi = new ClassB();
[208 byte] By [Lava_Javaa] at [2007-11-27 10:07:43]
# 1
Google for "Abstraction in Java".
_helloWorld_a at 2007-7-13 0:44:01 > top of Java-index,Java Essentials,New To Java...
# 2
> Google for "Abstraction in Java".Followed closely by "polymorphism"
georgemca at 2007-7-13 0:44:01 > top of Java-index,Java Essentials,New To Java...
# 3
also check inheritance in java http://www.google.com/search?q=inheritance+in+java&sourceid=navclient-ff&ie=UTF-8&rlz=1B3GGGL_enUS225US225&safe=active
kilyasa at 2007-7-13 0:44:01 > top of Java-index,Java Essentials,New To Java...
# 4
I'm somehwat familiar with all these terms. I'm just not familiar with the concept of declaring an object one type and initializing it as another. And while GOogling these terms, I couldn't find any examples specific to what I'm talking about.
Lava_Javaa at 2007-7-13 0:44:01 > top of Java-index,Java Essentials,New To Java...
# 5
Example:Set < Foo > x = new HashSet < Foo > ();The important thing is that x is a set. That it's implemented by hashing is an implementation detail.
BigDaddyLoveHandlesa at 2007-7-13 0:44:01 > top of Java-index,Java Essentials,New To Java...
# 6

> I'm somehwat familiar with all these terms. I'm just

> not familiar with the concept of declaring an object

> one type and initializing it as another.

Mammal furry = new Moggy("Tibbles");

...

furry = new Mouse("Gerald");

...

The types have to be compatible. If furry was declared as having type Moggy we wouldn't be allowed to assign a Mouse instance to it later on.

dcmintera at 2007-7-13 0:44:01 > top of Java-index,Java Essentials,New To Java...
# 7

> ClassA hi = new ClassB();

Car c = new Porsche();

On the left side we're saying, "c is the spot where a car is parked."

On the right side we're saying, "the particular car that's parked in slot c is a Porsche."

"Hand me a screwdriver."

"Here's a Crafstsman phillips-head."

"Put some fruit in this bag."

"Here's an apple."

jverda at 2007-7-13 0:44:01 > top of Java-index,Java Essentials,New To Java...
# 8
I see this devolving into a tortured analogy competition :-)
dcmintera at 2007-7-13 0:44:01 > top of Java-index,Java Essentials,New To Java...
# 9
Not really.I was just trying to relate the concept to more real-world-ish, code-free examples.
jverda at 2007-7-13 0:44:01 > top of Java-index,Java Essentials,New To Java...
# 10
> I see this devolving into a tortured analogy competition :-)Frankly, I'd prefer he keep is fruit in his own bag, thank you.~
yawmarka at 2007-7-13 0:44:01 > top of Java-index,Java Essentials,New To Java...
# 11
> > I see this devolving into a tortured analogy> competition :-)> > Frankly, I'd prefer he keep is fruit in his own bag,> thank you.Oh, sure, you say that now.
jverda at 2007-7-13 0:44:01 > top of Java-index,Java Essentials,New To Java...
# 12
> Oh, sure, you say that now.Well, yeah. You've been bagging fruit all over town; you can't expect me to act like nothing's changed.~
yawmarka at 2007-7-13 0:44:01 > top of Java-index,Java Essentials,New To Java...
# 13
> > Oh, sure, you say that now.> > Well, yeah. You've been bagging fruit all over town;> you can't expect me to act like nothing's changed.> > ~I'm on a mission from God.
jverda at 2007-7-13 0:44:01 > top of Java-index,Java Essentials,New To Java...
# 14
> > I'm on a mission from God.I don't like your God. Your god scares me.
thousea at 2007-7-13 0:44:01 > top of Java-index,Java Essentials,New To Java...