aspose file tools*
The moose likes Beginning Java and the fly likes Very simple Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Very simple" Watch "Very simple" New topic
Author

Very simple

J Abrams
Greenhorn

Joined: May 27, 2011
Posts: 3
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

Joined: Oct 24, 2010
Posts: 561
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.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4421
    
    8

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

Joined: May 27, 2011
Posts: 3
Wonderful!

Thanks a lot, and good luck!
karthik Suryanarayanan
Ranch Hand

Joined: Oct 07, 2009
Posts: 115

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.

"Learning is a Culture where your Eagerness & Curiosity plays a major Role".
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: Aug 16, 2005
Posts: 14268
    
  21

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Jesper de Jong wrote: . . .
No, he isn't, he swapped two things.
. . .
Damn! I should learn to read.
J Abrams
Greenhorn

Joined: May 27, 2011
Posts: 3
Oh, great! Thanks a million everyone! Greg, Mathew, Karthik, Campbell, Jesper- see you around...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
You're welcome
karthik Suryanarayanan
Ranch Hand

Joined: Oct 07, 2009
Posts: 115

Sorry Guys.. Made a small mistake over there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Very simple