• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

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