• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ques 14 from parikosh's site

 
Chaitali Deshpande
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
David Roberts
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chaitali Deshpande
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks .My problem is solved.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic