File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why Compiler error?..... 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 "Why Compiler error?....." Watch "Why Compiler error?....." New topic

Why Compiler error?.....

tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
This class doesn't compile.
I thought that in Line 1
Process t = new TestA(); //1
we are creating an object of type TestA which has class reference as Process.. that's causing the error.
I tried to change Line //1 as
Process t = new Process();
But still I get the same error.
Error message is
"No method matching write() found in class Process.
I haven't understood the flow of the program. Is it anything related with the access modifier private.. Can somebody explain pls..
Thanks in advance.
class Process
String s="Java";
private void write()
class TestA extends Process
String s="JavaScript";
public static void main(String[] args)
Process t = new TestA(); //1
public void write()
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
The problem is that TestA cannot access Process.write(). The access modifier private means that only Process objects can access that method. There are two ways to fix it. First, you can change Process.write to any other access modifier at all (public, protected, and default/package). Since TestA is both a subclass and in the same package, it would be allowed to access the method. The other option is to store/cast 't' as/to a TestA object, where write can be accessed by main().
Process t = new TestA(); //1
((TestA) t).write();
TestA t = new TestA(); //1
private void write() -> public/protected/nothing void write()
[This message has been edited by David Garland (edited August 12, 2001).]
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
I always thought what methods are available is determined by the actual object type. For instance,
Object object = new String("Hi!");
object.toString() will return "Hi!" and not some hashing mumble jumbles. But here it seems like it is also determined by the reference type. Can someone shed some light on as to what are determined by the actual object type and what are determined by the reference type?
Alex Sbityakov
Ranch Hand

Joined: Jul 23, 2001
Posts: 49
private methods do not particiate in inheritance, because the are invisible to the subclasses. This is precisely why, the program says that it can't find method write() in class Process - it is simply invisible to everything outside the class.
I agree. Here's the link:
subject: Why Compiler error?.....
It's not a secret anymore!