DEV Community

Cover image for Java Interview - abstract
Yiğit Erkal
Yiğit Erkal

Posted on

Java Interview - abstract

Let's talk about the abstract keyword in Java and what can be asked during the Java interview related to abstract?

Explain About Abstract Classes in Java?

Sometimes we may come across a situation where we cannot provide implementation to all the methods in a class. We want to leave the implementation to a class that extends it. In such case we declare a class as abstract. To make a class abstract we use key word abstract. Any class that contains one or more abstract methods is declared as abstract. If we don’t declare class as abstract which contains abstract methods we get compile time error.

For example; if we take a vehicle class we cannot provide implementation to it because there may be two wheelers , four wheelers etc. At that moment we make vehicle class abstract. All the common features of vehicles are declared as abstract methods in vehicle class. Any class which extends vehicle will provide its method implementation. It’s the responsibility of subclass to provide implementation. The important features of abstract classes are :

1) Abstract classes cannot be instantiated.
2) An abstract classes contains abstract methods, concrete methods or both.
3) Any class which extends abstract class must override all methods of abstract class.
4) An abstract class can contain either 0 or more abstract methods.

From this point, it is better to compare Abstract classes with Interfaces since the most common questions are related to comparison between them.

Abstract Class vs. Interface 🤼‍♂️

Interface support multiple inheritance
Abstract class does not support multiple inheritance

It means you can extend an interface with one or more (hence multiple inheritance) interfaces like:

interface Interface_A { }
interface Interface_B { }
interface Interface_C { }

interface MyInterface extends Interface_A, Interface_B, interface_C { }
Enter fullscreen mode Exit fullscreen mode

Let's try to extend an abstract class:

class Class_A { }

abstract class MyAbstractClass extends Class_A { }
Enter fullscreen mode Exit fullscreen mode
  • Empty methods can be defined in the interface, but both empty methods and filled methods can be defined in abstract classes.

  • Using abstract classes provides an advantage in terms of speed.

  • When we write a new method in the Interface, it is necessary to fill this method one by one in all the classes we implement from this interface, but the situation is different in abstract classes, when we define a method and fill it in, all classes derived from our abstract class gain this feature.

❗❗❗ This behavior has changed with Java 8. Please take a look at "default" methods in interfaces.

  • All the objects in the Interface must be "public", while in Abstract classes it is not necessary to have all the elements "public".

  • Interface does not contain constructor methods. An abstract class can contain constructor methods.

  • Interface methods cannot be static. Abstract class non-abstract methods can be defined as static.

Top comments (1)

Collapse
 
mustabelmo profile image
Mustapha Belmokhtar • Edited

Hi
the Third assertion is not true
Any concrete class which extends abstract class must override all methods of abstract class, if this class does not override all methods in the parent abstract class(es), it has to be marked abstract

abstract class A {
protected abstract void f();
protected abstract void g();
    }
abstract class B extends A {
@Override
protected void f(){
// overridden method
    }
 }
Enter fullscreen mode Exit fullscreen mode