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 Certification Study Guide by Simon Roberts Philip Heller Micheal Ernst Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Certification Study Guide by Simon Roberts Philip Heller Micheal Ernst" Watch "Certification Study Guide by Simon Roberts Philip Heller Micheal Ernst" New topic
Author

Certification Study Guide by Simon Roberts Philip Heller Micheal Ernst

Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Folks:
If any of you had used this book.
I have an issue to discuss
On page 79 .. Discussion about private access modifier..
The authoer quotes
"The rules for overriding can be summarized as follows:
A private method may be overridden by a private, friendly,
protected or public method."

Correct me if i am wrong. My understanding was overriding prohibited to the methods with the following modifiers?
1. private
2. final
3. static.
Of course i did some sanity check and javac doesnt like it if i
try to override a private method.
If i didnt comprehend author's explanation about modifier correctly can anyone throw me some explanations about what the author trying to convey?

public class A {
static int a ;
private void foo(){
System.out.println("private foo method from A class...");
}
}
public class B extends A{
static int b ;
public void foo() {
System.out.println("output from foo() B..");
}
public static void main(String _[]) {
A a = new B();
B b = new B();
a.foo();
b.foo();
}
}
****javac complaints***********
C:\SCJP>javac B.java
B.java:11: foo() has private access in A
a.foo();
^
1 error
PS:I can make this code to compile,
if i comment it out
A a = new B();
a.foo();
But then again, it is not really overriding isn't?
Mini Pilla
Ranch Hand

Joined: Jul 15, 2001
Posts: 112
HI Raghu,
In this code foo() is overloaded.
So when complier sees this line
A a = new B();//it refers to the foo of A
which is private which it cannot access from B.
Hope I explained.
Thanx
Rajani
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
What edition of the book do you have? I have nothing like that in my copy.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Rajani,
When we say about Overloading one thing comes to my mind
Reusing the same method name.
Now when can we do this reusability?
1. In a UNRELATED CLASS, no special conditions apply and the two
methods are not considered related any way
2. In the SAME CLASS, that defines the original method, or a
SUBCLASS of that class, the method name can be reused if the
ARGUMENT LIST DIFFERS in terms of the type of AT LEAST ONE
argument.
In our case, There is no difference in argument list on both
the foo's. So how can it qualify for an OverLoading?
In this strict class scenario, aren't they Overriding methods..?

Thomas,
I dont see any edition # of some sort in this book
However here is the ISBN: 0-7821-2700-2
Publisher: SYBEX
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
That is an old expired edition. I wouldn't use that to study for the test since it does not cover the new objectives. You need this book:
http://www.amazon.com/exec/obidos/ASIN/0782128254/electricporkchop
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Thomas Paul:
[B]That is an old expired edition. I wouldn't use that to study for the test since it does not cover the new objectives. You need this book:
Ok Thomas,
I also got the same book that Ragu has. So, shouldn't we prepare from that book any more. I also have Khalid's book(recent one). Does it make any difference.
And can you clear me on this concept.
"The rules for overriding can be summarized as follows:
A private method may be overridden by a private, friendly,
protected or public method."
Now can a private method be overridden by a private, friendly, protected or public method. Please let me know.
thanks
chandra!

Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
A private method can not be overridden. That is, a private method can not participate in polymorphism. A private method of the same name in a child class will shadow the parent method.

If you run this, you will get "Parent". If you make methoda public in both parent and Child and then run it, you will get "Child". The difference is that private methods can't participate in polymorphism so Parent can't "see" that it's methoda has been overridden.
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Wow!! I didnt know that its an Earlier Edition
But i am still stumped, that how could they goof in
modifiers?
Was the private methods overriding earlier? deprecated in later versions of jdk..? (Not that i know of...) I am just trying to find out the justification of those quotes from the book...:=)
But anyways, thankx paul for your information
In that context,
Could some one recommend a good book on scjp certification?

Christophe Lee
Ranch Hand

Joined: Jul 10, 2001
Posts: 142
What does "shadow" mean?
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
Tom, I have some problem reagrding the code below. When we call a method in the parent class, that method should call the method in the same class(i.e parent class)if it has one. In this case the super class method (methodb)is called from subclass(main method) and that super class method (methodb)is calling another method(methoda) in the same class, which is also (overriden or anything u want to call it)in the subclass(methoda). If the methoda in the super class is private, then the methodb call the methoda of thye same class else if it is public thet methodb call the methoda of the subclass. Itsd really confusing, pls explain,
--Farooq

class Parent {
public void methoda() {
System.out.println("Parent");
}// end method
public void methodb() {
methoda();
}//end method
}//end class
//============================================
public class Child extends Parent
{
public void methoda() {
System.out.println("Child");
} //end method
public static void main(String[] args) {
Parent pc = new Child();
pc.methodb();
System.out.println("==============================");
Parent p = new Parent();
p.methodb();
System.out.println("==============================");
Child c = new Child();
c.methodb();
}//end method
}//end class


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The answer is one word: polymorphism.
At run time, the method that will be executed is the method from the actual object, not what you are calling the object.
Parent p = new Child();
Here I have created an object that I am calling a "Parent" but it is really a child and at runtime it will run child methods.
p.methoda();
will run the methoda() of Child if methoda() of Child can participate in polymorphism. private methods can't be overridden so they can't participate in polymorphism.
p.methoda();
will run methoda() of child if the Parent methoda() can be overridden.
As to the example. When I do this:
p.methodb();
since there is no methodb() in Child, it will run methodb() of Parent. methodb() runs methoda() so what happens? According to the JLS:
1) is there a methoda() in Parent?
2) yes --- is that method eligible to be overridden (not private)?
3) yes --- has this method been overridden in Child (which is what this object actually is)?
4) yes --- run the overrding method in Child.
Try the code yourself and you will see that is exactly how it works.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Ragu Sivaraman:
Wow!! I didnt know that its an Earlier Edition
But i am still stumped, that how could they goof in
modifiers?
I don't see the erratta for the older edition on the Sybex web site. It may have been fixed there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Certification Study Guide by Simon Roberts Philip Heller Micheal Ernst
 
Similar Threads
static method query [KB book (Topic 4.10).]
Entangled in Threads
overriding
Question about static methods in superclasses and subclasses
VarArgs