• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final methods

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can an abstract class have final methods?
 
Kumaresh Vidhyasagar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you can't have
 
Paul Clapham
Sheriff
Posts: 20990
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. Why wouldn't that be allowed?
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Even I was under the impression that they cant have. But look at the code above. it compiles fine.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Sure. Why wouldn't that be allowed?

Final methods cannot be overriden, but once they are inherited by the subclass, there is a possibility that they will be overridden. I donno..I am a bit confused...
 
promila singh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you can have final methods in abstract class
try this code
abstract class abstractclass
{
final public void display(){
System.out.println("Display");
}
abstract void show();
}
public class extendedclass extends abstractclass{
public static void main(String args[]){
array aa = new array();
aa.display();
aa.show();
}
public void show(){
System.out.println("show");
}
}
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Promila, please use code tags.

http://faq.javaranch.com/java/UseCodeTags
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, Now I figured out. That when a subclass extends an abstract class, it inherits its methods(final also). Its only when you try to override the inherited final methods, when you get a Compiler error. Should have thought about it earlier ops: .
 
promila singh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m new here
thanks abhi vijay
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhi remember, abstract class cannot have:

1) abstract final methods.
2) private abstract methods.
3) native abstract methods.
4) abstract synchronized methods.
5) strictfp native methods.

abstract class can have:

1) private native methods
2) synchronized methods with body
3) protected abstract methods
4) private static synchronized native methods.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit, To my Rescue Again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic