Abstraction In Java Interview Questions

Abstraction In Java Interview Questions: In Java, an abstract class is a class that is declared abstract, meaning it cannot be instantiated on its own but rather serves as a blueprint for other classes to inherit from. Java abstract classes are an essential feature of Java programming and are commonly used in Java development. As a result, Java Abstract Class interview questions are a vital component of Java developer interviews.

These interview questions are aimed at assessing a candidate’s understanding of abstract classes in Java, including their purpose and advantages, as well as their application in programming. In this context, this article provides some commonly asked Java Abstract Class interview questions that can help Java developers prepare for interviews and better understand abstract classes in Java.

Abstraction In Java Interview Questions

  • What is Abstraction in Object Oriented Programming?
  • How is Abstraction different from Encapsulation?
  • What is an abstract class in Java?
  • Is it allowed to mark a method abstract method without marking the class abstract?
  • Is it allowed to mark a method abstract as well as final?
  • Can we instantiate an abstract class in Java?
  • What is an interface in Java?
  • Is it allowed to mark an interface method as static?
  • Why an Interface cannot be marked as final in Java?
  • What is a marker interface?
  • What can we use instead of the Marker interface?
  • How are Annotations better than Marker Interfaces?
  • What is the difference between abstract class and interface in Java?
  • Does Java allow us to use private and protected modifiers for variables in interfaces?
  • How can we cast an object reference to an interface reference?

Interview Questions On Abstraction In Java

  • Can an Interface Be Extended by Another Interface In Java?
  • When is an Abstract Method Used?
  • What is Abstraction?
  • What Is Interface in Java?
  • Can We Define Abstract Class Without Abstract Method?
  • In Which Kind of Situation Would an Interface Be Extended by Another Interface?
  • Why We Use Interface in Java?
  • Can We Create an Instance of Interface?
  • Can we declare the abstract method static?
  • Can We Declare the Abstract Method as Final?
  • Can We Declare the Abstract Method as Private?
  • Can We Use Public, Protected, and Default Modifiers with Abstract Method?
  • Differentiate an Interface and An Abstract Class?
  • What Is a Marker Interface?

Abstract Class Interview Questions

  • What Will Happen If We Do Not Override All the Abstract Methods in Sub-class?
  • Can an Inner Class Be Built-in An Interface?
  • How to Define an Abstract Class?
  • How Can We Define an Interface?
  • How Do We Use Comparator and Comparable Interfaces?
  • Can an abstract class be final in Java?
  • Can abstract class contain the main method in Java?
  • Can abstract class implement the interface in Java? Are they required to implement all methods?
  • What Is the Difference Between Path and Classpath?
  • Should The Main Method Be Compulsorily Declared in All Java Classes?
  • Why Is the Main Method Declared Static?
  • Can The Main Method Be Overloaded?
  • Can The Main Method Be Declared Final?
  • Can A Source File Contain More Than One Class Declaration?
  • Can A Class Be Declared as Static?
  • When Will You Define a Method as Static?
  • What Are the Restriction Imposed on A Static Method or A Static Block of Code?
  • Can We Declare a Static Variable Inside a Method?
  • What Is Use of An Abstract Variable?
  • Can A Method Inside an Interface Be Declared as Final?
  • Class C Implements Interface I, containing Method M1 And M2 Declarations. Class C Has Provided Implementation for Method M2. Can I create an object for Class C?
  • Can an Interface Extend Another Interface?
  • Can an Interface Implement Another Interface?
  • Can an Interface Be Final?
  • Can A Class Be Defined Inside an Interface?
  • Can an Interface Be Defined Inside a Class?
  • Why is an Interface Able to Extend More Than One Interface but an A-class can’t Extend More Than One Class?
  • If I Only Change the Return Type, Does the Method Become Overloaded?
  • Why Does Java Not Support Operator Overloading?
  • Can We Define Private and Protected Modifiers for Variables in Interfaces?
  • What Modifiers Are Allowed for Methods in An Interface?
  • What Is a Local, Member, and A-Class Variable?
  • Can A Byte Object Be Cast to A Double Value?
  • What Is the Difference Between a Static and A Non-Static Inner Class?
  • What Is an Object’s Lock and Which Objects Have Locks?
  • When Can an Object Reference Be Cast to An Interface Reference?
  • Which Non-Unicode Letter Characters May Be Used as The First Character of An Identifier?
  • What Restrictions Are Placed On Method Overloading?
  • What Is Casting?
  • What Is Downcasting?
  • If A Variable Is Declared as Private, Where May the Variable Be Accessed?
  • What Modifiers May Be Used with An Inner Class That Is a Member of An Outer Class?
  • How Many Bits Are Used to Represent Unicode, Ascii, Utf-16, And Utf-8 Characters?
  • What Restrictions Are Placed on The Location of a Package Statement Within a Source Code File?
  • What Is a Native Method?
  • What is the order of precedence and associativity, and how are they used?
  • Can an Anonymous Class Be Declared as Implementing an Interface and Extending A Class?
  • What Is the Range of The Char Type?
  • What Is the Range of The Short Type?
  • What Does It Mean That a Method or Field Is “static”?
  • Which Characters May Be Used as The Second Character Of An Identifier but not as the first character of an identifier?
  • How Is Rounding Performed Under Integer Division?
  • If A Class Is Declared Without Any Access Modifiers, Where May the Class Be Accessed?
  • Does A Class Inherit the Constructors of Its Superclass?
  • What Restrictions Are Placed on The Values of Each Case of a Switch Statement?
  • What Is the Difference Between a While Statement And A Do Statement?
  • What Modifiers Can Be Used with A Local Inner Class?
  • If A Method Is Declared as Protected, Where May the Method Be Accessed?
  • When Does the Compiler Supply a Default Constructor for A Class?

Abstraction Interview Questions In Java

What are the Abstract classes in Java? Or Explain Abstract classes?

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

What is an Abstract method in Java?

  • A method declaration preceded/prefixed with the abstract keyword with nobody or no implementation detail, which ends its method signature with a semicolon(;) is known as an abstract method

Does an abstract class compile successfully if it contains both concrete & abstract methods together?

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

Write an example of the abstract classes containing both concrete & abstract methods.

AbstractExample.java

package com.test.mobile.submitservicechange;
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");
	}
}

What happens if subclass extending abstract classes doesn’t override abstract methods?

  • The compiler throws an error to implement all abstract methods
  • Compiler-time error: The type AbstractExampleMain must implement the inherited abstract method AbstractExample.myAbstractMethod()
Abstraction In Java Interview Questions 1

What options are available for subclass extending an abstract class not to override abstract methods?

  • There are 2 options; either implement all abstract methods or make the extending class abstract
  • This way, the 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 the above screen capture from the previous question for details

Can abstract class implements an interface?

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

Can an abstract class be defined without any abstract methods?

  • Yes, a class can be declared with an abstract keyword even if it doesn’t get 1 abstract method
  • But vice-versa is not true; this means if a class contains abstract methods, then the class has to be declared with an abstract keyword

Is it mandatory to have abstract methods in an abstract class? If not, why is such a design required?

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

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

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

Can we define an abstract class without an abstract keyword in the class declaration?

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

The class compiles successfully if a 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
Abstraction In Java Interview Questions 2

Can we define a constructor inside an abstract class?

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

Can an abstract class be instantiated?

  • No, an abstract class cannot be instantiated
  • Instantiating abstract class throws a compile-time error
  • Compiler-time error: Cannot instantiate the type <Abstarct_Class_Name>
Abstraction In Java Interview Questions 3

Why abstract classes cannot be instantiated if constructors can be defined inside an abstract class?

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

Can an abstract class be final?

  • No, an abstract class cannot be the final
  • Abstract methods need to be implemented; therefore, it is overridden in the subclass
  • But by marking the final, we are restricting it to override
  • A 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

Can we declare an abstract method with a static modifier inside an abstract class?

  • No, an abstract class cannot be static
  • A 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

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

  • Yes, the concrete method can be declared with the final modifier

Can we declare an abstract method with a private modifier inside an abstract class?

  • No, an abstract class cannot be declared with private accessibility
  • A compilation error will be thrown with the 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

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

  • Final: as the sub-class needs to provide method implementation for all abstract methods inside an abstract class, therefore abstract cannot be marked as the final
  • Static: abstract methods belong to an instance, not a class. Therefore it cannot be marked as static
  • Private: abstract methods need to be overridden in the sub-class for this; we need wider accessibility
  • Marking abstract method declaration with final or static or private modifier –> results in a 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

What are all modifiers allowed for abstract method declaration?

  • public and protected access modifiers are allowed for abstract method declaration

What are all modifiers allowed for an abstract class declaration?

  • private, static, and final modifiers are NOT allowed for abstract method declaration

Can we define a private constructor inside an abstract class?

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

Is it ok to declare an abstract method inside a non-abstract class?

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

Can we declare static fields inside an abstract class?

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

Can we define static methods as giving concrete implementation inside an abstract class?

  • Yes, static methods are allowed inside an abstract class

Can an abstract method inside the abstract class throw exceptions? Or Can abstract method declaration include a throws clause?

  • Yes, abstract methods can throw an exception
  • See below screen capture
Abstraction In Java Interview Questions 4

Can the abstract class contain the main() method and start the execution? Write a program?

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

AbstractExampleMain.java

package in.software.testing.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();
	}
}

Displaying: main() method execution

Q) Can we use an overload abstract method in Java?

  • Yes, abstract methods can be overloaded
  • Its extending class will provide an implementation for all abstract methods

What is an Abstract Class, and what is its purpose?
Ans: A class that doesn’t provide complete implementation is defined as an abstract class. Abstract Classes enforce abstraction. Any class with an abstract method is automatically abstract itself and must be declared as such. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated. Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie. you may not call its constructor), abstract class may contain static data.

Can an abstract class be declared final?
Ans: No. An abstract class is useless unless it is inherited. Otherwise, it just results in a compile-time error.

What is the use of an abstract variable?
Ans: There is nothing called abstract variables, as variables can’t be declared abstract. Only classes and methods can be declared as abstract.

Can you create an object of an abstract class?
Ans: No. Abstract classes can’t be instantiated.

Can an abstract class be defined without any abstract methods?
Ans: Yes, it is possible. This is performed, to avoid instance creation of the class.

What is an abstract method?
Ans: An abstract method is a method whose implementation is deferred to a subclass.

Can an abstract class be final?
Ans: An abstract class may not be declared as final.

What is the final class?
Ans: A final class can’t be extended, i.e. the final class may not be subclassed. A final method can’t be overridden when its class is inherited. You can’t change the value of a final variable (is a constant).

What if the main() method is declared as private?
Ans: The program compiles properly, but at runtime, it will give the message “main() method not public.”

What if the static modifier is removed from the signature of the main() method?
Ans: Program compiles. But at runtime throws an error “NoSuchMethodError”.

What if I write static public void instead of the public static void?
Ans: The program compiles and runs properly.

What is the difference between abstract class and interface?
Ans:

  • All the methods declared inside an interface are abstract, whereas abstract classes must have at least one abstract method, and others may be concrete or abstract.
  • In abstract class, the keyword abstract must be used for the methods, whereas in interface, we need not use that keyword for the methods.
  • Abstract class must have subclasses, whereas interface can’t have subclasses.

If you feel that we missed adding any questions regarding Abstraction Interface Interview Questions, then you can mention that in that comment section. you can join our softwaretestingo telegram group to get more updates.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment