File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes any one give be the explanation for the code above and y it prints the output as pare Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "any one give be the explanation for the code above and y it prints the output as pare" Watch "any one give be the explanation for the code above and y it prints the output as pare" New topic
Author

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

Thangaraj Selvamani
Ranch Hand

Joined: Sep 20, 2008
Posts: 61
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
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Thangaraj Selvamani
Ranch Hand

Joined: Sep 20, 2008
Posts: 61
Sorry for the mistake... im new to this site...
here after i will give correct and reasonable title...
thanks...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: any one give be the explanation for the code above and y it prints the output as pare