Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java programming

 
Aruna
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wong KK
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At compile time, object reference type will be checked rather than the actual type for the object.
 
Vegad Arvind
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
}
}

Hope this will help.
Avi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic