• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

final method

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Did you miss me? Did you miss this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic