Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-07-2008, 12:12 PM
Niveditha's Avatar
Senior Member
 
Join Date: May 2008
Posts: 299
Niveditha is on a distinguished road
Send a message via Skype™ to Niveditha
[SOLVED] Confused with output sequence
I am a bit confused with the output given for this code. Can anyone help me?
Code:
class Alpha{ public void foo(String.. agr) { Sysout("Alpha:foo");} public void bar(String a) { Sysout("Alpha:bar");} } public class Beta extends Alpha{ public void foo(String.. agr) { Sysout("Beta:foo");} public void bar(String a) { Sysout("Beta:bar");} public static void main(String argv[]){ Alpha a=new Beta(); Beta b = (Beta)a; a.foo(test);b.foo(test); a.bar(test);b.bar(test); } }
I felt the ans should be Alpha:foo Beta:foo Alpha:bar Beta:bar
but the ans given was
Alpha:foo Beta:foo Beta:bar Beta:bar

Am i going wrong somewhere?
__________________
To finish sooner, take your own time....
Nivedithaaaa
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-07-2008, 12:25 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Why are you confusing. It's fine. On line,

Code:
Alpha a=new Beta();
you define a type Alpha to the object reference a, but assign to type Beta. Think about from there.

I don't see any confusion there at all.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-07-2008, 12:30 PM
baskar.nitt's Avatar
Member
 
Join Date: Apr 2008
Location: Chennai, India
Posts: 18
baskar.nitt is on a distinguished road
Hi Niveditha,

When running the code which you have posted i got the following output:

Beta:foo
Beta:foo
Beta:bar
Beta:bar



Just check your program output..
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-07-2008, 12:33 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Yes Nidveditha. Seems to me you are conflict with reference and overridden there. Start from the simple step. As I said earlier, you always refer the Beta class, not Alpha class.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-07-2008, 02:18 PM
Niveditha's Avatar
Senior Member
 
Join Date: May 2008
Posts: 299
Niveditha is on a distinguished road
Send a message via Skype™ to Niveditha
Ok..
That was my confusion. If it refers to Beta class then even the firsto/p shud have been Beta:foo as baskar mentioned but the ans given was Alpha:foo
Beta:foo
Beta:bar
Beta:bar


So i felt i was understanding in a wrong way,i cant expect internet to be correct always rite?
Anyways thank u guys...
__________________
To finish sooner, take your own time....
Nivedithaaaa
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-08-2008, 05:28 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
You are welcome.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in parsing sequence diagram using emf Saniya Eclipse 0 06-28-2008 05:54 AM
Using escape sequence with like clause (%) Java Tip Java Tips 0 02-14-2008 10:57 AM
Using escape sequence with like clause (_) Java Tip Java Tips 0 02-14-2008 10:55 AM
Escape sequence problem eva New To Java 2 01-21-2008 11:15 PM
Create entity field SEQUENCE oracle Marcus Database 1 06-27-2007 04:31 PM


All times are GMT +3. The time now is 02:47 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org