Abstraction Flashcards

(231 cards)

1
Q

What is abstraction in Object-Oriented Programming?

A

Abstraction is the process of hiding implementation details and exposing only essential features to the outside world.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why is abstraction important in OOP?

A

It reduces complexity and allows users to interact with objects at a high level without knowing internal logic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does abstraction help developers?

A

It lets them focus on what an object does rather than how it does it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is abstraction the same as an abstract class?

A

No. Abstraction is a concept, while abstract classes are one way to implement it in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the main ways to achieve abstraction in Java?

A

Abstract classes and interfaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the key goal of abstraction?

A

To hide complexity and expose only relevant behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Real-world example of abstraction?

A

Driving a car without knowing how the engine works internally.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is abstraction vs encapsulation?

A

Abstraction hides implementation complexity; encapsulation hides internal state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Does abstraction improve security?

A

Yes. It prevents users from accessing internal logic directly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which OOP principle reduces system complexity?

A

Abstraction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can abstraction exist without inheritance?

A

Yes. Interfaces and abstract classes can define abstraction independently.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is data abstraction?

A

Showing only essential data features while hiding internal representation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why is abstraction useful for large systems?

A

It simplifies design, improves maintainability, and supports modular development.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Is abstraction compile-time or runtime concept?

A

It is a design principle, not tied to compile-time or runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Key rule to remember?

A

Abstraction = hide details, show essentials.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How is abstraction different from encapsulation?

A

Abstraction hides implementation details and focuses on essential behavior, while encapsulation hides internal data and controls access to it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is abstraction in simple terms?

A

Hiding complexity and exposing only relevant features.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is encapsulation in simple terms?

A

Binding data and methods together and restricting direct access to internal state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

At what level does abstraction operate?

A

Design level — deciding what to expose and what to hide.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

At what level does encapsulation operate?

A

Implementation level — controlling how data is accessed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How is abstraction achieved in Java?

A

Using abstract classes and interfaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How is encapsulation achieved in Java?

A

Using access modifiers like private and providing getters/setters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What problem does abstraction solve?

A

Reduces complexity by hiding unnecessary implementation details.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What problem does encapsulation solve?

A

Protects data from unauthorized or unintended modification.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Which principle focuses on behavior exposure?
Abstraction.
26
Which principle focuses on data protection?
Encapsulation.
27
Example illustrating encapsulation?
Private fields with public getter/setter methods.
28
Example illustrating abstraction?
Interface defining methods without revealing internal logic.
29
Can abstraction exist without encapsulation?
Yes. A design can hide logic conceptually without strict access control.
30
Can encapsulation exist without abstraction?
Yes. Data can be protected even if implementation details are visible conceptually.
31
Key memory trick?
Abstraction hides how; Encapsulation hides data.
32
What is an abstract class in Java?
An abstract class is a class declared with abstract that cannot be instantiated and may contain abstract and concrete methods.
33
What is an abstract method?
A method declared without a body that must be implemented by subclasses.
34
Can an abstract class be instantiated?
No. Abstract classes cannot be instantiated directly.
35
Why do we use abstract classes?
To define a common base with shared logic while forcing subclasses to implement specific behavior.
36
Can an abstract class contain non-abstract methods?
Yes. It can include fully implemented methods.
37
What keyword is used to declare an abstract class?
abstract.
38
What must a subclass do if it extends an abstract class?
It must implement all abstract methods or itself be declared abstract.
39
Can abstract classes have constructors?
Yes. Constructors run when subclasses are instantiated.
40
Can abstract classes have fields?
Yes. They can have instance variables and static variables.
41
Can abstract classes have static methods?
Yes. Static methods are allowed.
42
Can abstract classes implement interfaces?
Yes. They can implement interfaces and optionally leave methods unimplemented.
43
Can abstract classes be final?
No. final prevents inheritance, which contradicts abstraction.
44
Can abstract methods be private?
No. Private methods cannot be overridden.
45
Difference between abstract class and interface?
Abstract classes can have state and implemented methods; interfaces define contracts.
46
Real-world example of abstract class?
Shape class with abstract draw() method implemented differently by Circle and Rectangle.
47
Key rule to remember?
Abstract class = partial implementation + must be subclassed.
48
Can a class contain an abstract method without being declared abstract?
No. If a class has even one abstract method, the class itself must be declared abstract.
49
Why must a class be marked abstract if it has an abstract method?
Because abstract methods have no implementation, and only abstract classes are allowed to defer implementation to subclasses.
50
What compile-time error occurs if a class has abstract method but isn’t abstract?
The compiler throws an error stating the class must be declared abstract.
51
Can abstract classes have zero abstract methods?
Yes. A class can be abstract even if it has no abstract methods.
52
Why would a class be abstract without abstract methods?
To prevent instantiation and enforce inheritance-only usage.
53
Can concrete classes contain abstract methods?
No. Only abstract classes can contain abstract methods.
54
What is the purpose of abstract methods?
To force subclasses to provide specific implementations.
55
Can an abstract method have a body?
No. Abstract methods cannot have implementations.
56
Can abstract methods be static?
No. Static methods cannot be abstract.
57
Can abstract methods be final?
No. final prevents overriding, which abstract methods require.
58
Can abstract methods be private?
No. Private methods cannot be overridden.
59
What keyword must be used with abstract methods?
abstract.
60
Must subclasses implement all abstract methods?
Yes, unless the subclass is also declared abstract.
61
Why does Java enforce this rule?
To guarantee that any concrete class provides implementations for all inherited abstract methods.
62
Key rule to remember?
Abstract method present → class must be abstract.
63
Can a method be declared both abstract and final in Java?
No. A method cannot be both abstract and final because the modifiers contradict each other.
64
Why can’t a method be abstract and final at the same time?
Because abstract requires overriding in subclasses, while final prevents overriding.
65
What is the purpose of an abstract method?
To force subclasses to provide an implementation.
66
What is the purpose of a final method?
To prevent subclasses from overriding it.
67
What compile-time error occurs if a method is abstract and final?
The compiler reports illegal combination of modifiers.
68
Can abstract methods be overridden?
Yes. They must be overridden unless subclass is abstract.
69
Can final methods be overridden?
No. final explicitly forbids overriding.
70
Can abstract methods have implementation?
No. They must not have a body.
71
Can final methods have implementation?
Yes. They must have a body because they cannot be overridden.
72
Which modifier requires subclass implementation?
abstract.
73
Which modifier blocks subclass modification?
final.
74
Can static methods be abstract?
No. Static methods cannot be overridden.
75
Can private methods be abstract?
No. Private methods cannot be overridden.
76
Why does Java forbid contradictory modifiers?
To maintain logical consistency and prevent impossible method behavior.
77
Key rule to remember?
abstract = must override; final = cannot override.
78
Can we instantiate an abstract class in Java?
No. Abstract classes cannot be instantiated directly.
79
Why can’t abstract classes be instantiated?
Because they may contain abstract methods without implementations.
80
What is the purpose of preventing abstract class instantiation?
To ensure incomplete classes are used only as base classes.
81
How do you use an abstract class if you can’t instantiate it?
By creating a subclass that extends it and instantiating the subclass.
82
Can an abstract class reference be created?
Yes. You can declare a reference of abstract type pointing to a subclass object.
83
Example of abstract reference usage?
Shape s = new Circle();
84
Can constructors exist in abstract classes?
Yes. They run when a subclass object is created.
85
What error occurs if you try to instantiate abstract class directly?
Compile-time error: class is abstract; cannot be instantiated.
86
Can an abstract class have no abstract methods?
Yes. It can still be abstract to prevent instantiation.
87
Can abstract classes be used for polymorphism?
Yes. They commonly serve as base types for runtime polymorphism.
88
Can you create anonymous instances of abstract classes?
Yes. Using anonymous inner classes.
89
Example anonymous abstract instantiation?
AbstractClass obj = new AbstractClass() { void method() {} };
90
Why allow references but not instances?
References enable polymorphism; instantiation would allow incomplete objects.
91
Can interfaces be instantiated?
No. Like abstract classes, they cannot be instantiated directly.
92
Key rule to remember?
Abstract class = blueprint, not object.
93
What is an interface in Java?
An interface is a blueprint that defines method signatures and constants which implementing classes must provide.
94
What does an interface contain?
Method declarations, constants, default methods, static methods, and nested types.
95
What is the main purpose of an interface?
To define a contract that classes must follow.
96
Are interface methods implemented by default?
No. Traditional interface methods are abstract unless declared default or static.
97
Can interfaces have method implementations?
Yes. Since Java 8, interfaces can have default and static methods with bodies.
98
What access modifier do interface methods have by default?
public.
99
What modifier do interface fields have by default?
public static final.
100
Can interfaces be instantiated?
No. Interfaces cannot be instantiated directly.
101
How does a class use an interface?
By implementing it using the implements keyword.
102
Can a class implement multiple interfaces?
Yes. Java supports multiple inheritance through interfaces.
103
What is the difference between interface and abstract class?
Interfaces define contracts; abstract classes can define both contracts and partial implementations.
104
Can interfaces extend other interfaces?
Yes. An interface can extend multiple interfaces.
105
Do interfaces support constructors?
No. Interfaces cannot have constructors.
106
Why are interfaces important for design?
They promote loose coupling and flexible architecture.
107
Real-world analogy of interface?
A remote control defines buttons (methods) but not how TV implements them.
108
Key rule to remember?
Interface = contract that classes must implement.
109
Is it allowed to declare static methods in a Java interface?
Yes. Since Java 8, interfaces can declare static methods with implementations.
110
Could interfaces have static methods before Java 8?
No. Before Java 8, interface methods were implicitly abstract and could not have bodies.
111
What is the purpose of static methods in interfaces?
To define utility or helper methods related to the interface that do not depend on instance objects.
112
How do you call a static method defined in an interface?
Using InterfaceName.methodName().
113
Can static interface methods be overridden?
No. Static methods cannot be overridden.
114
Can static interface methods be inherited by implementing classes?
No. They belong only to the interface and must be called using the interface name.
115
Difference between default and static methods in interfaces?
Default methods can be inherited and overridden; static methods cannot.
116
Can static interface methods access instance members?
No. They can only access static members.
117
Why were static methods added to interfaces in Java 8?
To allow interfaces to include helper logic without forcing implementation classes to define it.
118
Can static methods be private inside interfaces?
Yes. Since Java 9, interfaces can have private static methods for internal reuse.
119
Example of static method in interface?
interface Util { static void log() { System.out.println("log"); } }
120
Can interfaces contain static fields?
Yes. All interface fields are implicitly public static final.
121
What is a real use case of static interface methods?
Factory methods or validation utilities tied to the interface contract.
122
Key rule to remember?
Interface static methods belong to the interface, not implementing classes.
123
Can an interface be declared final in Java?
No. An interface cannot be declared final.
124
Why can’t an interface be final?
Because final prevents inheritance, but interfaces are meant to be implemented or extended by other classes or interfaces.
125
What does the final keyword do in Java?
It prevents inheritance, method overriding, or variable reassignment depending on context.
126
What would happen if an interface were final?
No class could implement it, which would defeat the purpose of an interface.
127
What is the primary purpose of an interface?
To act as a contract that other classes implement.
128
Can interface methods be final?
No. Interface methods must be overridable by implementing classes.
129
Why must interface methods be overridable?
Because implementing classes must provide their own implementations.
130
Can abstract classes be final?
No. Abstract classes also cannot be final because they must be extended.
131
What compile-time issue occurs if interface is marked final?
The compiler reports an illegal modifier combination.
132
Can an interface prevent implementation by design?
Not directly; interfaces are specifically designed to be implemented.
133
Which modifier would contradict interface purpose?
final.
134
Can interfaces extend other interfaces?
Yes. Interfaces can extend multiple interfaces.
135
Does final affect method implementation or inheritance?
It affects inheritance and overriding.
136
Key design principle behind interfaces?
Interfaces are contracts meant for extension and implementation.
137
Memory trick?
Interface = must implement; final = cannot extend.
138
What is a marker interface in Java?
A marker interface is an empty interface with no methods that signals a class has a special property or behavior.
139
What is the purpose of a marker interface?
To mark a class so JVM or frameworks treat it differently.
140
Does a marker interface contain methods?
No. It is intentionally empty.
141
How does a marker interface affect behavior?
Code checks whether a class implements it and changes execution accordingly.
142
Example of marker interfaces in Java?
Serializable, Cloneable, RandomAccess.
143
What happens if a class implements Serializable?
It signals that objects of that class can be serialized.
144
What happens if a class implements Cloneable?
It allows Object.clone() to copy the object without throwing CloneNotSupportedException.
145
Is a marker interface the same as annotation?
No. Annotations are metadata, while marker interfaces participate in type checking.
146
How does JVM detect a marker interface?
Using instanceof or reflection checks.
147
Why are marker interfaces considered a design pattern?
Because they communicate capability through type rather than methods.
148
Are marker interfaces still widely used today?
Less often. Modern Java prefers annotations for metadata signaling.
149
What is an alternative to marker interfaces?
Annotations like @Deprecated or custom annotations.
150
Can marker interfaces define default methods?
They technically could, but then they would no longer be pure marker interfaces.
151
Why were marker interfaces introduced historically?
To provide metadata-like signals before annotations existed.
152
Key rule to remember?
Marker interface = empty interface used as a capability tag.
153
What can be used instead of marker interfaces in Java?
Annotations can be used instead of marker interfaces.
154
Why are annotations preferred over marker interfaces?
Because annotations are more flexible, expressive, and do not affect class inheritance hierarchy.
155
What is the main limitation of marker interfaces?
They rely on type checking and cannot carry parameters or metadata.
156
What advantage do annotations have over marker interfaces?
Annotations can store metadata values and be processed at compile time or runtime.
157
Example replacement of marker interface?
Instead of implementing Serializable, a custom annotation like @Serializable could signal behavior.
158
Do annotations affect inheritance like interfaces?
No. Annotations do not change class hierarchy.
159
Can annotations carry data?
Yes. They can include fields like @Author(name="John").
160
How are annotations processed?
Using reflection or annotation processors.
161
Are marker interfaces obsolete?
Mostly replaced by annotations, but still used in some legacy or JVM-integrated cases.
162
Which Java feature replaced many marker interface use cases?
Annotations introduced in Java 5.
163
Can annotations enforce behavior directly?
No. They only provide metadata; logic must interpret them.
164
When might marker interfaces still be useful?
When type checking via instanceof is required.
165
Key difference between marker interface and annotation?
Marker interface affects type system; annotation provides metadata.
166
Which is more modern design approach?
Annotations.
167
Memory trick?
Marker interface = type tag; annotation = metadata tag.
168
How are annotations better than marker interfaces?
Annotations provide metadata without affecting inheritance and can carry values, making them more flexible and expressive than marker interfaces.
169
What is the main advantage of annotations over marker interfaces?
They can store additional information instead of just acting as a tag.
170
Do annotations change class hierarchy?
No. They do not affect inheritance or type structure.
171
Why are marker interfaces less flexible?
Because they only indicate presence and cannot include parameters or metadata.
172
Can annotations pass configuration data?
Yes. They can include attributes like @Role(name="Admin").
173
How are annotations processed?
Using reflection at runtime or annotation processors at compile time.
174
What limitation do marker interfaces have compared to annotations?
They cannot hold data or additional instructions.
175
Why do modern Java frameworks prefer annotations?
Because they allow declarative programming and configurable behavior.
176
Example framework use of annotations?
Spring uses annotations like @Autowired or @Component to configure components.
177
Can annotations replace all marker interfaces?
Most use cases yes, but not when type checking with instanceof is required.
178
Do annotations provide compile-time processing capability?
Yes. Annotation processors can generate code or validate rules during compilation.
179
Which is more readable in modern codebases?
Annotations, because intent and configuration are explicit.
180
Are annotations considered metadata or behavior?
Metadata that can influence behavior indirectly.
181
Key design advantage of annotations?
They separate metadata from type hierarchy.
182
Memory trick?
Marker interface = simple flag; annotation = smart flag with data.
183
What is the difference between an abstract class and an interface in Java?
An abstract class can have state, constructors, and implemented methods, while an interface defines a contract and supports multiple inheritance but cannot have instance state.
184
Can abstract classes contain implemented methods?
Yes. Abstract classes can include both abstract and concrete methods.
185
Can interfaces contain implemented methods?
Yes. Since Java 8, interfaces can have default and static methods with implementations.
186
Can abstract classes have instance variables?
Yes. They can define fields with any access modifier.
187
Can interfaces have instance variables?
No. Interface fields are implicitly public static final constants.
188
Can abstract classes have constructors?
Yes. Constructors run when subclasses are instantiated.
189
Can interfaces have constructors?
No. Interfaces cannot be instantiated, so they have no constructors.
190
How many abstract classes can a class extend?
Only one, because Java does not support multiple class inheritance.
191
How many interfaces can a class implement?
Multiple interfaces can be implemented.
192
What keyword is used for abstract class inheritance?
extends.
193
What keyword is used for interface implementation?
implements.
194
Can abstract classes implement interfaces?
Yes. Abstract classes can implement interfaces and optionally leave methods unimplemented.
195
Which is better for defining contracts?
Interfaces.
196
Which is better for sharing common implementation?
Abstract classes.
197
Can interfaces extend other interfaces?
Yes. Interfaces can extend multiple interfaces.
198
Can abstract classes extend other classes?
Yes. They can extend one class.
199
Which supports multiple inheritance?
Interfaces.
200
Which supports constructors and state?
Abstract classes.
201
Key memory trick?
Interface = contract only; Abstract class = contract + partial implementation.
202
Can interface variables be declared private or protected in Java?
No. All interface variables are implicitly public, static, and final.
203
What access modifier do interface fields have by default?
public.
204
Are interface variables instance variables?
No. They are class-level constants shared by all implementations.
205
Can you explicitly write public static final for interface variables?
Yes, but it is redundant because Java adds them automatically.
206
Why can’t interface variables be private or protected?
Because interfaces define contracts meant to be accessible to implementing classes.
207
Can interface variables be modified after initialization?
No. They are final constants.
208
What happens if you try to declare private variable in interface?
Compile-time error.
209
Can interfaces contain mutable fields?
No. All interface fields must be constant values.
210
How are interface variables accessed?
Using InterfaceName.VARIABLE_NAME.
211
Example of interface constant declaration?
interface A { int MAX = 100; }
212
Can interface methods be private?
Yes, but only since Java 9 and only for helper methods, not fields.
213
What is the purpose of interface variables?
To define shared constants for implementing classes.
214
Are interface variables stored per object?
No. They exist once at class level.
215
Why are interface variables always static?
Because interfaces cannot be instantiated.
216
Key rule to remember?
Interface fields = public static final constants.
217
How can you cast an object reference to an interface reference in Java?
If an object’s class implements an interface, its reference can be cast to that interface type.
218
Why is casting an object to an interface allowed?
Because the object already implements all methods defined by that interface.
219
Example of casting object to interface?
Runnable r = (Runnable) new Thread();
220
Is explicit casting always required when assigning to interface type?
No. Upcasting to an interface is automatic and does not require a cast.
221
What is upcasting to an interface?
Assigning an object reference to an interface type it implements.
222
Example of automatic interface upcasting?
List list = new ArrayList();
223
What is downcasting from interface to class?
Casting an interface reference back to its concrete class type.
224
When is downcasting safe?
Only when the object actually belongs to that class type.
225
What happens if you cast to an interface not implemented?
A ClassCastException occurs at runtime.
226
Can one object be cast to multiple interfaces?
Yes, if its class implements multiple interfaces.
227
Does casting change the object itself?
No. Casting only changes how the reference is treated.
228
Why use interface references instead of class references?
To enable polymorphism and flexible code design.
229
What methods can be called using interface reference?
Only methods declared in the interface type.
230
Can you access class-specific methods through interface reference?
No. You must downcast to the concrete class.
231
Key rule to remember?
Object can be referenced as any interface it implements.