Java Interview
Java Interview
Java Interview
Used to represent the behavior of an object. Used to initialize the state of an object.
Method name may or may not be the same as class Constructor name must always be the same as the cla
name. name.
35. Can you override a private method or static method in Java?
You cannot override a private or static method in Java. You cannot override a private method
in subclass because it’s not accessible there.
36. What is method hiding?
Method hiding is an alternative to overriding a private or static method, which occurs when
you hide the superclass method. You create a similar method with the same return type and
same method arguments in child class. For example, you can create another private method
with the same name in the child class.
37. What is the difference between equals() and == in Java?
Equals() method
o Is defined in object class in Java.
o Used for checking the equality of two objects defined by business logic.
“==” (equality operator)
o A binary operator provided by Java programming language and used to
compare primitives and objects.
o public boolean equals (object o) is the method provided by the Object class.
o Default uses == operator to compare two objects. For example, you can
override a method like string class. equals() method is used to compare the
values of two objects.
38. Can you write multiple catch blocks under a single try block?
Yes, you can have multiple catch blocks under a single try block. Your approach should be
from specific to general, as shown in the following example:
What is Java?
What is a ClassLoader?
What are the differences between Heap and Stack Memory in Java?