class Process { String s="Java"; Process() { write(); }
private void write() { System.out.println(Thread.activeCount()); } } class Test extends Process { String s="JavaScript"; public static void main(String[] args) { Process t = new Test(); t.write(); System.out.println(t.s); } public void write() { System.out.println(Thread.interrupted()); } } Ans : Compile-time error occurs Question : Why compile time error occurs? Pls explain.
A. Aruna
kapil apshankar
Ranch Hand
Joined: Dec 17, 2000
Posts: 66
posted
0
Hi, Object 't' refers to the base class Process. Therefore t.write() calls the version of write()defined in Process. As it has been declared private, it cannot be directly called. Hence the compile time error. Please correct me if I am wrong. Cheers Kapil
Hope this helps. Correct me if I am wrong.<p>Cheers <img src="smile.gif" border="0"> ,<br />Kapil
Wong KK
Ranch Hand
Joined: Jan 15, 2001
Posts: 52
posted
0
At compile time, object reference type will be checked rather than the actual type for the object.
Vegad Arvind
Ranch Hand
Joined: Jan 10, 2001
Posts: 42
posted
0
Hi,
There r some problems in ur code. 1. u used class name - Process //class name Process clashes with imported class java.lang.Process 2 Private method in Process class Private method can not be override. 3. i make some changes in ur code see and test the result.
Result is : False False Java
File Test.java class Process1 { String s="Java"; Process1() { write(); } //private void write() { //i comment public void write() { System.out.println(Thread.activeCount()); } } class Test extends Process1 { String s="JavaScript"; public static void main(String[] args) { Process1 t = new Test(); t.write(); // search in process1 class System.out.println(t.s); } public void write() { //public since super class has pulic System.out.println(Thread.interrupted()); } }