Java OOP: 17 flashcards
All 17 flashcards from this kit, free and with no sign-up.
1. What is abstraction in Java?
Show the answer
Abstraction is the process of hiding implementation details and showing only essential features to the user.
2. Which two constructs in Java are used to achieve abstraction?
Show the answer
**Abstract classes** and **interfaces**.
3. What is an abstract class?
Show the answer
A class declared with the `abstract` keyword that cannot be instantiated and may contain abstract methods (without body) and concrete methods.
4. What is an abstract method?
Show the answer
A method declared without an implementation (no body) and marked with the `abstract` keyword; it must be overridden by subclasses.
5. Can an abstract class have constructors?
Show the answer
Yes. Abstract classes can have constructors, which are called when a concrete subclass is instantiated.
6. Can you instantiate an abstract class directly?
Show the answer
No. You cannot create an object of an abstract class directly; you must subclass it and instantiate the subclass.
7. What is an interface in Java?
Show the answer
A reference type that defines a contract of abstract methods (and constants) that implementing classes must fulfill.
8. Which keyword is used by a class to implement an interface?
Show the answer
`implements`
9. Can a class implement multiple interfaces?
Show the answer
Yes. Java allows a class to implement multiple interfaces, providing a form of multiple inheritance of type.
10. What is the difference between an abstract class and an interface in terms of method types?
Show the answer
Abstract classes can have both abstract and concrete methods; interfaces (before Java 8) could only have abstract methods. Since Java 8, interfaces can have default and static methods.
11. When should you prefer an interface over an abstract class?
Show the answer
Use an interface when you want to define a role or contract that unrelated classes can implement, or when you need multiple inheritance of type.
12. When should you prefer an abstract class over an interface?
Show the answer
Use an abstract class when you want to share code (fields, concrete methods) among closely related classes, or when you need to define non-public members.
13. What is the output of the following code? ```java abstract class A { abstract void show(); } class B extends A { void show() { System.out.println("B"); } } public class Main { public static void main(String[] args) { A obj = new B…
Show the answer
`B`
14. What happens if a concrete class does not override all abstract methods of its abstract superclass?
Show the answer
The subclass must also be declared `abstract`; otherwise, a compile-time error occurs.
15. Can an interface extend another interface?
Show the answer
Yes. An interface can extend one or more interfaces using the `extends` keyword.
16. What is a default method in an interface?
Show the answer
A method in an interface with a body, declared using the `default` keyword, allowing interfaces to provide a default implementation without breaking implementing classes.
17. What is the main benefit of abstraction in software design?
Show the answer
It reduces complexity by hiding implementation details, allowing changes to implementation without affecting the code that uses the abstraction.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages