aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can somebody explain this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "can somebody explain this?" Watch "can somebody explain this?" New topic
Author

can somebody explain this?

Bin Wang
Ranch Hand

Joined: May 01, 2001
Posts: 82
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(); //line 18, compile error
System.out.println(t.s);
}
public void write() {
System.out.println(Thread.interrupted());
}
}
This program doesn't compile because of line 18. Why? If line 18 is deleted, the program compile and run, but why t.s print "Java" instead of "Javascript"?
Please help.
Maitham H
Greenhorn

Joined: Jan 07, 2001
Posts: 29
You are having problem with line 18 because the class Test , try to access a private method in class process , even if Test extends Process.
Check out the definition of the private modifier, it will state that if a method or a vairable is declared private it can only be accessed by the class that declares it.
hope that helps


A Software life cycle can be greater than its Developer's
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The thing to watch out for is that variable t is a Process "type" not a Test "type".
The private method write() is NOT inherited because it is private, so there is not overriding happening. The write() method in Test just hides the write() method in Process. When you used t.write() you were trying to access the write method of the variable type (Process) from a different class.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Zc Liao
Greenhorn

Joined: May 17, 2001
Posts: 2
Hi Cindy:
I change the code as following:
class Process {
String s="Java";
Process() {
write();
}
void write() { //delete private
System.out.println("in process write"+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("in test write "+ Thread.interrupted());
}
}
The result is
"in test write false
in test write false
java"
But you said
"When you used t.write() you were trying to access the write method of the variable type (Process) from a different class."
Why the result is not
"in process write false"
please clear my doubt.
sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234

class Process {
String s="Java";
Process() { // when the constructor is called the write method
write();
// in process is supposed to be called but write() is overridden in the subclass and hence the overridden method is called.
}
void write() { //delete private
System.out.println("in process write"+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("in test write "+ Thread.interrupted());
}
}
The result is
"in test write false
in test write false
java"
But you said
"When you used t.write() you were trying to access the write method of the variable type (Process) from a different class."
Why the result is not
"in process write false"
please clear my doubt.
[/B]

sona<br />SCJP
sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
how will we access the write method of the process class
can someone write the code
please
reehan ishaque
Ranch Hand

Joined: Mar 25, 2001
Posts: 54
if u r having late binding n u want to call super class's private method, i think theres no way. although without binding u can call private methods from with in super class's constructer by just calling base class's constructer. overriding will still be there.
here is the previous code with little modification.

class Process {
String s="Java";
Process() { // when the constructor is called the write method
write();
// in process is supposed to be called but write() is overridden in the subclass and hence the overridden method is called.
}
private void write() {
System.out.println("in process write"+Thread.activeCount());
}
}
class Test extends Process {
String s="JavaScript";
public static void main(String[] args) {
new Test();
}
public void write() {
System.out.println("in test write "+ Thread.interrupted());
}
}
correct me if im wrong
Bin Wang
Ranch Hand

Joined: May 01, 2001
Posts: 82
Hi, I understand the method part of the program, but still don't understand why t.s is "Java" not "JavaScript"?
If Test t=new Test(); t.s is "JavaScript".
If Process t=new Test(); t.s is "Java", why? I create an instance of Test class here although I use Process as its type, right?
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Instance variables are set at compile time, unlike instance methods. When a subclass hides the parent's variable, the variable referenced by an Object will depend on the reference type of the Object.
Process t = new Test(); \\ t.s will reference the s of the Process class
Test t = new Test(); \\t.s will reference the s of the Test class
This is the opposite of the overridden instance method write() (assuming that the Process method has the private keyword removed), in which the write() method defined in Test is the method used by t.write() in both of the examples above (you'd have to declare t as a Process in order to use the write() method defined by the Process class)
Bin Wang
Ranch Hand

Joined: May 01, 2001
Posts: 82
Hi Scott,
You explain it clearly. Thanks a lot.
Ranjan Chaudhuri
Ranch Hand

Joined: May 22, 2001
Posts: 33
Originally posted by sona nagee:
how will we access the write method of the process class
can someone write the code
please

I guess the write() method of the super class can be accessed by using the "super" reference; however, it can't be done from the main() method because that is static... however it could be done in a tortured way as below (not sure if this is what you were asking anyway):
-------------------
class Process {
String s="Java";
Process() {
write();
}
void write() { //delete private
System.out.println("in process write"+Thread.activeCount());
}
}
class Test extends Process {
String s="JavaScript";
public static void main(String[] args) {
Test t = new Test();
t.notstatic(t);
}
public void notstatic(Test t) {
t.super.write();
System.out.println(t.s);
}
public void write() {
System.out.println("in test write "+ Thread.interrupted());
}
}

sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
we can say t.super.write()
it gives an error
please someone explain how to call the write method in the superclass in this case
shailesh sonavadekar
Ranch Hand

Joined: Oct 12, 2000
Posts: 1874
Maitham H , Welcome to javaranch.
PROPER NAMES ARE NOW REQUIRED
Please look carefully at official naming policy at javaranch & reregister yourself with proper last name. Please adhere to official naimg policy & help maintain the decorum of the forum.
Waiting for your posts with proper last name. Once you have reregister , please let us know about that & then your previous account will be disabled.
Regards.

Your Friendly Bartender
Shailesh.
[This message has been edited by shailesh sonavadekar (edited June 02, 2001).]
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Originally posted by sona nagee:
how will we access the write method of the process class
can someone write the code
please
--------------------------------
Hi,
I guess we can't access the private method of the superclass from subclass. If we want to access the private method ,we have to instantiate the super class.
pls correct me if I am wrong.
Vanitha.

[This message has been edited by Vanitha Sugumaran (edited June 02, 2001).]
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
I modified the code and executed the following
class Process {
String s="Java";
Process() {
write();
}
private void write() {
System.out.println("inside process " + Thread.activeCount());
}
}
class javar1 extends Process {
String s="JavaScript";
public void write() {
System.out.println(Thread.interrupted());
}
public static void main(String[] args) {
Process t;
javar1 j = new javar1();
j.write();
t = j;
t.write();//now this line gives compile error
System.out.println(t.s);
}
}
When we call write() from j it executes but when we assign j to t and call write() it give compile error that an attempt to access private function was made. My question is - since the methods are called through a reference and it depends on the actual object which method is called - why is this happening? My guess is that this would be it happens because of late binding. But then compiler sees it in a different way than it actually is executed.
thanks all


AM<BR> <A HREF="mailto:anshulmohan@rediffmail.com" rel="nofollow">anshulmohan@rediffmail.com</A>
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Anshul, the reason you are getting the error is because the call to t.write() is occurring within the javar1 class, which has no access to the private write() method of t (an instance of the Process class).
In order to access the private write() method, you'll need to add a new (non-private) method within the Process class which itself calls the write() method. Then you can call the new method from your javar1 class.


[This message has been edited by Scott Appleton (edited June 04, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can somebody explain this?