Abstract Classes and Interfaces Interview Questions
Abstract Classes and Interfaces Interview Questions
o An abstract class is a class that cannot be instantiated and may contain abstract
methods (without implementation) and non-abstract methods (with
implementation).
o An abstract class cannot be instantiated and may have abstract methods, whereas a
concrete class can be instantiated and must implement all abstract methods from
abstract classes.
o Yes, an abstract class can have a constructor. It is called when a subclass object is
created, to initialize fields or perform any setup in the subclass.
o Yes, an abstract class can have final methods. These methods cannot be overridden
by any subclass.
o Yes, an abstract class can have multiple constructors. They can be used by subclasses
to initialize the state of the object in different ways.
o No, you cannot instantiate an abstract class directly. You can instantiate a subclass
that provides implementations for the abstract methods.
o Yes, an abstract class can implement an interface, but it can leave the
implementation of interface methods to its subclasses.
9. What happens if a subclass doesn’t implement all abstract methods of an abstract class?
o Yes, an abstract class can have static methods, but they cannot be abstract. Static
methods belong to the class, not to instances.
o Yes, an abstract class can extend another abstract class and inherit its abstract and
non-abstract methods.
o Yes, an abstract class can implement multiple interfaces and provide default
implementations for their methods.
o Abstract classes allow you to define a common base class with shared behavior while
forcing subclasses to implement specific behaviors.
o Abstract classes enable you to implement shared logic (non-abstract methods) that
can be reused by subclasses while enforcing subclass-specific behavior via abstract
methods.
o No, abstract classes cannot have abstract fields. Fields must always be initialized or
declared final.
o Abstract classes provide a foundation for other classes to extend, promoting code
reuse, and defining common behaviors in complex systems.
o Use an abstract class when you need to share code among related classes or when
you need a class to define some default behavior.
o Yes, an abstract class can be generic and can use type parameters in its methods and
fields.
Interfaces in Java
o Yes, since Java 8, interfaces can have default methods with implementations, which
allow backward compatibility.
o Yes, interfaces can have static methods with implementations, which are not
inherited by implementing classes.
o Yes, an interface can extend one or more interfaces. The implementing class must
implement all methods from the parent interfaces.
o Yes, Java allows a class to implement multiple interfaces, which helps achieve
multiple inheritance.
o Interfaces provide a way to define a contract for behavior, allowing multiple classes
to implement the same set of methods regardless of their class hierarchy.
o Yes, interfaces can have variables, but they are implicitly public, static, and final. You
cannot assign them new values after declaration.
o No, interfaces cannot have constructors because they cannot be instantiated directly.
32. What happens if a class implements two interfaces with conflicting default methods?
o If a class implements two interfaces with conflicting default methods, the class must
provide its own implementation to resolve the conflict.
o Use an interface when you need to define a contract that can be implemented by
multiple, unrelated classes. Interfaces provide more flexibility for multiple
inheritance.
o Yes, methods in an interface can throw exceptions, and the implementing class must
either handle them or declare them.
o Yes, interfaces can have static methods with implementations. These methods are
not inherited by implementing classes.
38. What is multiple inheritance and how does Java handle it?
39. What happens if a class implements multiple interfaces with the same method signature?
o The class must provide its own implementation of the method, resolving any
conflicts between the interfaces.
o As of Java 9, interfaces can have private methods, which are used for internal
implementation details, and cannot be accessed outside the interface.
o Interfaces allow decoupling between the client and the implementation. The client
only interacts with the interface, making it easy to change the implementation.
42. What are functional interfaces and why are they important?
o A functional interface has exactly one abstract method. They are used primarily for
lambda expressions and method references, enabling functional programming
features in Java.
o Interfaces allow you to write code that can work with objects of any class that
implements the interface, promoting polymorphism by decoupling code from
specific implementations.
o No, you cannot instantiate an interface directly. You must implement the interface in
a concrete class and instantiate the concrete class.
47. What is the difference between interface and abstract class in terms of method
implementation?
o No, an interface cannot extend a class. It can only extend other interfaces. However,
a class can implement multiple interfaces.
50. What is the difference between a marker interface and a regular interface?