Top 25 Java Abstraction Interview Questions You Must Prepare 19.Mar.2024

No, we cannot create object of both an interface and abstract class.

An abstract method is declared in the parent class when we want a class which contains a particular method but on the other hand we want its implementation to be determined by child class.

Marker interface is an interface with no fields or methods in Java. 

Uses of marker interface are as follows: 

  • We use marker interface to tell java compiler to add special behavior to the class implementing it. 
  • Java marker interface has no members in it. 
  • It is implemented by classes in order to get some functionality. 

For instance when we want to save the state of an object then we can implement serializable interface.

An interface in java is a blueprint of a class that has static constants and abstract method by default.

No, we can't declare abstract method in non-abstract class.

For example:

class Demo

{

abstract void show();

}

Above example will throw compile time error.

Remember that any class that implements an interface must implement the method headings that are declared in that interface.

If that particular interface extends from other interfaces, then the implementing class must also implement the methods in the interfaces that are being extended or derived from.

As shown in the example above, if we have a class those implements the FourLegs interface, then that class must define the method headings in both the 'FourLegs' interface and the 'Body' interface.

Yes, an inner class can be built in an interface.

Example:

public interface xyz

{

static int p=0;

void m();

class c

{

c ()

{

int q;

System.out.println("inside");

};

public static void main(String c[])

{

System.out.println("inside ");

}

}

}

No, we cannot use final keyword with abstract class.

There are two ways in java we can achieve abstraction:

  • By using abstract class (0 to 100%).
  • By using interface (100%).

Yes, we can use public, protected and default modifiers with abstract method.

No, we can't use static keyword with abstract method.

No, we cannot create an instance of an abstract class.

These classes cannot be instantiated and are either partially implemented or not at all implemented.

This class contains one or more abstract methods which are simply method declarations without a body.

A class containing abstract method is called an abstract class. An Abstract class cannot be instantiated.

Example of Abstract class:

abstract class test Abstract Class

{

protected String myString;

public String get String()

{

return myString;

}

public abstract string any Abstract Function();

}

  • It refers to the ability to make a class abstract in OOP.
  • It helps to reduce the complexity and also improves the maintainability of the system.

In java we use interface so that we can achieve fully abstraction because through abstract class we can't achieve full abstraction.

An interface can be extended by another interface in Java. 

The code for the same would be like as shown below: 

// this interface extends from the Body interface:

public interface FourLegs extends Body 

{

public void walkWithFourLegs( );

}

No, we cannot declare abstract method as private.

When we declared any class with "abstract" keyword is known as abstract class. In abstract class we can keep abstract method (without body) and non-abstract method (with body).

For example:

abstract class Demo

{

abstract void show();//abstract method

void show(){}//non-abstract method

}

java.util. Comparator

java.util.Comparator: compares some other classes instances.

java.lang. Comparable

java.lang.Comparable: compares another object with itself.

In java, Abstraction me show functionality and hide complexity or internal details or hide implementation details to the user is known as abstraction in java.

For example:

The best example in the world of abstraction is ATM machine where we can withdraw or trfer money easily but how it happens. We don't know. We don't know internal details or implementation details.

  • An abstract class may have many instance methods which sport default behavior. 
  • On the other hand, an interface cannot implement any default behaviour. 
  • However, it can declare different constants and instance methods. 
  • Whereas an interface has all the public members, an abstract class contains only class members like private, protected and so on.

It will throw compile-time error. We have to override all the abstract method in sub-class. If you do not want to override all the abstract method in sub-class then you have to make your sub-class as abstract class.

In Java an interface just defines the methods and not implement them. Interface can include constants.

A class that implements the interfaces is bound to implement all the methods defined in an interface.

Example of Interface :

public interface sample Interface 

{

public void function One();

public long CONSTANT_ONE = 1000;

}

Yes, we can define abstract class without abstract method.