Java - AbstractionWhy might a developer choose an abstract class over an interface in Java when designing a base type?ABecause abstract classes can be instantiated directly.BBecause interfaces cannot have any methods.CTo provide some shared method implementations and state (fields) along with abstract methods.DTo avoid the need for subclasses to implement any methods.Check Answer
Step-by-Step SolutionSolution:Step 1: Understand abstract class capabilitiesAbstract classes can have both concrete methods and fields to share code and state.Step 2: Compare with interfacesInterfaces cannot hold state and until recent versions had only abstract methods; they cannot provide shared fields.Final Answer:To provide some shared method implementations and state (fields) along with abstract methods. -> Option CQuick Check:Abstract class for shared code and state = B [OK]Quick Trick: Abstract classes can hold fields and method code [OK]Common Mistakes:Thinking abstract classes can be instantiatedBelieving interfaces have no methods at allAssuming abstract classes remove implementation requirements
Master "Abstraction" in Java9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Java Quizzes Abstraction - Why abstraction is required - Quiz 6medium Constructors - Parameterized constructor - Quiz 3easy Constructors - Parameterized constructor - Quiz 12easy Custom Exceptions - Throwing custom exceptions - Quiz 1easy Exception Handling - Checked vs unchecked exceptions - Quiz 3easy Inheritance - Method overriding - Quiz 2easy Inheritance - Why inheritance is used - Quiz 8hard Object-Oriented Programming Concepts - Classes and objects - Quiz 15hard Polymorphism - Why polymorphism is needed - Quiz 3easy Polymorphism - Method overriding rules - Quiz 15hard