Many Java programmers get confused on this query simply due to the wording of the query, as I stated, even the reader who was requested this query was conversant in the idea, he did not understand that as quickly as you declare an summary methodology in a category, it is going to turn into an summary class and it isn’t doable to make an summary class last in Java.
That is the lacking piece of knowledge otherwise you name it a trick which separates it from the extra standard and often requested questions, are you able to declare a category each summary and last in Java?
Let’s examine one code instance to show this level that you simply can not declare an summary methodology in a last class. We now have a public last class known as Hiya which has an summary methodology known as print() in Hiya.java file.
public last class Hiya { public summary print(); }
As quickly as you kind that in your Eclipse IDE, you’ll obtain following error:
The sort Hiya should be an summary class to outline summary strategies
Right here is one other screenshot which exhibits that you simply can not have an summary methodology in a last class in Java:
The identical is true once you write down this code in notepad and compile it utilizing javac from the command-line window. As per Java specification, when you declare an summary methodology inside a category, it mechanically turns into an summary class and since you can’t make an summary class last in Java, the compiler will throw an error.
That is true for each top-level or nested lessons in Java. Even when declare an summary methodology on a nested last class you’ll obtain the identical error.
One other follow-up query on this subject is can an summary class have static strategies in Java? The reply is sure, there isn’t any downside with declaring a static methodology inside an summary class in Java since you needn’t instantiate a category to make use of the static methodology, you may simply name them through the use of the category identify.
We will modify our code instance to incorporate the fundamental() methodology within the Hiya class, which is a static methodology in Java as proven under:
public summary class Hiya { public summary void print(); public static void fundamental(String args[]) { .. some code right here } }
You’ll be able to see that there isn’t any compile-time error. The code compiles advantageous, therefore it’s completely okay to declare a static methodology inside an summary class in Java.
That is all about whether or not it is doable to have an summary methodology in a last class or not? As I stated it isn’t doable as a result of when you create an summary methodology inside a category, as per Java specification the category mechanically turns into an summary class. Because you can not make a last class summary in Java, this turns into unlawful and the compiler prohibits you from doing that by throwing an error.
Different associated Java Interview Questions for observe
- Can summary class have a constructor in Java? (reply)
- Are you able to override a static methodology in Java? (reply)
- Are you able to make a category summary and last in Java? (reply)
- Are you able to overload a static methodology in Java? (reply)
- Are you able to run a program with out fundamental() methodology in Java? (reply)
- Are you able to override a personal methodology in Java? (reply)
- Are you able to overload and override the primary() methodology in Java? (reply)
- Are you able to make array unstable in Java? (reply)
- Are you able to declare a category static in Java? (reply)
Thanks for studying this text, in case you like this core Java interview query and my rationalization then please share with your pals and colleagues.