• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt 3

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
watch out the following code
----------------------------
class A
{String s = "AB";}
class B{ A a = new A();}
class C extends B
{
public static void main(String [] args)
{
C c = new C();
c.print();
}
void print()
{
System.out.println(a.s);//First
System.out.println(a.s);//IInd
}
}
}
-------------------------------
which one of the above is the simplest way to print s on the screen
Thanking in advance
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I don't understand your question. Explain your questions in easy way to understand others. It will helpful to get quick result.
 
Amitkumar Dhama
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry i was in hurry at that time nw i have done the editing here is the modified code
-------------------------------------
class A
{String s = "AB";}
class B{ A a = new A();}
class C extends B
{
public static void main(String [] args)
{
C c = new C();
c.print();
}System.out.println(a.s);//First
System.out.println(super.a.s);//IInd
}
}
}

void print()
{
---------------------------------------------
Nw my question is that which one is better First or IInd in order to print the value of s, we have to choose only one from them....
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a very strange question. You want to know which is simpler? Since a is accessible from within c, you could say that a.s is simpler, but an argument could be made that direct access to the super object's variable is simpler, rather than relying on inheritance. Who posed this question to you? Both options are functionally the same in this example.

Also, I don't recommend writing your posts in a hurry, as there are errors in both your posts that make it difficult to answer your question.
 
Amitkumar Dhama
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jhon i got it from one of the mock exam, actually i am mailing frm syber cafe, and there was virus on that sys, i think that might be a cause 4 error.
In that mock exam there were three options third one was incorrect, and i had to select only one frm them, frm these two i was getting output, i couldn't opt for one thats why i asked it which one will be better..

Anyways thanks 4 ur great respo....
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic