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

This is 1 of the interesting pith Java questions which was asked to 1 of my readers lately during a telephonic interview for Java developer project interview. Even though he knows that you lot cannot brand an abstract degree in conclusion inward 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 inward a in conclusion 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 convey an abstract method inward a in conclusion degree inward Java.

Many Java programmers acquire confused inward this query simply because of the wording of the question, every bit I said, fifty-fifty the reader who was asked this query was familiar amongst the concept, he didn't realize that every bit shortly every bit you lot declare an abstract method inward a class, it volition driblet dead an abstract degree in addition to it's non possible to brand an abstract degree in conclusion inward Java.

This is the missing slice of information or you lot telephone telephone it a flim-flam which separates it from the to a greater extent than pop in addition to oftentimes asked questions, can you lot declare a degree both abstract in addition to in conclusion inward Java?

Let's run across 1 code event to examine this betoken that you lot cannot declare an abstract method inward a in conclusion class. We convey a world in conclusion 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 endure an abstract degree to define abstract methods



Here is about other screenshot which shows that you lot cannot convey an abstract method inward a in conclusion degree inward Java:

 This is 1 of the interesting pith Java questions which was asked to 1 of my readers r Is it possible to convey an abstract method inward a in conclusion 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, 1 time 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 in conclusion inward Java, the compiler volition throw an error.

This is truthful for both overstep flat or nested degree inward Java. Even if declare an abstract method on a nested in conclusion class you lot volition have the same error.

Another follow-up query on this theme is can an abstract degree convey static methods inward Java? The reply is yes, at that topographic point is no employment amongst 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 telephone them past times using the degree name.


We tin alteration 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[]) {      .. about code hither   }  }

You tin run across that at that topographic point 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 almost whether it's possible to convey an abstract method inward a in conclusion degree or not? As I said it's non possible because 1 time 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 in conclusion 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 in conclusion degree every bit good every bit abstract classes, at that topographic point is no employment amongst that. If you lot are learning Java, I advise you lot farther read Core Java for Impatient to acquire yourself familiar amongst pith concepts similar abstract in addition to in conclusion class. It besides covers Java 8.


Other related Java Interview Questions for practice
  1. Can abstract degree convey 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 soul 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 ii
Java Programming Interview Exposed
Cracking the code interview - 189 qustions in addition to solutions

Thanks for reading this article, if you lot similar this pith Java interview query in addition to my explanation in addition to so delight part amongst your friends in addition to colleagues. 

Komentar

Postingan populer dari blog ini

2 Ways To Banking Concern Tally If A String Is Rotation Of Other Inward Java?

How To Convert String To Integer To String Inward Coffee Amongst Example

How To Induce Chrome, Firefox Blurry, Over Bright, Fading Afterwards Windows Ten Update