Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

any one give be the explanation for the code above and y it prints the output as pare

 
Thangaraj Selvamani
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Parent {
String message = "parent";
void say() {
System.out.println(message);
}
}

class Child extends Parent {
String message = "child";

}

public class ScjpTest3 {
public static void main(String[] args) {
new Child().say();
}
}
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really should consider creating a more reasonable title, and placing the question in the body of the post, instead of the title....

any one give be the explanation for the code above and y it prints the output as pare


Overridding applies to methods -- not to instance variables. So, when you call the say() method, it will execute the latest version. This latest version is in the parent class, which at compile time was coded to access the instance variable of the parent.

Henry
[ October 05, 2008: Message edited by: Henry Wong ]
 
Thangaraj Selvamani
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the mistake... im new to this site...
here after i will give correct and reasonable title...
thanks...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic