0% found this document useful (0 votes)
6 views5 pages

Abstraction and Abstract Keyword

The document discusses various concepts related to abstract classes and interfaces in Java, including their properties, constructors, and methods. Key points include that abstract classes cannot be instantiated directly, can have constructors, and can contain both abstract and concrete methods. It also clarifies the differences between abstraction and encapsulation, as well as the rules surrounding method declarations in abstract classes and interfaces.

Uploaded by

Blue Red
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Abstraction and Abstract Keyword

The document discusses various concepts related to abstract classes and interfaces in Java, including their properties, constructors, and methods. Key points include that abstract classes cannot be instantiated directly, can have constructors, and can contain both abstract and concrete methods. It also clarifies the differences between abstraction and encapsulation, as well as the rules surrounding method declarations in abstract classes and interfaces.

Uploaded by

Blue Red
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Can we create an object of an abstract class?

Trick: Interviewer may expect confusion.

Answer: No, we cannot create an object of an abstract class directly.

Can an abstract class have a constructor?

Trick: Many assume "no" because objects can't be created.

Answer: Yes, an abstract class can have a constructor. It's called when a subclass
object is created.

Can an abstract class have a main() method?

Answer: Yes. Abstract class can have a main() and can be run like any other class.

Can an abstract class have non-abstract (concrete) methods?

Answer: Yes. Abstract class can contain both abstract and concrete methods.

What happens if we don't implement all abstract methods of an abstract class in a


subclass?

Answer: The subclass must also be declared abstract if it does not implement all
abstract methods.

Can an abstract method be private?

Trick: It's logical but invalid.

Answer: No. Abstract methods must be overridden, and private methods are not
accessible in subclasses.

Can we declare an abstract class as final?

Answer: No. final means no subclassing; abstract means expecting subclassing — they
conflict.

Can an interface contain constructors like an abstract class?

Answer: No. Interfaces cannot have constructors.

🔹 Conceptual/Real-World Abstraction Questions


What is abstraction in Java? Give a real-life example.

Answer: Hiding internal details and showing only essential features.


Example: You drive a car using the steering and pedals (interface), but don't know
the engine internals.

Difference between Abstraction and Encapsulation?

Answer:

Abstraction: Hides implementation details (focus on "what").

Encapsulation: Hides data using access modifiers (focus on "how").

Why use abstraction?


Answer: To achieve loose coupling, improve code readability, and separate "what"
from "how".

🔹 Code-Based Tricky Questions


What’s the output?

java
Copy
Edit
abstract class A {
A() {
System.out.println("Constructor of A");
}
}

class B extends A {
B() {
System.out.println("Constructor of B");
}
}

public class Test {


public static void main(String[] args) {
B b = new B();
}
}
Answer:

css
Copy
Edit
Constructor of A
Constructor of B
What’s wrong in this code?

java
Copy
Edit
abstract class A {
abstract void show();
private abstract void hide(); // error here
}
Answer: Compile-time error — abstract methods cannot be private.

Can we have a static abstract method?

Answer: No. Abstract methods need to be overridden, but static methods cannot be
overridden.

Can we override an abstract method as final in the subclass?

Answer: Yes, but it cannot be overridden further.

Can you instantiate an abstract class in Java?

Trick: Many might think abstract classes cannot be instantiated, but the question
is meant to check the understanding that you cannot directly instantiate an
abstract class but can instantiate a subclass of it.

What happens if a class that extends an abstract class doesn’t implement all the
abstract methods?

Trick: The interviewer is checking if you know that the subclass will either need
to implement the remaining abstract methods or itself be declared abstract.

Can you create an object of an abstract class using reflection?

Trick: While you can't instantiate an abstract class directly, the use of
reflection might trick the candidate into thinking otherwise.

What is the difference between abstract class and interface in Java?

Trick: This question might seem easy, but candidates often confuse the two. Key
points include that an abstract class can have both abstract and non-abstract
methods, while an interface can have only abstract methods (pre-Java 8) or default
methods (Java 8+).

If a class is declared abstract but all its methods are concrete, is it still
considered abstract?

Trick: The class is still considered abstract because it has been explicitly
declared as such, regardless of whether its methods are abstract or not.

Can an abstract class have a constructor?

Trick: Yes, an abstract class can have a constructor. The interviewer wants to see
if the candidate understands that even though you can’t instantiate an abstract
class directly, its constructor can be called through a subclass.

Is it possible to override a private method in an abstract class?

Trick: No, because private methods are not inherited by subclasses, so they can't
be overridden.

Can an abstract class implement an interface?

Trick: Yes, an abstract class can implement an interface and leave the method
implementations to its concrete subclasses.

If a subclass of an abstract class does not implement an abstract method, can it


still be instantiated?

Trick: No, unless the subclass itself is abstract. If it's a concrete subclass, it
must implement all abstract methods from the abstract class.

What is the purpose of declaring a method in an abstract class as abstract?

Trick: The purpose is to force subclasses to provide their own implementation of


the method, allowing for flexibility in how the method is implemented in different
contexts.
1. Can an abstract class have a constructor?
Answer:
Yes, abstract classes can have constructors. They are invoked when a subclass is
instantiated to initialize shared variables or common setup logic. However, you
cannot directly instantiate an abstract class 136.
Why Tricky: Many assume constructors are only for concrete classes.

2. Can an abstract class exist without any abstract methods?


Answer:
Yes. A class declared abstract doesn’t need abstract methods. Its purpose is to
prevent instantiation and enforce inheritance 16.
Example:

java
abstract class Logger {
void log(String message) { System.out.println(message); } // Concrete method
}
3. Can an abstract method be declared as static or private?
Answer:

Static: No. Abstract methods require overriding, but static methods cannot be
overridden 16.

Private: No. Private methods are not visible to subclasses, making overriding
impossible 13.

4. What happens if a subclass doesn’t override all abstract methods?


Answer: The subclass must also be declared abstract, or the code will fail to
compile. This ensures all abstract contracts are fulfilled 15.

5. Can an abstract class implement an interface without defining its methods?


Answer: Yes. The abstract class can leave interface methods unimplemented, forcing
subclasses to provide the implementation 36.
Why Tricky: Many think interfaces require immediate implementation.

6. Is an abstract class a "pure" abstraction?


Answer: No. Abstract classes provide 0–100% abstraction (they can have both
abstract and concrete methods). Interfaces (pre-Java 8) represent 100% abstraction
18.

7. Can an abstract class have a main() method?


Answer: Yes. The main() method is static and can be executed without instantiating
the abstract class 6.
Example:

java
abstract class Test {
public static void main(String[] args) {
System.out.println("Hello from abstract class!");
}
}
8. Why use an abstract class over an interface?
Answer:

Share code between related classes.

Define non-static/non-final fields.

Use access modifiers like protected 38.


Tricky Part: Choosing between the two depends on design goals (e.g., code reuse vs.
multiple inheritance).

9. Can a method in a non-abstract class be declared abstract?


Answer: No. Only abstract classes or interfaces can declare abstract methods. A
non-abstract (concrete) class must provide implementations for all methods 15.

10. What is the difference between abstraction and encapsulation?


Answer:

Abstraction: Hides implementation details (e.g., using abstract


classes/interfaces).

Encapsulation: Bundles data and methods into a single unit (e.g., using private
variables with getters/setters) 15.
Common Confusion: Freshers often conflate the two concepts.

Bonus: Can an abstract class be final?


Answer: No. final prevents inheritance, but an abstract class requires subclassing
to be useful

You might also like