Java – Interview Question and Answers on Abstract Classes & Methods

In this article, we will cover some of the interview questions with their justification on Java Abstract Classes & methods

These are most frequently asked interview question from OOPS concepts

Read Abstract classes and methods with example

Java Abstract Classes & Methods :

Q) What is Abstract class in Java? Or Explain Abstract classes ?

  • A class with abstract keyword in class declaration is known as abstract class in Java
  • Unlike class, an abstract class can contain both abstract methods as well as concrete methods (i.e.; methods with braces and method body/implementation)

Q) What is an Abstract method in Java ?

  • A method declaration preceded/prefixed with abstract keyword with no body/implementation detail which ends its method signature with semicolon(;) is known as abstract method

Q) Whether abstract class compiles successfully, if it contains both concrete & abstract methods together ?

  • Yes, abstract class compile successfully as it can contain both abstract methods and concrete

Q) Write an example for abstract class containing both concrete & abstract method ?

AbstractExample.java

package in.bench.resources.abstractclass.example;

// abstract class
public abstract class AbstractExample {

	String demoString;
	static int demoCounter;

	// default no-arg constructor
	AbstractExample(){
		// do some initialization logic here
	}

	// abstract method declaration in abstract class
	abstract void myAbstractMethod();

	// concrete method definition in abstract class
	void myConcreteMethod() {
		System.out.println("AbstractExample: "
				+ "This is my concrete method in abstract class");
	}
}

Q) What happens, if sub class extending abstract class doesn’t override abstract methods ?

  • Compiler throws error to implement all abstract methods
  • Compiler-time error: The type AbstractExampleMain must implement the inherited abstract method AbstractExample.myAbstractMethod()
3_Interview_abstract_class_extending

Q) What all options available for sub class extending abstract class to not to override abstract methods ?

  • There are 2 options; either implements all abstract methods or make extending class as abstract
  • This way, next extending class must provide implementation or else again it can be marked as abstract
  • Options:
    1. Add unimplemented methods
    2. Make type ‘ExtendingClass’ abstract
  • Note: See above screen capture from previous question for details

Q) Can abstract class implements interface ?

  • Yes, an abstract class can implement interface and this is allowed

Q) Can an abstract class be defined without any abstract methods ?

  • Yes, a class can be declared with abstract keyword even if it doesn’t got 1 abstract method
  • But vice-versa is not true; means if a class contains abstract methods then class must be declared with abstract keyword

Q) Whether it is mandatory to have abstract methods in abstract class? If not, why such design is required ?

  • It’s not mandatory to have abstract methods in abstract class
  • Even without a single abstract method in a class can be declared as abstract
  • This is to flag compiler that this class is not for instantiation

Q) Can we define abstract class without abstract method? Why it is needed ?

  • Yes, a class can be declared with abstract keyword even if it doesn’t got 1 abstract method
  • This is to flag compiler that this class is not allowed to instantiate

Q) Can we define abstract class without abstract keyword in class declaration ?

  • No, abstract keyword is required at class declaration to declare abstract class

Q) Whether class compiles successfully, if class contains abstract methods and no abstract keyword at class declaration ?

  • Compiler throws error
  • Compiler-time error: This method requires a body instead of a semicolon
4_Interview_class_without_abstract_keyword

Q) Can we define constructor inside abstract class ?

  • Yes, we can define constructor inside abstract class
  • Both default & parameterized constructors are allowed inside abstract class

Q) Can abstract class be instantiated ?

  • No, abstract class cannot be instantiated
  • Instantiating abstract class throws compile-time error
  • Compiler-time error: Cannot instantiate the type <Abstarct_Class_Name>
5_Interview_abstract_method_throwing_exception

Q) Why abstract classes cannot be instantiated, if constructor can be defined inside abstract class ?

  • True, an abstract class cannot be instantiated; still having instance data members and constructor
  • This is to instruct compiler that no one should create an object of type abstract class
  • The reason is, every object has got some default behavior and specific behavior. In this case, abstract class is apt
  • So, we can put more common & general behavior with concrete method implementation and later extending (sub-classing) class can provide specific implementation for abstract methods in their own way

Q) Can an abstract class be final ?

  • No, an abstract class cannot be final
  • Abstract methods need to be implemented; therefore it is overridden in the sub class
  • But by marking final, we are restricting it to override
  • Compile-time error will be thrown: The abstract method display in type <abstract-class-name> can only set a visibility modifier, one of public or protected
  • In short, remember only public & protected modifiers are allowed for abstract method

Q) Can we declare abstract method with static modifier inside abstract class ?

  • No, an abstract class cannot be static
  • Compile-time error will be thrown: The abstract method display in type <abstract-class-name> can only set a visibility modifier, one of public or protected

Q) Can we declare concrete (non-abstract) method with final modifier inside abstract class ?

  • Yes, concrete method can be declared with final modifier

Q) Can we declare abstract method with private modifier inside abstract class ?

  • No, an abstract class cannot be declared with private accessibility
  • Compilation error will be thrown with below error
  • Compile-time error: The abstract method display in type <abstract-class-name> can only set a visibility modifier, one of public or protected

Q) Why modifiers such as final, static & private are not allowed for abstract methods declaration in abstract class ?

  • Final: as sub-class needs to provide method implementation for all abstract methods inside abstract class, therefore abstract cannot be marked as final
  • Static: abstract methods belongs to instance not class, therefore it cannot be marked as static
  • Private: abstract methods needs to be overridden in the sub-class for this we need more wider accessibility
  • By marking abstract method declaration with final or static or private modifier –> results in compilation error
  • Compile-time error: The abstract method display in type <abstract-class-name> can only set a visibility modifier, one of public or protected

Q) What are all modifiers allowed for abstract method declaration ?

  • pubic and protected access modifiers are allowed for abstract method declaration
  • Note :- private, static and final modifiers are NOT allowed for abstract method declaration

Q) What are all modifiers allowed for abstract class declaration ?

  • public & abstract modifiers are permitted
  • Note :- private, protected & static modifiers are NOT allowed for abstract class declaration

Q) Can we define private constructor inside abstract class?

  • Yes, it is allowed to have private constructor inside abstract class

Q) Is it ok to declare abstract method inside non-abstract class ?

  • No, it is not allowed to have abstract method inside concrete class
  • If there any abstract method, then class must be marked with abstract modifier

Q) Can we declare static fields inside abstract class ?

  • Yes, static fields and static methods are allowed to declare inside abstract class

Q) Can we define static methods giving concrete implementation inside abstract class ?

  • Yes, static methods are allowed inside abstract class

Q) Whether abstract method inside abstract class can throw exceptions? Or Can abstract method declaration include throws clause ?

  • Yes, abstract methods can throw exception
  • See below screen capture
5_Interview_abstract_method_throwing_exception

Q) Can abstract class contain main() method and starts the execution? Write an program ?

  • Yes, main() method allowed inside abstract class; also we can execute

AbstractExampleMain.java

package in.bench.resources.abstractclass.example;

// abstract class
public abstract class AbstractExampleMain {

	// abstract method throwing exception
	abstract void display() throws ClassCastException;

	static void staticDisplay() {
		System.out.println("Displaying: main() method execution");
	}

	public static void main(String arg[]) {
		staticDisplay();
	}
}

Output:

Displaying: main() method execution

Q) Can we an overload abstract method in Java ?

  • Yes, abstract methods can be overloaded
  • And its extending class will provide implementation for all abstract methods

Q) What is the difference between abstract class and interface ?

Q) Design choice between Abstract class v/s Interface in Java ?

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - Interface v/s Abstract Classes
Java - Abstract classes and methods with example