• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

can somebody explain this?

 
Ranch Hand
Posts: 82
  • 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(); //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.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 234
  • 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() { // 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 gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how will we access the write method of the process class
can someone write the code
please
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Scott,
You explain it clearly. Thanks a lot.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic