Is It Possible To Accept An Abstract Method Inwards A In Conclusion Class?

This is i of the interesting gist Java questions which was asked to i of my readers late during a telephonic interview for Java developer project interview. Even though he knows that you lot cannot brand an abstract cast terminal inwards Java, he got confused past times the wording of the methods. The reply is simple, No, it's non possible to convey an abstract method inwards a terminal cast inwards Java. Why? because every bit presently every bit you lot declare an abstract method inwards a Java class, the cast automatically becomes an abstract cast as well as you lot cannot brand an abstract cast final inwards Java every bit discussed before, thus it's non possible to convey an abstract method inwards a terminal cast inwards Java.

Many Java programmers acquire confused inwards this enquiry merely because of the wording of the question, every bit I said, fifty-fifty the reader who was asked this enquiry was familiar alongside the concept, he didn't realize that every bit presently every bit you lot declare an abstract method inwards a class, it volition acquire an abstract cast as well as it's non possible to brand an abstract cast terminal inwards Java.

This is the missing slice of information or you lot telephone outcry upward it a fob which separates it from the to a greater extent than pop as well as ofttimes asked questions, can you lot declare a cast both abstract as well as terminal inwards Java?

Let's meet i code event to seek this signal that you lot cannot declare an abstract method inwards a terminal class. We convey a populace terminal cast called Hello which has an abstract method called print() inwards Hello.java file.

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

As presently every bit you lot type that inwards your Eclipse IDE, you lot volition have next error:
The type Hello must live on an abstract cast to define abstract methods



Here is roughly other screenshot which shows that you lot cannot convey an abstract method inwards a terminal cast inwards Java:

 This is i of the interesting gist Java questions which was asked to i of my readers r Is it possible to convey an abstract method inwards a terminal class?


Same is truthful when you lot write downwards this code inwards notepad as well as compile it using javac from the command-line window. As per Java specification, i time you lot declare an abstract method within a class, it automatically becomes an abstract cast as well as since you lot cannot brand an abstract cast terminal inwards Java, the compiler volition throw an error.

This is truthful for both overstep marker or nested cast inwards Java. Even if declare an abstract method on a nested terminal class you lot volition have the same error.

Another follow-up enquiry on this topic is can an abstract cast convey static methods inwards Java? The reply is yes, at that spot is no occupation alongside declaring a static method within an abstract cast inwards Java because you lot don't demand to instantiate a cast to piece of work the static method, you lot tin post away merely telephone outcry upward them past times using the cast name.


We tin post away alteration our code event 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 post away meet that at that spot 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 nigh whether it's possible to convey an abstract method inwards a terminal cast or not? As I said it's non possible because i time you lot create an abstract method within a class, every bit per Java specification the cast automatically becomes an abstract class. Since you lot cannot brand a terminal cast abstract inwards Java, this becomes illegal as well as compiler prohibits you lot from doing that past times throwing an error. But, yes, you lot tin post away declare static methods on both terminal cast every bit good every bit abstract classes, at that spot is no occupation alongside that. If you lot are learning Java, I advise you lot farther read Core Java for Impatient to acquire yourself familiar alongside gist concepts similar abstract as well as terminal class. It too covers Java 8.


Other related Java Interview Questions for practice
  1. Can abstract cast convey a constructor inwards Java? (answer)
  2. Can you lot override a static method inwards Java? (answer)
  3. Can you lot overload a static method inwards Java? (answer)
  4. Can you lot run a programme without main() method inwards Java? (answer)
  5. Can you lot override a soul method inwards Java? (answer)
  6. Can you lot overload as well as override the main() method inwards Java? (answer)
  7. Can you lot brand array volatile inwards Java? (answer)
  8. Can you lot declare a cast static inwards Java? (answer)

Further Reading
Java Fundamentals, Part 1 as well as ii
Java Programming Interview Exposed
Cracking the code interview - 189 qustions as well as solutions

Thanks for reading this article, if you lot similar this gist Java interview enquiry as well as my explanation so delight portion alongside your friends as well as colleagues. 

Belum ada Komentar untuk "Is It Possible To Accept An Abstract Method Inwards A In Conclusion Class?"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel