Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Core java fundamental help

Thread view: 
pras.vaidya - 31 Aug 2006 08:35 GMT
Hello all , I am a newbie in java .Learning core java concepts and i
got confused with this piece of code . The output of code is "Extends
Super" . First time it prints Extends because of overriding but why it
prints " super" later.

Thanks in advance.

Code goes as follow

class SuperShow{
public String str ="Super";
public void show(){
System.out.println(str);
}}
class ExtendsShow extends SuperShow{
 public String str = "Extend";
 public void show(){
 System.out.println(str);}
 public static void main(String[]args){
 ExtendsShow ext = new ExtendsShow();
 SuperShow sup = ext;
 sup.show();
 //ext.show();
 System.out.println(sup.str);
 //System.out.println( ext.str);
 }}
Chris Uppal - 31 Aug 2006 10:49 GMT
> class SuperShow{
>  public String str ="Super";

That defines a field in SuperShow called "str".

>  class ExtendsShow extends SuperShow{
>   public String str = "Extend";

That defines /another/ field, in ExtendsShow, which is also called "str".  So
now you have two fields with the same name (fields don't override in the way
that methods do).

Now, in:

>   ExtendsShow ext = new ExtendsShow();
>   SuperShow sup = ext;
>   System.out.println(sup.str);

when your code refers to the "str" field of an object only known to be of type
SuperShow, Java understands that to be a reference to the first field.  If you
have written:
  System.out.println(ext.str);
then Java would have taken it to be a reference to the second field.  Even
though "sup" and "ext" refer to the same object, it has two separate fields
with the same name (but different values), so it makes a difference which one
the code refers to.

In general it's not a good to give a subclass a field with the same name as a
(visible) field in a superclass -- precisely because it is likely to confuse
you ;-)

   -- chris
Kevin - 31 Aug 2006 11:00 GMT
well i am not quite sure, but as far as i know this is quite simple.
if you create a class with "extends" then you override the methods, but
not the variables.

if you call "sup.show()", you call a method and so you use the "new"
method of ExtendsShow.

Later you print "sup.str". This is no mehtod and that's why you get the
String from the orignial class.

i hope that helps you..

cheers,
kevin

pras.vaidya schrieb:

> Hello all , I am a newbie in java .Learning core java concepts and i
> got confused with this piece of code . The output of code is "Extends
[quoted text clipped - 22 lines]
>   //System.out.println( ext.str);
>   }}
Kevin - 31 Aug 2006 11:03 GMT
oh... it took me too long... someone was faster than me :)
kittu - 31 Aug 2006 11:52 GMT
> Hello all , I am a newbie in java .Learning core java concepts and i
> got confused with this piece of code . The output of code is "Extends
[quoted text clipped - 22 lines]
>   //System.out.println( ext.str);
>   }}

            Hai friend,  when you are calling sup.show() it displays
"Extend",because of overriding.ok you know it.your question is   later
why it displays "super"?
                   Because you r  printing  (sup.str). so it displays
super class String i.e it displays
  Super. when ever we call super class vars,constructors,methods by
using super class reference variable it displays super class  members
only  except if the sub class method overrides super class method it
executes  subclass method only.if i am wrong please reply
the solution..byeeeeeeeee
krishna - 31 Aug 2006 12:03 GMT
> Hello all , I am a newbie in java .Learning core java concepts and i
> got confused with this piece of code . The output of code is "Extends
[quoted text clipped - 22 lines]
>   //System.out.println( ext.str);
>   }}

            Hai friend,  when you are calling sup.show() it displays
"Extend",because of overriding.ok you know it.your question is   later
why it displays "super"?
                   Because you r  printing  (sup.str). so it displays
super class String i.e it displays
  Super. when ever we call super class vars,constructors,methods by
using super class reference variable it displays super class  members
only  except if the sub class method overrides super class method it
executes  subclass method only.if i am wrong please reply
the solution..byeeeeeeeee


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.