Basic (conceptual) question on inheritance and polymorphism

Hi,

This question is pretty basic, but this is my first time delving into thefun fun fun world of inheritance, as I have to work with some code that has 3+ levels of inheritance.

Let's say I have three classes, A, B, and C. B inherits from A, and C inherits from B. Therefore, Since B is type A, and C is type B, C is type A, correct?

I can say:

C c =new C();

and this will call of their constructors. I can also do something like:

B b =new C();

and do the same thing.

Now what confuses me is this: why can I say B = new C(), but I cannot say C = new B()? Is C not of the type B? Is this not allowed for that very purpose -- that it would be redundant to declare C as a new B(), since it already is one, but that it would NOT be redundant to declare B as a new C() because it doesn't HAVE to be one?

Did I just answer my own question?

I'd appreciate it if someone could clarify.

[1027 byte] By [Djaunla] at [2007-11-27 10:51:52]
# 1

B b = new C();

Says that C is a type of B.

C c = new B();

Says that B is a type of C, which it isn't. B is only a type of A and Object. C is a type of A, B, and Object.

hunter9000a at 2007-7-29 11:34:02 > top of Java-index,Java Essentials,Java Programming...
# 2

Argh! Useful classnames, puh-leaze!!</rant>

C is a specialization of B. B is a generalization of C. The relationship is not 2-way. Consider the following

Object a = new Socket(); // legal?

Socket b = new Object(); // legal?

Which is legal? Which would make sense to be legal? We can generalize a Socket as an Object because Object is a supertype of Socket, all Sockets are Objects. The reverse is not true, though. Not all Objects are Sockets

georgemca at 2007-7-29 11:34:02 > top of Java-index,Java Essentials,Java Programming...
# 3

class Child extends Parent {}

Parent p = new Child();

// works because every Child IS-A Parent, so when p points to a Child, it's pointing to a Parent.

Child c = new Parent(); // That Parent is NOT a child, so this fails. c must point to a Child.

jverda at 2007-7-29 11:34:02 > top of Java-index,Java Essentials,Java Programming...
# 4

> B b = new C();

Says that C is a type of

> B.

> C c = new B();

Says that B is a type of

> C, which it isn't. B is only a type of A and Object.

> C is a type of A, B, and Object.

It seems I was thinking backwards. I was thinking that declaring

C c = new B();

would say that C is a new B, not B is a new C. Remind me to use better class names in my examples next time so I don't confuse myself.

Thanks for the help

Djaunla at 2007-7-29 11:34:02 > top of Java-index,Java Essentials,Java Programming...
# 5

> Remind me to use better class

> names in my examples next time so I don't confuse

> myself.

Way ahead of you!

georgemca at 2007-7-29 11:34:02 > top of Java-index,Java Essentials,Java Programming...
# 6

Alright I think I understand the concept now. It seems I was thinking backwards, and somewhat crazily.

I just seem to think for some reason that a declaration like:

Child c = new Parent()

should be read as: The Child, c, is a Parent. Although that's backwards, that's just the first interpretation that comes to mind.

Thanks to everyone who responded.

Djaunla at 2007-7-29 11:34:02 > top of Java-index,Java Essentials,Java Programming...
# 7

> Alright I think I understand the concept now. It

> seems I was thinking backwards, and somewhat crazily.

>

>

> I just seem to think for some reason that a

> declaration like:

> Child c = new Parent()

should be read as:

> The Child, c, is a Parent. Although that's backwards,

> that's just the first interpretation that comes to

> mind.

>

> Thanks to everyone who responded.

The one on the left has to be either the same type as the one on the right, or more general

georgemca at 2007-7-29 11:34:02 > top of Java-index,Java Essentials,Java Programming...
# 8

> The one on the left has to be either the same type as

> the one on the right, or more general

But in the case of

Child c = new Parent();

isn't Child the same type as Parent, and less general than Parent?

Djaunla at 2007-7-29 11:34:02 > top of Java-index,Java Essentials,Java Programming...
# 9

> > The one on the left has to be either the same type

> as

> > the one on the right, or more general

>

> But in the case of

> Child c = new Parent();

isn't Child the

> same type as Parent, and less general than Parent?

By same I meant exactly the same. And that snippet - providing Child extends Parent - won't compile

georgemca at 2007-7-29 11:34:02 > top of Java-index,Java Essentials,Java Programming...
# 10

> By same I meant exactly the same. And that snippet -

> providing Child extends Parent - won't compile

I know the snippet won't compile.

Ah, I see. Child is less general than parent, therefore the one on the left is NOT more general, therefore it's incorrect. Makes sense.

Inheritance is so screwy. Polymorphism is much easier to understand when a class implements an interface than when it extends another class, in my opinion.

I wish there was a way to state the declaration in a sentence type structure, as I find those easy to understand.

Maybe in the case of

Parent p = new Child();

one could say something like, "a new specialization of Parent is Child, p."

Djaunla at 2007-7-29 11:34:02 > top of Java-index,Java Essentials,Java Programming...
# 11

In your original example,

Object -> A -> B -> C

^^

MostMost

general specific

hunter9000a at 2007-7-29 11:34:02 > top of Java-index,Java Essentials,Java Programming...
# 12

> > By same I meant exactly the same. And that snippet

> -

> > providing Child extends Parent - won't compile

>

> I know the snippet won't compile.

> Ah, I see. Child is less general than parent,

> therefore the one on the left is NOT more general,

> therefore it's incorrect. Makes sense.

>

> Inheritance is so screwy. Polymorphism is much easier

> to understand when a class implements an interface

> than when it extends another class, in my opinion.

>

> I wish there was a way to state the declaration in a

> sentence type structure, as I find those easy to

> understand.

>

> Maybe in the case of

> Parent p = new Child();

one could say

> something like, "a new specialization of Parent is

> Child, p."

How about "A parent, specifically a child, called p". (throws ClumsyClassnameException)

How about the old examples?

class Animal {}

class Dog extends Animal {}

Animal rover = new Dog();

"I need an animal, called Rover. Specifically, I need a Dog"

georgemca at 2007-7-29 11:34:02 > top of Java-index,Java Essentials,Java Programming...
# 13

> How about "A parent, specifically a child, called p".

> (throws ClumsyClassnameException)

>

> How about the old examples?

>

> > class Animal {}

> class Dog extends Animal {}

> Animal rover = new Dog();

>

>

> "I need an animal, called Rover. Specifically, I need

> a Dog"

Haha, I do like the Dog example. I actually think that cleared it up for me.

I guarantee one of these days I'll get some project where I have to use code in my weakest areas: inheritance, polymorphism, and recursion. Oh well, I live and learn.

Thanks everybody for the help!

Djaunla at 2007-7-29 11:34:02 > top of Java-index,Java Essentials,Java Programming...
# 14

> > How about "A parent, specifically a child, called

> p".

> > (throws ClumsyClassnameException)

> >

> > How about the old examples?

> >

> > > > class Animal {}

> > class Dog extends Animal {}

> > Animal rover = new Dog();

> >

> >

> > "I need an animal, called Rover. Specifically, I

> need

> > a Dog"

>

> Haha, I do like the Dog example. I actually think

> that cleared it up for me.

>

> I guarantee one of these days I'll get some project

> where I have to use code in my weakest areas:

> inheritance, polymorphism, and recursion. Oh well, I

> live and learn.

>

> Thanks everybody for the help!

Don't forget that both the extends and the implements keywords are ways of expressing inheritance! I'm not a big fan of 'extends'. Google "favour composition over inheritance" for details

georgemca at 2007-7-29 11:34:02 > top of Java-index,Java Essentials,Java Programming...