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 final method 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 "final method" Watch "final method" New topic
Author

final method

Hasitha Randika
Ranch Hand

Joined: Sep 04, 2007
Posts: 50
class A{
private final void method1(){
System.out.println("A");
}
class B extends A{
public void method2(){
System.out.println("b");
}
}
class C{
public static void main(String ar[]){
new(B).method2();
}
}

here the answer is "b".can we override the final method in class A?
Abhishek khare
Greenhorn

Joined: Aug 08, 2007
Posts: 25
See the code example below :

public class Gen {

public static void main(String ar[]){
new BGen().method2();
new BGen().method1();
}

}
class AGen{
private final void method1(){
System.out.println("A");
}
}
class BGen extends AGen{
public void method2(){
System.out.println("b");
}
public final void method1(){
System.out.println("A");
}
}

You can see that we have overriden the method1 but thats because it marked private in Class AGen.
if you declare it Public Final in Class AGen then you wont be able to override it in class BGen.


"Things come to those who wait, but only the things left by those who hustle."
yo loo
Greenhorn

Joined: Jul 16, 2007
Posts: 3
final method can't be override ,if the method in super class is public ,it will throw a compile mistake if you try to override the method ,because it 's "final"
if the modifier of the method is "private" so we could think the method is be hidding in the super class
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final method
 
Similar Threads
Inheritance Problem
Hiding of instance variables. Hard question?
Problem -- Virtual Method Invocation
final methods
An example of abstraction in java