• 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

my experiment ...

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class New {
String data = "super" ;
void method() {
System.out.println("method of super");
System.out.println(data);
}
}
class SubNew extends New {
String data = "sub" ;
void method() {
System.out.println("method of sub");
System.out.println(data);
}
public static void main(String manal[]) {
New nn = new New();
SubNew ss = new SubNew();
New ns = new SubNew();
System.out.println("************** nn ***********");
nn.method();
System.out.println("************** ss ***********");
ss.method();
System.out.println("************** ns ***********");
ns.method();
}
}
The outpout of this prog. is ..
************** nn ***********
method of super
super
************** ss ***********
method of sub
sub
************** ns ***********
method of sub
sub
Can anyone please Explain me why after calling ns.method(); it is using data of SubNew when we r using a
reference of New class ...
Thanks ...
Manal
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you read the JavaRanch tutorial:
http://www.javaranch.com/campfire/StoryPoly.html
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Daryl said it. Please do understand object and dynamic binding.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic