Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Ques 14 from parikosh's site 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 "Ques 14 from parikosh Watch "Ques 14 from parikosh New topic
Author

Ques 14 from parikosh's site

Chaitali Deshpande
Greenhorn

Joined: Jan 17, 2001
Posts: 26
how does the following program compile?
how does method f() specified as final in parent class
gets overriden in the child class.

class WF {
private final void f() {
System.out.println("WF.f()");
}
private void g() {
System.out.println("WF.g()");
}
}
class OP extends WF {
public final void f() {
System.out.println("OP.f()");
}
public void g() {
System.out.println("OP.g()");
}
}
public class Overriding {
public static void main(String[] args) {
OP op = new OP();
op.f();
op.g();
WF wf = op;
((OP)wf).f();
((OP)wf).g();
}
}
Suresh Selvaraj
Ranch Hand

Joined: Nov 14, 2000
Posts: 104
Hi,
Any method declared with private accessibility can be overridden
by any method with "default - i.e, no access modifier ",
"protected" and "public" accessibility.
The order in which a method can be overridden is :
private --> friendly --> protected --> public
- Suresh Selvaraj
[ www.jtips.net ]


Suresh Selvaraj, (author of JQuiz)<br />SCJP2<br /><a href="http://www.decontconsulting.com" target="_blank" rel="nofollow">www.decontconsulting.com</a>
David Roberts
Ranch Hand

Joined: Nov 03, 2000
Posts: 142
The answer to your question is: It's not overriden. Because it's private and the sub class has no visibility to a private method of a super class, a new method is created. If it were a protected final method, then a compile error would happen.
- David


David Roberts - SCJP2,MCP
Chaitali Deshpande
Greenhorn

Joined: Jan 17, 2001
Posts: 26
Thanks .My problem is solved.
 
jQuery in Action, 2nd edition
 
subject: Ques 14 from parikosh's site