Questions & Answers on Methods

Java Method questions and answers will help you to clear concept and increase your knowledge of the previously discussed chapters. These questions can be asked in any interviews, college vivas etc. I hope you will learn something out of these questions and answers.

1. What are the different types of methods in Java?

Static, non-static, final, native, abstract, synchronized.

2. What is the use of the final keyword in Java?

Final can be used for:
a) Methods, if the logic of that method is fixed.

b) Variables, if the value of the variable remains fixed.

c) Class, when declared final cannot be inherited.

3. What is the use of Native method?

The native method is used to indicate the compiler that existence of a method should not be checked during the time of compilation but should always be checked at the time of execution.

4. What do you mean by abstraction in Java & what are the different types?

Abstraction is a process in which only the functionalities are shown to the user while the implementation details are kept hidden. In

In Java, abstraction can be achieved with the help of abstract class and Interface.

5. What is the use of the abstract keyword in Java?

The abstract keyword is used to declare a method or class as abstract. An abstract method cannot have a body and an abstract class cannot be instantiated.

6. What is an Interface?

An Interface is similar to a class which accepts only public or default access specifiers.

7. What's the similarity between abstract class and interface?

Both support the concept of abstraction and cannot be instantiated.

8. What is the difference between abstract class and Interface?

  • An abstract class supports 0-100% abstraction whereas Interface supports 100% abstraction.
  • The user can design any type of method in an abstract class, however, the default nature of methods in Interface is public and abstract.
  • Any type of variable can be declared within abstract class whereas all variables in Interface are b default public static and final.
  • In an abstract class, a user can define any constructor, however, in Interface constructor design is not possible.

9. What is the procedure to call a static and non-static method?

Static methods- class name, object name, directly

Non-static methods- object name.

10. When are abstract methods used?

Abstract methods are used in those scenarios where the user does not want to instantiate the class directly but rather wants to provide the implementation details in the child class.

11. What is Externalizable Interface?

The Externalizable interface extends the Serializable interface and adds two methods, writeExternal() and readExternal(). These are automatically called while serialization or deserialization.

12. What are the restrictions imposed on a static method and a static block of code?

From a static method, one can only call static methods, invoking non-static method will throw compile time error and keywords like this and super cannot be used in static methods else a compile-time error will be thrown.

In Static blocks, you cannot declare a static variable.

13. Can an interface extend another interface?

An interface can extend another interface, which means it can add more methods and inherit its type.

14. If a method is declared as protected what is the scope of the method?

Protected methods can only be accessed within the scope of the same package and Class.

15. What is the impact of declaring a method as final?

A method cannot be overridden.

16. Can we have two methods in the same class with the same name?

Yes, and it is called method overloading.