300+ [LATEST] Java Abstraction Interview Questions and Answers

Q1. Can We Create Instance Of Interface?

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

Q2. When Is An Abstract Method Used?

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.

Q3. What Is A Marker Interface?

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.

Q4. What Is Interface In Java?

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

Q5. Can We Declare Abstract Method In Non-abstract Class?

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.

Q6. In Which Kind Of Situation Would An Interface Be Extended By Another Interface?

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.

Q7. Can An Inner Class Be Built In An 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 “);

}

}

}

Q8. Can We Declare Abstract Method As Final?

No, we cannot use final keyword with abstract class.

Q9. How To Achieve Abstraction In Java?

There are two ways in java we can achieve abstraction:

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

Q10. Can We Use Public, Protected And Default Modifiers With Abstract Method?

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

Q11. Can We Declare Abstract Method As Static?

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

Q12. Can We Create Instance Of Abstract Class?

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

Q13. What Is 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.

Q14. How To Define An Abstract Class?

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();

}

Q15. What Is Abstraction?

  • 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.

Q16. Why We Use Interface In Java?

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

Q17. Can An Interface Be Extended By Another Interface In Java?

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( );

}

Q18. Can We Declare Abstract Method As Private?

No, we cannot declare abstract method as private.

Q19. What Is Abstract Class In Java?

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

}

Q20. How Do We Use Comparator And Comparable Interfaces?

java.util. Comparator

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

java.lang. Comparable

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

Q21. What Is Abstraction In Java?

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.

Q22. Differentiate An Interface And An Abstract Class?

  • 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.

Q23. What Will Happen If We Do Not Override All The Abstract Methods In Sub-class?

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.

Q24. How Can We Define An Interface?

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;

}

Q25. Can We Define Abstract Class Without Abstract Method?

Yes, we can define abstract class without abstract method.