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

This is i of the interesting essence Java questions which was asked to i of my readers latterly during a telephonic interview for Java developer chore interview. Even though he knows that you lot cannot brand an abstract degree finally inward Java, he got confused past times the wording of the methods. The reply is simple, No, it's non possible to accept an abstract method inward a finally degree inward Java. Why? because every bit shortly every bit you lot declare an abstract method inward a Java class, the degree automatically becomes an abstract degree in addition to you lot cannot brand an abstract degree final inward Java every bit discussed before, thence it's non possible to accept an abstract method inward a finally degree inward Java.

Many Java programmers acquire confused inward this enquiry simply 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 shortly every bit you lot declare an abstract method inward a class, it volition acquire an abstract degree in addition to it's non possible to brand an abstract degree finally inward Java.

This is the missing slice of information or you lot telephone phone it a fob which separates it from the to a greater extent than pop in addition to oft asked questions, can you lot declare a degree both abstract in addition to finally inward Java?

Let's encounter i code event to essay this indicate that you lot cannot declare an abstract method inward a finally class. We accept a populace finally degree called Hello which has an abstract method called print() inward Hello.java file.

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

As shortly every bit you lot type that inward your Eclipse IDE, you lot volition have next error:
The type Hello must move an abstract degree to define abstract methods



Here is simply about other screenshot which shows that you lot cannot accept an abstract method inward a finally degree inward Java:

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


Same is truthful when you lot write downward this code inward notepad in addition to compile it using javac from the command-line window. As per Java specification, in i trial you lot declare an abstract method within a class, it automatically becomes an abstract degree in addition to since you lot cannot brand an abstract degree finally inward Java, the compiler volition throw an error.

This is truthful for both occur bird or nested degree inward Java. Even if declare an abstract method on a nested finally class you lot volition have the same error.

Another follow-up enquiry on this theme is can an abstract degree accept static methods inward Java? The reply is yes, at that spot is no work alongside declaring a static method within an abstract degree inward Java because you lot don't demand to instantiate a degree to role the static method, you lot tin simply telephone phone them past times using the degree name.


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

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

You tin encounter that at that spot is no compile fourth dimension error. The code compiles fine, thence it is perfectly ok to declare a static method within an abstract class inward Java.


That's all well-nigh whether it's possible to accept an abstract method inward a finally degree or not? As I said it's non possible because in i trial you lot create an abstract method within a class, every bit per Java specification the degree automatically becomes an abstract class. Since you lot cannot brand a finally degree abstract inward Java, this becomes illegal in addition to compiler prohibits you lot from doing that past times throwing an error. But, yes, you lot tin declare static methods on both finally degree every bit good every bit abstract classes, at that spot is no work alongside that. If you lot are learning Java, I advise you lot farther read Core Java for Impatient to acquire yourself familiar alongside essence concepts similar abstract in addition to finally class. It every bit good covers Java 8.


Other related Java Interview Questions for practice
  1. Can abstract degree accept a constructor inward Java? (answer)
  2. Can you lot override a static method inward Java? (answer)
  3. Can you lot overload a static method inward Java? (answer)
  4. Can you lot run a computer program without main() method inward Java? (answer)
  5. Can you lot override a person method inward Java? (answer)
  6. Can you lot overload in addition to override the main() method inward Java? (answer)
  7. Can you lot brand array volatile inward Java? (answer)
  8. Can you lot declare a degree static inward Java? (answer)

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

Thanks for reading this article, if you lot similar this essence Java interview enquiry in addition to my explanation in addition to then delight part alongside your friends in addition to colleagues. 

Sumber https://javarevisited.blogspot.com/

0 Response to "Is It Possible To Convey An Abstract Method Inwards A In Conclusion Class?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel