Forums Register Login

can somebody explain this?

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send

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]
+Pie Number of slices to send: Send
how will we access the write method of the process class
can someone write the code
please
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send
Hi Scott,
You explain it clearly. Thanks a lot.
+Pie Number of slices to send: Send
 

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());
}
}

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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).]
catch it before it slithers away! Oh wait, it's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 863 times.
Similar Threads
Jtips Mock 2
java programming
simple super & this question
Why Compiler error?.....
javaprogramming
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:00:35.