Can an abstract class have a final method?

旧时模样 提交于 2019-12-03 17:14:05

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.

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

Yes.

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

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

Yes.

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

Yes it can ... need more characters

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

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.

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.

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.

muthyala raju

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!