0% found this document useful (0 votes)
32 views

Java Abstract Classes and Methods Interview Questions: Abstraction

This document discusses Java abstract classes and methods through a series of interview questions and answers. It explains that abstraction hides implementation details and shows only functionality to the user. An abstract class cannot be instantiated and must be extended, while an abstract method declaration does not include a body. Abstract classes can have constructors and implement interfaces, but cannot be final. While abstract classes do not need abstract methods, abstract methods are method declarations without a body.

Uploaded by

oasisdesert
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Java Abstract Classes and Methods Interview Questions: Abstraction

This document discusses Java abstract classes and methods through a series of interview questions and answers. It explains that abstraction hides implementation details and shows only functionality to the user. An abstract class cannot be instantiated and must be extended, while an abstract method declaration does not include a body. Abstract classes can have constructors and implement interfaces, but cannot be final. While abstract classes do not need abstract methods, abstract methods are method declarations without a body.

Uploaded by

oasisdesert
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Abstract Classes And Methods Interview Questions

codespaghetti.com/abstract-interview-questions/

Abstraction
Java Abstract Class Interview Questions, Programs and Examples.

Question: How To Describe Abstraction In Interview?

Abstraction is a process of hiding the implementation details and showing only


functionality to the user.
A method that is declared as abstract and does not have implementation is known as
abstract method.
There are two ways to achieve abstraction in java
1- By Abstract class (0 to 100%) , 2- By Interface (100%)

Question: What is abstraction and abstract class in Java?

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.

Ways to achieve Abstaction

There are two ways to achieve abstraction in java


1/4
Abstract class (0 to 100%)
Interface (100%)

Abstract class in Java:

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.

Example abstract class

abstract class A{}

Abstract method:

A method that is declared as abstract and does not have implementation is known as
abstract method.
Example abstract method

abstract void printStatus();//no body and abstract

Question: Can abstract class have constructors in Java?

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.

Question: Can abstract class implements interface in Java? does they


require to implement all methods?

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.

Question: Can abstract class be final in Java?

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.

Question: Is it necessary for abstract class to have abstract method?

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.

Question: What is abstract method in Java?

An abstract method is a method without body. It uses abstract keyword in method


declaration. All method declared inside Java Interface are by default abstract. Here is an
example of abstract method in Java

public void abstract printVersion();

Now, In order to implement this method, We need to extend this abstract class and
override its method.

Question: Can abstract class contains main method in Java?

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.

Question: Abstract Class Vs Interface

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

You might also like