• 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

Jtips Mock 2

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javac Test.java
output

HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code will give error at line t.write(); because write() is a private method of class Process.
- Manish
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it
thanx pal
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jose and Manish.
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx
Manish
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
reply
    Bookmark Topic Watch Topic
  • New Topic