Core java fundamental help

P

pras.vaidya

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);
}}
 
C

Chris Uppal

pras.vaidya said:
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
 
K

Kevin

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
 
K

kittu

pras.vaidya said:
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);
}}

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
 
K

krishna

pras.vaidya said:
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);
}}

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top