Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Download 1.52 Mb.
Pdf ko'rish
bet112/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   108   109   110   111   112   113   114   115   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

284.
285.
known as method hiding. In such a scenario, the version of the
method invoked is not determined by the type of object assigned
to the reference variable but by the type of the reference variable.
In this case, a base class reference variable is used. So, the
static method in the base class gets invoked.
Suppose you have a method in a base class that throws
an ArrayIndexOutOfBoundsException. And suppose this
method is overridden in the sub–class and declares that it
throws a RuntimeException. What do you think will
happen?
Answer:
The scenario above states that a base class method throws an
ArrayIndexOutOfBoundsException which is an unchecked
exception. The sub–class method overrides this method and
declares that it throws a RuntimeException. This situation is
valid and will not cause any error. In method overriding, a sub–
class method is not allowed to throw a checked exception that is
new or broader than the exception declared in the super–class
method. However, this rule does not apply to unchecked
exceptions. So, a sub–class method is free to declare any
unchecked exception and this will not cause any error. If the
subclass method had declared the java.lang.Exception or
any of its sub–classes in the throws clause, a compile time error
would have occurred. This is because java.lang.Exception
is a checked exception.
Suppose you have a List of objects. You want to send it as
a parameter to a client application but want to ensure that
the client application does not make changes to it. How
can you achieve this?


286.
Answer:
In order to ensure that a client application does not make
changes to a List, you need to obtain a read–only copy of the List
and return that copy to the client application. The Collections
class has several utility methods that return a read only copy of a
Collection. For example, it has a method called
Collections.unmodifiableList. This method accepts a
List as parameter and returns a List which is a read–only copy of
the input List. Once a read only copy of a Collection is obtained
via this method, any attempt to modify the List (like adding or
removing data) will result in a
UnsupportedOperationException. So, If the client
application tries to modify this List, the exception will occur.

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   108   109   110   111   112   113   114   115   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling