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 06-30-2007, 07:33 PM
Member
 
Join Date: Jun 2007
Posts: 11
creativehacker is on a distinguished road
confused code
Can anyone explain abt the output of this code..
class A
{
int x=1;
public void show()
{
System.out.print("hello");
}
}

public class B extends A
{
int x=2;
public void show()
{
System.out.println("hi");
}

public static void main(String args[])
{
A a;
B b=new B();
a=b;
a.show();
System.out.println(a.x);
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-01-2007, 08:43 AM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
First of all it gives the following output:

Quote:
hi
1
I think this can be explained like:

When you declare a with "A a;", you define a object pointer of type A. So whatever this object pointer points is a type of A and considered to be an A. Actually if you try to assign some other object which does not inherent from A, compiler will return an error.

So in the following line, when you assign b to a with "a=b;", b object is considered to be an instance of A and when you access x variable, the compiler gets its value from A object.

What do you think?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-01-2007, 04:52 PM
Member
 
Join Date: Jun 2007
Posts: 11
creativehacker is on a distinguished road
Then y doesn't it print "hello"
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-01-2007, 05:31 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Quote:
Then y doesn't it print "hello"
Hmm, i did not notice that. Interesting..
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-02-2007, 06:13 PM
Member
 
Join Date: Jun 2007
Posts: 11
creativehacker is on a distinguished road
Its the rule of INHERITANCE
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-02-2007, 06:58 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Quote:
Its the rule of INHERITANCE
So is this valid for C++ too? Explain it in detail..
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-02-2007, 09:10 PM
Member
 
Join Date: Jun 2007
Posts: 11
creativehacker is on a distinguished road
Unless it is declared as virtual, the binding is done at compile time in C++. This is not the case in Java.

However, static members are bind at compile time in Java.
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
[SOLVED] Confused by the textField jamesfrize New To Java 4 03-28-2008 06:30 PM
a lot confused vineethraj New To Java 4 01-18-2008 01:36 AM
what does it mean:confused: sivasayanth New To Java 2 01-12-2008 05:52 AM
Java confused over Generics? Bibendum New To Java 3 12-26-2007 07:23 AM
bit confused on this question money123 New To Java 2 07-27-2007 01:14 PM


All times are GMT +3. The time now is 06:28 PM.


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