JavaRush /Java Blog /Random EN /Question and Answer: Is it possible to define abstract me...

Question and Answer: Is it possible to define abstract methods in a final class?

Published in the Random EN group
This interesting and important question was asked to one of my readers during a telephone interview for a Java developer position. Although he knew that it was impossible to make an abstract class finalin the Java language , he was confused by the wording of the question. The answer is simple: no, finalthere cannot be abstract methods in a -class. Why? Because as soon as you declare an abstract method in a Java language class, this class automatically becomes abstract, and finalyou cannot make an abstract class in the Java language. finalThis means that there cannot be abstract methods in a Java -class.
Question and Answer: Is it possible to define abstract methods in a final class?  - 1
Many Java developers get confused when answering this question because of the wording. Even if the person asked the question is familiar with the general concept, he may not realize that a class, after declaring an abstract method in it, becomes abstract, and you cannot make an abstract class finalin Java. This point separates this question from the more common and frequently asked question: is it possible in Java to declare a class to be abstract and final at the same time? Let's look at one example code to demonstrate that finalit is not possible to declare an abstract method in a -class. Let Hello.javaus have public finala class called Hello, which has an abstract method print().
public final class Hello {
  public abstract void print();
}
Type this code into the IDE, you will receive the following error message, they say Helloit must be an abstract class so that abstract methods can be declared in it.
Question and Answer: Is it possible to define abstract methods in a final class?  - 2
Question and Answer: Is it possible to define abstract methods in a final class?  - 3
The same thing will happen if you type this code in Notepad and compile it using the javac utility from the command line. According to the Java specifications, as soon as you declare an abstract method inside a class, that class becomes an abstract class, and since you cannot make an abstract class finalin Java, the compiler will generate an error. This is true for both top-level classes and nested classes in the Java language. Even if you declare an abstract method in a nested final-class, the error will be the same. Another related question is: can an abstract class in Java have static methods? Answer: yes, they can, there is no problem with declaring a static method in an abstract class, since to use a static method you do not need to create an instance of the class, they can be called simply using the class name. We can modify our example to include Helloa method in the class main(), which is static in Java, as shown below:
public abstract class Hello {
  public abstract void print();
  public static void main(String args[]) {
     // Howой-то code
  }
}
As you can see, no compilation error is thrown. This code compiles without problems, since in Java you can completely safely declare a static method in an abstract class. Brief conclusions It is impossible to declare an abstract method in a final-class. Because once you do this, the class automatically becomes abstract, according to the Java specifications. And since finala -class in Java cannot be abstract, this is unacceptable, and the compiler will prohibit you from doing this by throwing an error. But you can declare static methods both in final and in abstract classes, there are no problems with this.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION