Can an abstract class have a final method in Java?
12 Answers
Sure. Take a look at the Template method pattern for an example.
abstract class Game
{
protected int playersCount;
abstract void initializeGame();
abstract void makePlay(int player);
abstract boolean endOfGame();
abstract void printWinner();
/* A template method : */
final void playOneGame(int playersCount) {
this.playersCount = playersCount;
initializeGame();
int j = 0;
while (!endOfGame()) {
makePlay(j);
j = (j + 1) % playersCount;
}
printWinner();
}
}
Classes that extend Game
would still need to implement all abstract methods, but they'd be unable to extend playOneGame
because it is declared final.
An abstract class can also have methods that are neither abstract nor final, just regular methods. These methods must be implemented in the abstract class, but it's up to the implementer to decide whether extending classes need to override them or not.

- 398,270
- 210
- 566
- 880
-
excellent! Upvote for bringing up the template method pattern – Chris Dale Aug 24 '09 at 21:37
-
But wats the use of it – Sawyer Aug 12 '16 at 12:19
-
@Bill the Lizard: excellent answer! but I have one question: what if the playOneGame() method was a normal one (not final). it was the same thing? what advantage to use final here instead of not using any keyword? Thanks – Jad Chahine May 05 '17 at 12:13
-
@BilltheLizard: Thanks but I do not found what are the advantages of using final keyword over not using any keyword. – Jad Chahine May 05 '17 at 18:05
Yes, it can. But the final method cannot be abstract itself (other non-final methods in the same class can be).

- 50,487
- 48
- 148
- 202
Yes, there may be "final" methods in "abstract" class. But, any "abstract" method in the class can't be declared final. It will give "illegal combination of modifiers: abstract and final" error.
public abstract final void show();
illegal combination of modifiers: abstract and final
Here is the working example of the implementation.
abstract class Sian //ABSTRACT CLASS
{
public final void show() // FINAL METHOD
{
System.out.println("Yes");
}
public void display()
{
System.out.println("Overriding");
}
public abstract void success();
}
class Ideone extends Sian //INHERTING ABSTRACT CLASS
{
public void display()
{
System.out.println("Overridden");
}
public void success() //OVERRIDING THE ABSTRACT METHOD
{
System.out.println("Success overriding");
}
public static void main (String[] args) throws java.lang.Exception
{
Ideone id = new Ideone(); //OBJECT OF SUBCLASS
id.show(); //CALLING FINAL METHOD
id.display(); //OVERRIDDEN METHODS
id.success();
}
}
OUTPUT:-
Yes
Overridden
Success overriding
Here is the ideone link:- http://ideone.com/G1UBR5

- 153
- 1
- 10
Yes.
Hint: just fire up your favorite IDE (eclipse, netbeans, etc) and try it out. It will complain if it does not work.

- 113,398
- 19
- 180
- 268
-
14-1. The point of SO is not to tell people "try it yourself". It is a waste of effort and time if everyone who does an Internet search for this question has to launch an IDE and try it for themselves when someone can provide a complete and comprehensive answer to the question: http://stackoverflow.com/questions/1299398/1299434#1299434 – Grant Wagner Aug 19 '09 at 15:58
-
1Yes, your right and I accept the downvote, but this is keyur's third trivial question about abstract classes in row so I made an exemption. – Andreas Dolk Aug 19 '09 at 16:20
-
@Andreas_D: I missed that keyur had asked numerous questions on the same subject, and I had seen enough "why don't you try it for yourself" answers and comments this morning that I reacted a little too quickly with the downvote. I've removed it (does that mean you get your rep back?) since your answer isn't actually incorrect. – Grant Wagner Aug 19 '09 at 16:46
-
@Andreas_D: ones who look for **different aspects, reasons and explanations on using final methods in abstract classes** e.g. via Google don't really care about keyur's other questions (neither do I), so giving an unnecessarily sarcastic answer just to "punish" the OP (referring to other stupid questions) is not a good point. These "try it yourself"-answers do not help at all. Also think about people who just want to know the answer (quickly) or do not have the opportunity to try that at that very moment (sitting on the bus, using a cell phone). BTW this question is not _"trivial"_. -1, sorry. – Sk8erPeter Jun 16 '13 at 23:43
-
@Sk8erPeter - did you have a look at the date of that question? It's four years old, keyur has gone (grey) and would have been closed immediately if it appeared these days :) Have a look at my comment to the first downvote, that explains it all for this question ;) – Andreas Dolk Jun 19 '13 at 08:25
-
@Andreas_D: yepp,I saw the date of the question - why is it important? Java abstract classes can still have final methods... ;) (It still can be explained what it can be used for.) And yes, I saw what you wrote as an "explanation", this is why I replied that noone cares how stupid _other_ questions the OP possibly asked (if he did, he gets downvotes in the other question's topic, and what? Why does it matter in _this_ topic?), and that I don't think this question is _"**trivial**"_ (I quoted your word, so you could find out that I read your explanation, which doesn't make it a better answer). – Sk8erPeter Jun 19 '13 at 23:30
-
four years ago, if you'd had followed that thread, you'd anwered different. There was a time at SO where this was quite usual and I won't delete or change that answer only because things got better on SO. As I said, question would have been downvoted to -6 and closed within 10 minutes these days. – Andreas Dolk Jun 20 '13 at 05:56
Yes.

- 81,643
- 20
- 123
- 127
-
1Its good to become lazy and give answer in yes and no only,, when user itself is also too lazy. I think your answer deserves for one like from my side. – devsda Sep 16 '12 at 14:41
Yes, those methods cannot be overriden in subclasses. An example of that is the template method pattern...

- 12,614
- 4
- 38
- 46
-
You should give more explanation. Template method does not imply final, although it certainly makes life simpler. – kdgregory Aug 19 '09 at 11:46
-
Ok let say final is strongly recommended :) I consider it as an error not to use final for the template method... – pgras Aug 19 '09 at 12:07
Of course, it means you can subclass it, but you cannot override that particular method.

- 9,371
- 4
- 33
- 42
In Abstract Class methods may be defined or not. If we extend the abstract class then only it has meaning, so what ever methods we declare or defined in Abstract call it will over ride in subclass. So we can declare a method as final in Abstract class, and it will be over ridden in subclass.
-
3I see what you're trying to say here, but please edit your answer as it's VERY unclear now. – szegedi Oct 04 '12 at 16:31
Yes. The abstract
modifier makes it possible to omit some of the implementation of a class (i.e. have some abstract
methods) but does not impose any restrictions on you.

- 22,055
- 12
- 44
- 46
Suppose I want to designed class which has some implementation but I do not want others(sub classes) to implement it but other methods, then in that case we need a final implemented method and obvious choice abstract class.
Yes, We can write the final method with implementation.
public abstract class AbstractWithfinalMethod {
public static final boolean m1() {
System.out.println(" M1 method executed");
return true;
}
public static void main(String[] args) {
System.out.println(m1());
}
}
output: M1 method executed true

- 1,296
- 1
- 13
- 29