Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final method

 
Hasitha Randika
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
yo loo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic