Java Interview Questions for Selenium Automation Testing

41. Is it allowed to override an overloaded method?

πŸ…° Yes, we can override an overloaded method in Java.

42. What is Abstraction in OOPs?

πŸ…° Abstraction is a technique by which we can hide the data that is not required to a user. It hides all unwanted data so that users can work only with the required data.

43. What is the difference between abstraction and encapsulation?

πŸ…° Abstraction is used to hide the implementation details whereas, encapsulation binds code and data into a single unit.

44. How to achieve or implement abstraction in Java?

πŸ…° There are two ways by which we can achieve abstraction in java. They are:

  • Abstract class (0 to 100%)
  • Interface (100%)

45. What is abstract class in Java?

πŸ…° A class that is declared with an abstract keyword is called abstract class. An abstract class has to be extended and its abstract methods must be implemented by a child class.

46. What is abstract method in Java?

πŸ…° A method which is declared with abstract modifier and has no implementation (means no body) is called an abstract method. It does not contain any body.

47. Can an abstract class be defined without any abstract methods?

πŸ…° Yes, it is possible. It is basically done to avoid instance creation of the class.

48. Can there be any abstract method without abstract class in Java?

πŸ…° No, if there is any abstract method in a class then class must be declared with abstract keyword.

49. Is it allowed to declare a method abstract as well as final?

πŸ…° No, because abstract method needs to be overridden by child class whereas, a final method cannot be overridden. Therefore, a method can be either abstract or final in Java.

50. Can we create an object of abstract class in Java?

πŸ…° No, we cannot create an instance of abstract class in Java. This is because abstract class is not a concrete class. If you try to instantiate, you will get compile time error.

51. Can we create a reference for an abstract class?

πŸ…° Yes, we can create a reference for an abstract class only when the object being provided the implementation for the abstract class.

52. When to use abstract class and abstract method in Java?

πŸ…° An abstract method is generally used when the same method has to perform different tasks depending on the object calling it.

An abstract class is used when we need to share the same method to all non-abstract subclasses with their own specific implementations.

53. What is interface in Java?

πŸ…° An interface in Java is a mechanism that is used to achieve complete abstraction. It is basically a kind of class but can have only abstract methods declaration and constants as members.

54. Is it allowed to declare an interface method as static?

πŸ…° Yes, from Java 8 onwards, we can declare static and default methods in an interface. Prior to Java 8, it was not allowed.

55. Can an interface be final?

πŸ…° No, because its implementation is provided by another class. A final method cannot be overridden. Therefore, an interface method cannot be declared as final.

56. What is marker interface?

πŸ…° An interface that has no variable and method is known as marker interface. For example, serializable, cloneable, remote, etc.

57. What is the difference between abstract class and interface?

πŸ…° Go to this tutorial: 12 Difference between abstract class and interface

58. What is the difference between class and interface?

πŸ…° Refer to this tutorial: Class vs Interface

59. What is the difference between abstract class and class?

πŸ…° a. We cannot create an instance of abstract class whereas, we can create an instance of class.
b. In abstract class, methods have no body. But in class, methods have body.

60. Why abstract class has constructor even though you cannot create object?

πŸ…° We cannot create an object of abstract class but we can create an object of subclass of abstract class. When we create an object of subclass of an abstract class, it calls the constructor of subclass.

This subclass constructor has super in the first line that calls constructor of an abstract class. Thus, the constructors of an abstract class are used from constructor of its subclass.

If the abstract class doesn’t have constructor, a class that extends that abstract class will not get compiled.