Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 28, 2022 10:20 am GMT

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 dont 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. Its 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 { }

Let's try to extend an abstract class:

class Class_A { }abstract class MyAbstractClass extends Class_A { }
  • 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.


Original Link: https://dev.to/yigi/java-interview-abstract-1e32

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To