Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Very simple

 
J Abrams
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

Very simple:

Parent obj1 = new Parent();
Parent obj2 = new Child();

class Parent{}
class Child{}

I DON'T UNDERSTAND, WHAT JUST HAPPENED ON LINE 2?

Thanks!
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 2 should be:


In your version, you declare obj2 to be a Parent object but then use the Child() constructor to define it. That's incorrect.

Please use code tags and meaningful thread titles.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. Welcome to The Ranch!

Your line 2 would be OK, though, if you changed the definition of Child to:
In that case, because a Child IS-A Parent, you can assign it to a Parent reference variable. Everything still works because of polymorphism.
 
J Abrams
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wonderful!

Thanks a lot, and good luck!
 
karthik Suryanarayanan
Ranch Hand
Posts: 121
Java Opera Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abrams,
Welcome to the JavaRanch . Perhaps you would understand better by the following one.

If we have a Parent Child relationship like below(ie)Child extends Parent), we can create objects for those classes as follows.

Parent
|
Child

Possible Ways :
==========

The impossible way is


The reason is that
1.)Parent class can create an object of its own and use its variables and methods.(By default all classes extend to Object class)
2.)A child class can also create an own object of it like the parent class.
3.)When a child class extends to a parent class, it can create an Object of itself while it's Reference can point to Super class Object(in this case the Parent class) , since it inherits properties from the Parent class .
4.) A Parent class will not know it's child classes behaviours & properties. So it's impossible to refer it's object to it's child class.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Karthik Suryanarayanan's explanation is correct.

Parent and Child are bad names for those classes; they should be superclass and subclass. Look for a book which doesn't use such bad nomenclature.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15482
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Karthik Suryanarayanan's explanation is correct.

No, he isn't, he swapped two things.

These are correct:


And this one is wrong:

This will get more clear when you use better names than "Parent" and "Child". For example, use "Animal" and "Dog". A Dog is an Animal, but an Animal is not always a Dog:
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote: . . .
No, he isn't, he swapped two things.
. . .
Damn! I should learn to read.
 
J Abrams
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, great! Thanks a million everyone! Greg, Mathew, Karthik, Campbell, Jesper- see you around...
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
karthik Suryanarayanan
Ranch Hand
Posts: 121
Java Opera Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Guys.. Made a small mistake over there.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic