Thursday, June 13, 2024
HomeJavaIs it doable to have an summary methodology in a last class?...

Is it doable to have an summary methodology in a last class? Instance


This is without doubt one of the fascinating core Java questions which was requested to one in every of my readers just lately throughout a telephonic interview for a Java developer job interview. Though he is aware of that you simply can not make an summary class last in Java, he received confused by the wording of the strategies. The reply is easy, No, it isn’t doable to have an summary methodology in a last class in Java. Why? as a result of as quickly as you declare an summary methodology in a Java class, the category mechanically turns into an summary class and also you can not make an summary class last in Java as mentioned earlier than, therefore it isn’t doable to have an summary methodology in a last class in Java.

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:

Is it possible to have an abstract method in a final class?

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. 

However, sure, you may declare static strategies on each last class in addition to summary lessons, there isn’t any downside with that. If you’re studying Java, I counsel you additional learn Core Java for Impatient to get your self conversant in core ideas like summary and last class. It additionally covers Java 8.

Different associated Java Interview Questions for observe

  1. Can summary class have a constructor in Java? (reply)
  2. Are you able to override a static methodology in Java? (reply)
  3. Are you able to make a category summary and last in Java? (reply)
  4. Are you able to overload a static methodology in Java? (reply)
  5. Are you able to run a program with out fundamental() methodology in Java? (reply)
  6. Are you able to override a personal methodology in Java? (reply)
  7. Are you able to overload and override the primary() methodology in Java? (reply)
  8. Are you able to make array unstable in Java? (reply)
  9. 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. 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments