Two Laptop Bag*
The moose likes Beginning Java and the fly likes OO doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "OO doubt" Watch "OO doubt" New topic
Author

OO doubt

podonga poron
Ranch Hand

Joined: May 12, 2008
Posts: 55


thanks !!
[ May 30, 2008: Message edited by: podonga poron ]
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Where is the doubt you are talking about?


SCJP, SCWCD.
|Asking Good Questions|
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi podonga,

ClassOne d = new ClassTwo(); /* I DONT UNDERSTAND WHAT IT DOES ! HELP */

You're variable d is of type ClassOne which means (because of polymorphism) it's allowed to hold an object of type ClassOne but also of any other type which inherits from ClassOne. Because ClassTwo extends (== inherits) from ClassOne it's allowed to a assign objects of type ClassTwo to this variable of type ClassOne!

Marco
podonga poron
Ranch Hand

Joined: May 12, 2008
Posts: 55
hey thanks !

ClassOne d = new ClassTwo();

d => can only access to classOne methods and NOT classTwo methods

you can test it if you want ...

so according of what you said i'm very confuced
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

d => can only access to classOne methods and NOT classTwo methods

That's correct! I only said you can ASSIGN any subtype of ClassOne to it because any subtype of ClassOne has at least everything ClassOne has plus something more. So there's no problem when you substitute ClassTwo for ClassOne.

USAGE of the variable is another thing. Here all objects assigned to d are treated like an object of ClassOne and not more. This is all which is guaranteed and the compiler doesn't make any assumptions about the type even if it may be obvious to you that you have an object of ClassTwo here.

If you're not very familiar with the principles of OOP this may seem confusing and not very useful but there are good reasons for how this works! If it confuses you just try to go on with experimenting and learning OOP and you'll surely understand it soon.

Marco
[ May 30, 2008: Message edited by: Marco Ehrentreich ]
Rakesh Chaudhary
Ranch Hand

Joined: Feb 19, 2008
Posts: 120
SO if you have methods in parent class and if the child class is overriding it ....the delegation will take place from the child class ...i.e it will start executing the method in the child class and than the parent....class thats Polymorphism ...thanks


Acquire the unexpected.......
Preparing for SCJP 6
podonga poron
Ranch Hand

Joined: May 12, 2008
Posts: 55
very appreciated

thanks a lot !
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
Originally posted by Rakesh Chaudhary:
SO if you have methods in parent class and if the child class is overriding it ....the delegation will take place from the child class ...i.e it will start executing the method in the child class and than the parent....class thats Polymorphism ...thanks


What exactly do you mean by "... start executing the method in the child class and than the parent.."

Until its not a constructor or I am explicitly doing super call in some method, I don't see how will it go for parent class method and that too when object in question is of child class type.

[ May 30, 2008: Message edited by: amitabh mehra ]
[ May 30, 2008: Message edited by: amitabh mehra ]
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

What exactly do you mean by "... start executing the method in the child class and than the parent.."

In the example at the top there's a method print() which is inherited from ClassOne to ClassTwo and overridden in ClassTwo with a new behavior. If you have the following code:

The compiler looks at the actual ClassTwo object assigned to d for a method print(). Even if d is of type ClassOne the compiler looks at the real ClassTwo object you assigned to it! Because ClassTwo overrides the print() method inherited from ClassOne, ClassTwo's own print() will get called. If it wouldn't override print() the compiler would crawl up the inheritance hierarchy for a print method an finally find the one declared in ClassOne and call this.

I hope this makes it clearer

Marco
[ May 30, 2008: Message edited by: Marco Ehrentreich ]
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
Originally posted by Marco Ehrentreich:

If it wouldn't override print() the compiler would crawl up the inheritance hierarchy for a print method an finally find the one declared in ClassOne and call this.

I hope this makes it clearer

Marco

[ May 30, 2008: Message edited by: Marco Ehrentreich ]


Then in this case, its because of pure inheritance and not because of dynamic binding/polymorphism. The question in context was of polymorphism and run time binding.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Inheritance, polymorphism and dynamic binding are closely related so there's a little bit of everything which comes into play in the code example. On the other hand these are of course different concepts which should be confused.

I hope I didn't make it more confusing than it already was?
Rakesh Chaudhary
Ranch Hand

Joined: Feb 19, 2008
Posts: 120
Hi
All this is in reference to the comments made towards my answer....

I hope this will help




The output is :
In ANIMAL
In Dog
Proprietory action--yawning
In ANIMAL
Proprietory action--yawning

I think this wil Clear it all

Bye.....

Keep living on the edge.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: OO doubt
 
Similar Threads
Protected Behaviour
Passing a refrence to another Class vs method
Simple interface & constructor question
package statement
package problem