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.