File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Jtips Mock 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Jtips Mock 2" Watch "Jtips Mock 2" New topic
Author

Jtips Mock 2

Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Hi,
This question is from JTips Mock 2 Q7
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());
}
}

Answer given is :Compile time error
Could anyone please explain why.
Thanx
Neha

nss
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
javac Test.java
output

HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

The code will give error at line t.write(); because write() is a private method of class Process.
- Manish
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
hey,
i am getting confused.
t.write(); is calling write method of test
Write method in process class is private and so it is not seen in subclass.
Then according to me write method of test class should be called ,please tell me where am i wrong.
thanx
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

Originally posted by Neha Sawant:
hey,
i am getting confused.
t.write(); is calling write method of test
Write method in process class is private and so it is not seen in subclass.
Then according to me write method of test class should be called ,please tell me where am i wrong.
thanx

At compile time when methods are bound, the compiler looks for the declared type of the variable, (which is Process) and finds if the method exists in that class and if it's accessible. Only if such method is overriden in subclass, then Java runtime will invoke the subclass implementation of this method at runtime. Here, of course the private method is neither inherited nor overriden.
HTH,
- Manish
[This message has been edited by Manish Hatwalne (edited November 01, 2001).]
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Manish,
So u mean to say the compiler will first check if write() is in
the Process class (yes it is)
But since it is not accessible in Subclass so it is giving an error.
Am i right?
But if the method was protected or with no access modifier then there would be no error and write() of subclass will be called.
I hope my conclusion is correct.
Correct me if i am wrong
thanx
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

Originally posted by Neha Sawant:
So u mean to say the compiler will first check if write() is in
the Process class (yes it is) But since it is not accessible in Subclass so it is giving an error. Am i right?

No! The method exists in class Process, but it is not accessible outside the class. The compiler gives error at tis point because it "can not see" any method called write() in class process, so statement t.write() results in a compiler error. For compiler, t at the time of compiling is Process.

But if the method was protected or with no access modifier then there would be no error and write() of subclass will be called.

Yes, it will be applicable for public also. For package/default (w/o modifier) of course, the subclass must exist in the same package.

===============================================================
This is slighly unrelated but it will help, try this code.


Hope it is clear now,
- Manish
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
got it
thanx pal
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
manish sorry to bother u again
If there is no write method in Process at all.
For example if it is like thatclass Process
{
String s="Java";
Process()
{

}

}
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());
}
}
Then will the program execute without error.
Acording to me ans should be
false
Java
Correct if i am wrong
thanx
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

Then will the program execute without error.

The program won't even compile.
I think you are missing the point here. The method must exist and should be accessible in the base class Procees whose reference you are using to invoke the method. Remember that all "new" operations take place at runtime, by creating an object on heap and its reference is assigned to the base class reference variable. At compile time, the compiler is actually blind about the actual type of object that is being created (not really, it performs some checks, but so to speak); so the compilation will fail for any method invokation (or any member reference for that matter) that does not fulfil folowing criteria -
(1) The member must exist in the base class whose reference you are using.
(2) It must be accessible in the base class.
HTH,
- Manish
[This message has been edited by Manish Hatwalne (edited November 01, 2001).]
Rashmi Hosalli
Ranch Hand

Joined: Jun 25, 2001
Posts: 50
Hi Manish,
Thanks for the explanation.I have one doubt and I request you to please clarify the same...
In the sample code you gave:
class B
{
public static void foo()
{
System.out.println("Inside Base foo"); }
}
public class D extends B
{
public static void foo( )
{
System.out.println("Inside Derived foo");
}
public static void main(String args[])
{
B obj = new D();
obj.foo(); //prints Inside Base foo }
}
if the static is removed from the foo method, it prints "Inside Derived foo".So,what role does static play in such situations.Can such methods be overridden?
Regards,
Rashmi
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hi Rashmi
static methods are inherited but they are bound statically. This means based on the declared type of the reference used to invoke them


SCJP2. Please Indent your code using UBB Code
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Manish,
Thanx for your explanation, now i think i have a clear picture about it.
Can you suggest some good site where i will be able to learn more about this.
regards
Neha
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

Hi Rashmi,
Jose already answered question, the code dealing with static method is an example of "hiding", and not overriding. Here is from JLS about hiding -
8.4.6.2 Hiding (by Class Methods)
"If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method. "
Neha, I think the best source for this is "JLS", you can download it for free from Sun's website and use it offline. Of course, JLS is not easy reading, it takes time to go through it,
but once you get used to its language and way of explaining things (I think "stating" would be a more appropriate word, JLS doesn't explain much; there's Jose Botella to do that. Thank god!! ), you'll find it most useful. If you like to refer to books more, use Java Programming Language 3rd ed by James Gosling, that's the best.
HTH,
- Manish
Rashmi Hosalli
Ranch Hand

Joined: Jun 25, 2001
Posts: 50
Thank you Jose and Manish.
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
thanx
Manish
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Sumit...
Your removing "static" from the methods foo() is OK..but the "static" in the main must remain.
Add static to the main() method in your code...it'll do just fine.

Hope this helps
Shyam
Hey! I saw a post here by Sumit sometime back. It seems that he has deleted his post.

[This message has been edited by Shyamsundar Gururaj (edited November 04, 2001).]
Sumit Gangrade
Greenhorn

Joined: Nov 02, 2001
Posts: 2
Hi,
Thanks shyam .
After posting I noticed that I had forgotten the static in the main. So I came back and deleted it.
sorry for the inconv.
Rgd's,
Sumit

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jtips Mock 2
 
Similar Threads
Jtips mock exam
JTips Question
jtips
jtips
jtips