Reputation: 249
Can an abstract class have a final method in Java?
Upvotes: 11
Views: 57587
Reputation: 1
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
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 163
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
Upvotes: 7
Reputation: 9
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.
Upvotes: -1
Reputation: 405715
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.
Upvotes: 43
Reputation: 51311
Yes, it can. But the final method cannot be abstract itself (other non-final methods in the same class can be).
Upvotes: 8
Reputation: 114757
Yes.
Hint: just fire up your favorite IDE (eclipse, netbeans, etc) and try it out. It will complain if it does not work.
Upvotes: 2
Reputation: 22775
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.
Upvotes: 0
Reputation: 10051
Of course, it means you can subclass it, but you cannot override that particular method.
Upvotes: 1
Reputation: 12770
Yes, those methods cannot be overriden in subclasses. An example of that is the template method pattern...
Upvotes: 1