0% found this document useful (0 votes)
7 views

Abstract Classes and Interfaces Interview Questions

Uploaded by

abinash ayyappan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Abstract Classes and Interfaces Interview Questions

Uploaded by

abinash ayyappan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Abstract Classes in Java

1. What is an abstract class in Java?

o An abstract class is a class that cannot be instantiated and may contain abstract
methods (without implementation) and non-abstract methods (with
implementation).

2. What is the difference between an abstract class and a concrete class?

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.

3. Can an abstract class have a constructor?

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.

4. Can an abstract class have final methods?

o Yes, an abstract class can have final methods. These methods cannot be overridden
by any subclass.

5. Can we have multiple constructors in an abstract class?

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.

6. Can we instantiate an abstract class?

o No, you cannot instantiate an abstract class directly. You can instantiate a subclass
that provides implementations for the abstract methods.

7. Can an abstract class implement an interface?

o Yes, an abstract class can implement an interface, but it can leave the
implementation of interface methods to its subclasses.

8. What is the difference between an abstract method and a non-abstract method in an


abstract class?

o An abstract method has no body and must be implemented by subclasses, while a


non-abstract method has a body and can be used directly by subclasses.

9. What happens if a subclass doesn’t implement all abstract methods of an abstract class?

o If a subclass does not implement all abstract methods, it must be declared as


abstract itself.

10. Can an abstract class have static methods?

o Yes, an abstract class can have static methods, but they cannot be abstract. Static
methods belong to the class, not to instances.

11. Can an abstract class extend another abstract class?

o Yes, an abstract class can extend another abstract class and inherit its abstract and
non-abstract methods.

12. Can an abstract class implement multiple interfaces?

o Yes, an abstract class can implement multiple interfaces and provide default
implementations for their methods.

13. What is the significance of abstract classes in Java?

o Abstract classes allow you to define a common base class with shared behavior while
forcing subclasses to implement specific behaviors.

14. How does an abstract class help in code reusability?

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.

15. Can abstract classes have abstract fields?

o No, abstract classes cannot have abstract fields. Fields must always be initialized or
declared final.

16. What is the purpose of abstract classes in real-world applications?

o Abstract classes provide a foundation for other classes to extend, promoting code
reuse, and defining common behaviors in complex systems.

17. Is it possible to override a non-abstract method in a subclass?

o Yes, a non-abstract method in an abstract class can be overridden in a subclass,


although it is optional.

18. When should you use an abstract class over an interface?

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.

19. Can an abstract class be generic?

o Yes, an abstract class can be generic and can use type parameters in its methods and
fields.

20. What is the significance of abstract classes in polymorphism?

o Abstract classes enable polymorphism by providing a common superclass for


different subclasses, allowing for dynamic method dispatch.

Interfaces in Java

21. What is an interface in Java?

o An interface defines a contract that specifies methods a class must implement. It


cannot provide method implementations (except default or static methods).

22. What is the difference between an interface and an abstract class?


o An interface cannot have method implementations (except default or static
methods), while an abstract class can have both abstract and concrete methods.

23. Can an interface have default methods in Java?

o Yes, since Java 8, interfaces can have default methods with implementations, which
allow backward compatibility.

24. Can an interface have static methods in Java?

o Yes, interfaces can have static methods with implementations, which are not
inherited by implementing classes.

25. Can an interface extend another interface?

o Yes, an interface can extend one or more interfaces. The implementing class must
implement all methods from the parent interfaces.

26. Can a class implement multiple interfaces in Java?

o Yes, Java allows a class to implement multiple interfaces, which helps achieve
multiple inheritance.

27. What is the use of the default keyword in an interface?

o The default keyword allows an interface to provide default implementations for


methods, reducing the need for implementing them in every class.

28. What is the significance of interfaces in Java?

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.

29. Can an interface have variables in Java?

o Yes, interfaces can have variables, but they are implicitly public, static, and final. You
cannot assign them new values after declaration.

30. Can an interface have a constructor?

o No, interfaces cannot have constructors because they cannot be instantiated directly.

31. What is the @FunctionalInterface annotation in Java?

o The @FunctionalInterface annotation indicates that the interface is intended to be


used as a functional interface, which must have exactly one abstract method.

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.

33. What is the difference between implements and extends in Java?

o A class uses implements to implement an interface, while it uses extends to inherit


from another class or interface.

34. Can an interface be generic in Java?


o Yes, interfaces can be generic, allowing you to define type parameters that are used
in the method signatures of the interface.

35. Why would you choose an interface over an abstract class?

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.

36. Can a method in an interface throw exceptions?

o Yes, methods in an interface can throw exceptions, and the implementing class must
either handle them or declare them.

37. Can you define a method as static in an interface?

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?

o Java supports multiple inheritance through interfaces. A class can implement


multiple interfaces, but it cannot extend more than one class.

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.

40. Can interfaces have private methods?

o As of Java 9, interfaces can have private methods, which are used for internal
implementation details, and cannot be accessed outside the interface.

41. How does an interface help in achieving loose coupling in Java?

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.

43. Can an interface have an abstract method with default implementation?

o No, an abstract method cannot have a default implementation. Only non-abstract


methods in an interface can have default implementations.

44. How can an interface be used for polymorphism 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.

45. What are the advantages of using interfaces in Java?


o Interfaces allow multiple inheritance of behavior, improve modularity, and provide a
clear contract for classes to follow, enhancing flexibility and maintainability.

46. Can you instantiate an interface directly?

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 An interface defines only method signatures (except default/static methods), while


an abstract class can have both abstract and concrete methods with
implementations.

48. Can an interface extend a class?

o No, an interface cannot extend a class. It can only extend other interfaces. However,
a class can implement multiple interfaces.

49. How does Java 8 improve interfaces with default methods?

o Java 8 introduced default methods in interfaces, allowing interfaces to have method


implementations, making it easier to evolve APIs without breaking existing code.

50. What is the difference between a marker interface and a regular interface?

o A marker interface is an interface with no methods, used to indicate some property


or behavior to a class. Regular interfaces define methods to be implemented by the
class.

You might also like