• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Certification Study Guide by Simon Roberts Philip Heller Micheal Ernst

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What edition of the book do you have? I have nothing like that in my copy.
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does "shadow" mean?
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Goodbye moon men. Hello tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic