Abstraction

Java Abstract Class Interview Questions, Programs and Examples.

Factory-Pattern

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

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.

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


 

Abstract Vs interfaces in java
Can I ask you a small favour?