Skip to main content

Abstract Class and Interface Interview Questions And Answers - Programming Blog

abstract class and Interface interview questions and answers in java

abstract class and interface interview questions and answers

Lets see most asked interview questions and answers about interface and abstract class in java. 

1. What is Interface in Java?

Ans :-
  • Interface is completely Abstract Class.
  • From java 8, we can use default method in interface.
  • Interface is just declarations of methods. Interface does not provide implementation of methods.
  • We can implement interface and whoever class implements that interface it must provide implementation of that interface.
  • Any class implements as many interface they want.
  • Interface also can extends another interface. but one interface can not implements another interface.

Learn more about Interface with Examples :-

2. What is Abstract class in Java?

Ans :- 
  • Any class defined with abstract keyword is called abstract class.
  • Abstract class contains abstract method as well as non abstract methods also.
  • Abstract class can not instantiated means we can not create object of abstract class. If we try to instantiated then it gives "Cannot instantiate type 'AbstractClassName'" gives error.
  • We can extends the abstract class but sub class must provide implementation of parent abstract class methods. If subclass does not want to implement then subclass must be declared abstract also.

Want to learn more about abstract class with EXAMPLES :-

 

3. What is difference between Abstract class and Interface?

Ans:- 

If you want to learn more about difference between abstraction and interface in java here is another articles link :- 

 

4. How to choose when to use Interface and Abstract class in Java?

Ans:-

Choose Abstract class for following cases:-

  1. When you want to your class have abstract as well as non abstract methods.
  2. When you want to use methods that contains datatype other than public like private, protected.
  3. when you want to use variables other than public static final.
  4. Share code among several closely related classes. It establishes is a relation.
  5. For instance, maybe you want to make sure to initialize some variables in a class to perform logic in a helper method which all derived classes can use, choosing to go with an abstract class is the right choice.

Choose Interface for following cases :-

  1. When you want to extends another class. because we can extends only one class in java and implement interfaces as many as we want.
  2. When you want to achieve multiple inheritance in java.
  3. To link unrelated classes with has a capabilities.
  4. When you want to achieve more abstraction then Abstract class.

5.  Can Abstract class contains Constructor in Java?

Ans :- 

Yes, abstract class contains Constructor in java.

If you does not provides any constructor in java then compiler automatically generates Default constructor. and it is also applicable for abstract class.

We can not create object of abstract classes, we can only extend abstract classes, so we have to use abstract class constructor from subclass's constructor.

Lets see example

Can Abstract class contains Constructor in Java?
 
Can Abstract class contains Constructor in Java?

If you want to learn more about it check out stackoverflow and quora link :-

6.  Can Interface contains Constructor in Java? 

Ans :-

No, Interface can not contains constructor. 

If you try to create constructor in java then it gives following error :-
 'Interfaces cannot have constructors'
 
Check out more about this:-
 

7. Can  we declare Abstract class as final in Java? OR Can Abstract class is final in Java?

Ans :-

No, We can not declare abstract class as final in java     

Because abstract is incomplete. Abstract class contains abstract method and we have to implements in the sub class. means we need to extends abstract class.

If we declare abstract class as final we can not extends it because final class can not be extended.

If you want to learn more about final class link is here:-

If we try to declare abstract class as final it gives you error :-

"The class 'AbstractClassName' can be either abstract or final, not both"

 

8. Can we declare Interface as final in Java? OR Can Interface is final in Java?

Ans :-

No, We can not declare interface as final in java.

Like in abstract class we have to extends that class to implement abstract methods, in interface we have implement interface. so it won't be final.


9. Can abstract class implements interface in Java?

Ans :-

Yes, abstract class can also implements interface java.

Now you have one questions. Then how we can implements interface methods?

You can implements in abstract class or you can extends abstract class and then implements in sub class.

Lets see example of that.

Can abstract class implements interface in Java?
Can abstract class implements interface in Java?
Can abstract class implements interface in Java?

In above example first we created interface, then implements by abstract class. 

That abstract class extended by sub class and implemented abstractMethodInInterface() method.


10. Can Interface implements another interface in Java?

Ans :-

No,  Interface can not implements another Interface in java.

 

11. Can Interface extends another interface in Java?

Ans :-

Yes, Interface can extends another interface in java.

 

12. What options are available for subclass when implements interface or extends abstract class?

Ans :-

There are only two options for subclass who extends abstract class or implements interface.

  1. Implements all unimplemented methods
  2. Make subclass abstract also.

 

13. Can we add define abstract method in non abstract class?

Ans :-

No, non abstract class does not contains abstract methods. if we try to define abstract method in non abstract class then it gives error.

See below image.

Can we add define abstract method in non abstract class?

14. Can abstract class contains Static method in Java?

Ans :-

Yes, we can define static method in abstract class in java. See example below.

abstract class Demo {

    static void staticMethod() { }


15. Why can not we declare abstract method as static in Java?

Ans :-

No, In java we can not declare abstract method with static keyword. In java, if we declare abstract method then we have to implement abstract method into subclass. And in java, Static method can not overridden by subclass. So In java, we cannot declare method as static and abstract at same time.

 

Save this article because i add more and more questions and answers in upcoming days.

Other article you may like.
Top FREE applications and Websites to learn Coding and Programming.

 

Comments

Popular posts from this blog

Plus Minus HackerRank Solution in Java | Programming Blog

Java Solution for HackerRank Plus Minus Problem Given an array of integers, calculate the ratios of its elements that are positive , negative , and zero . Print the decimal value of each fraction on a new line with 6 places after the decimal. Example 1 : array = [1, 1, 0, -1, -1] There are N = 5 elements, two positive, two negative and one zero. Their ratios are 2/5 = 0.400000, 2/5 = 0.400000 and 1/5 = 0.200000. Results are printed as:  0.400000 0.400000 0.200000 proportion of positive values proportion of negative values proportion of zeros Example 2 : array = [-4, 3, -9, 0, 4, 1]  There are 3 positive numbers, 2 negative numbers, and 1 zero in array. Following is answer : 3/6 = 0.500000 2/6 = 0.333333 1/6 = 0.166667 Lets see solution Solution 1 import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.st

Flipping the Matrix HackerRank Solution in Java with Explanation

Java Solution for Flipping the Matrix | Find Highest Sum of Upper-Left Quadrant of Matrix Problem Description : Sean invented a game involving a 2n * 2n matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times. The goal of the game is to maximize the sum of the elements in the n *n submatrix located in the upper-left quadrant of the matrix. Given the initial configurations for q matrices, help Sean reverse the rows and columns of each matrix in the best possible way so that the sum of the elements in the matrix's upper-left quadrant is maximal.  Input : matrix = [[1, 2], [3, 4]] Output : 4 Input : matrix = [[112, 42, 83, 119], [56, 125, 56, 49], [15, 78, 101, 43], [62, 98, 114, 108]] Output : 119 + 114 + 56 + 125 = 414 Full Problem Description : Flipping the Matrix Problem Description   Here we can find solution using following pattern, So simply we have to find Max of same number of box like (1,1,1,1). And last