• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

java programming

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At compile time, object reference type will be checked rather than the actual type for the object.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I think I'll just lie down here for a second. And ponder this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic