Java Abstract Classes and Methods Interview Questions: Abstraction
Java Abstract Classes and Methods Interview Questions: Abstraction
codespaghetti.com/abstract-interview-questions/
Abstraction
Java Abstract Class Interview Questions, Programs and Examples.
Abstraction:
Abstraction is a process of hiding the implementation details and showing only functionality
to the user.
Another way, it shows only important things to the user and hides the internal details for
example sending sms, you just type the text and send the message. You don't know the
internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.
A class that is declared as abstract is known as abstract class. It needs to be extended and
its method implemented. It cannot be instantiated.
Abstract method:
A method that is declared as abstract and does not have implementation is known as
abstract method.
Example abstract method
Yes, abstract class can declare and define constructor in Java. Since you can not create
instance of abstract class, constructor can only be called during constructor chaining, i.e.
when you create instance of concrete implementation class.
Yes, abstract class can implement interface by using implements keyword. Since they are
abstract, they don’t need to implement all methods.
It’s good practice to provide an abstract base class, along with an interface to declare
Type.
No, abstract class can not be final in Java. Making them final will stop abstract class from
being extended, which is the only way to use abstract class.
They are also opposite of each other, abstract keyword enforces to extend a class, for
using it, on the other hand, final keyword prevents a class from being extended.
In real world also, abstract signifies incompleteness, while final is used to demonstrate
completeness. Bottom line is, you can not make your class abstract and final in Java, at
same time, it’s a compile time error.
2/4
Question: Can you create instance of abstract class?
No, you can not create instance of abstract class in Java, they are incomplete.
Even though, if your abstract class don’t contain any abstract method, you can not create
instance of it.
By making a class abstract, you told compiler that, it’s incomplete and should not be
instantiated. Java compiler will throw error, when a code tries to instantiate abstract class.
No, It is not mandatory for an abstract class to have any abstract method. We can make a
class abstract in Java, by just using abstract keyword in its declaration.
Now, In order to implement this method, We need to extend this abstract class and
override its method.
Yes, abstract class can contain main method, it just another static method and you can
execute Abstract class with main method, until you don’t create any instance.
3/4
Reference
http://softwareengineering.stackexchange.com/questions/96947/why-should-i-
declare-a-class-as-an-abstract-class
http://stackoverflow.com/questions/1320745/abstract-class-in-java
https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
4/4