Can an abstract class have a final method?

2020-06-21 03:11发布

问题:

Can an abstract class have a final method in Java?

回答1:

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.



回答2:

Yes, it can. But the final method cannot be abstract itself (other non-final methods in the same class can be).



回答3:

Yes.

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



回答4:

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



回答5:

Yes.



回答6:

Yes, those methods cannot be overriden in subclasses. An example of that is the template method pattern...



回答7:

Yes it can ... need more characters



回答8:

Of course, it means you can subclass it, but you cannot override that particular method.



回答9:

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.



回答10:

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.



回答11:

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.



回答12:

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