What will be the output of following java program?

By | August 9, 2019

In this post we will learn about one of very tricky java programming interview question where interviewer will give you code snippet and you will have to find the output of given code snippet.

What will be the output of following java program?

 Answer: The code won’t compile because we can’t have an Object class method with static keyword in subclass. You should note that Object class is having toString() method. Above code will throw a compile-time error as “This static method cannot hide the instance method from Object”. The reason is that static method belongs to the Class and since every class is subclass of Object class in java so we can’t have the same method in the instance as well as in Class. You will not get this error if you change the method name from toString() to something else that is not present in superclass Object class.

You May also like:

Can we call static method with null object?
Can we override static method in Java?

Thanks for visiting this blog.
If you have any feedback or suggestion please feel free to drop in blow comment box.

Leave a Reply

Your email address will not be published.