Is It Possible To Convey An Abstract Method Inwards A End Class?

Advertisement

Masukkan script iklan 970x90px

Is It Possible To Convey An Abstract Method Inwards A End Class?

Senin, 01 Maret 2021

This is 1 of the interesting nub Java questions which was asked to 1 of my readers lately during a telephonic interview for Java developer labor interview. Even though he knows that y'all cannot brand an abstract degree finally inwards Java, he got confused yesteryear the wording of the methods. The respond is simple, No, it's non possible to accept an abstract method inwards a finally degree inwards Java. Why? because every bit presently every bit y'all declare an abstract method inwards a Java class, the degree automatically becomes an abstract degree in addition to y'all cannot brand an abstract degree final inwards Java every bit discussed before, thus it's non possible to accept an abstract method inwards a finally degree inwards Java.

Many Java programmers acquire confused inwards this enquiry but because of the wording of the question, every bit I said, fifty-fifty the reader who was asked this enquiry was familiar amongst the concept, he didn't realize that every bit presently every bit y'all declare an abstract method inwards a class, it volition acquire an abstract degree in addition to it's non possible to brand an abstract degree finally inwards Java.

This is the missing slice of information or y'all telephone telephone it a play tricks which separates it from the to a greater extent than pop in addition to often asked questions, can y'all declare a degree both abstract in addition to finally inwards Java?

Let's run into 1 code illustration to test this betoken that y'all cannot declare an abstract method inwards a finally class. We accept a populace finally degree called Hello which has an abstract method called print() inwards Hello.java file.

public final class Hello {    public abstract print();  }

As presently every bit y'all type that inwards your Eclipse IDE, y'all volition have next error:
The type Hello must last an abstract degree to define abstract methods



Here is roughly other screenshot which shows that y'all cannot accept an abstract method inwards a finally degree inwards Java:

 This is 1 of the interesting nub Java questions which was asked to 1 of my readers r Is it possible to accept an abstract method inwards a finally class?


Same is truthful when y'all write downwardly this code inwards notepad in addition to compile it using javac from the command-line window. As per Java specification, in 1 lawsuit y'all declare an abstract method within a class, it automatically becomes an abstract degree in addition to since y'all cannot brand an abstract degree finally inwards Java, the compiler volition throw an error.

This is truthful for both move yesteryear flat or nested degree inwards Java. Even if declare an abstract method on a nested finally class y'all volition have the same error.

Another follow-up enquiry on this topic is can an abstract degree accept static methods inwards Java? The respond is yes, in that place is no work amongst declaring a static method within an abstract degree inwards Java because y'all don't demand to instantiate a degree to piece of work the static method, y'all tin but telephone telephone them yesteryear using the degree name.


We tin alter our code illustration to include the main() method inwards the Hello class, which is a static method inwards Java every bit shown below:

public abstract class Hello {    public abstract void print();    public static void main(String args[]) {      .. roughly code hither   }  }

You tin run into that in that place is no compile fourth dimension error. The code compiles fine, thus it is perfectly ok to declare a static method within an abstract class inwards Java.


That's all close whether it's possible to accept an abstract method inwards a finally degree or not? As I said it's non possible because in 1 lawsuit y'all create an abstract method within a class, every bit per Java specification the degree automatically becomes an abstract class. Since y'all cannot brand a finally degree abstract inwards Java, this becomes illegal in addition to compiler prohibits y'all from doing that yesteryear throwing an error. But, yes, y'all tin declare static methods on both finally degree every bit good every bit abstract classes, in that place is no work amongst that. If y'all are learning Java, I advise y'all farther read Core Java for Impatient to acquire yourself familiar amongst nub concepts similar abstract in addition to finally class. It too covers Java 8.


Other related Java Interview Questions for practice
  1. Can abstract degree accept a constructor inwards Java? (answer)
  2. Can y'all override a static method inwards Java? (answer)
  3. Can y'all overload a static method inwards Java? (answer)
  4. Can y'all run a computer programme without main() method inwards Java? (answer)
  5. Can y'all override a mortal method inwards Java? (answer)
  6. Can y'all overload in addition to override the main() method inwards Java? (answer)
  7. Can y'all brand array volatile inwards Java? (answer)
  8. Can y'all declare a degree static inwards Java? (answer)

Further Reading
Java Fundamentals, Part 1 in addition to ii
Java Programming Interview Exposed
Cracking the code interview - 189 qustions in addition to solutions

Thanks for reading this article, if y'all similar this nub Java interview enquiry in addition to my explanation in addition to so delight percentage amongst your friends in addition to colleagues.