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

Sales by Match HackerRank Solution | Java Solution

HackerRank Sales by Match problem solution in Java   Problem Description : Alex works at a clothing store. There is a large pile of socks that must be paired by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. For example, there are n=7 socks with colors socks = [1,2,1,2,1,3,2]. There is one pair of color 1 and one of color 2 . There are three odd socks left, one of each color. The number of pairs is 2 .   Example 1 : Input : n = 6 arr = [1, 2, 3, 4, 5, 6] Output : 0 Explanation : We have 6 socks with all different colors, So print 0. Example 2 : Input : n = 10 arr = [1, 2, 3, 4, 1, 4, 2, 7, 9, 9] Output : 4 Explanation : We have 10 socks. There is pair of color 1, 2, 4 and 9, So print 4. This problem easily solved by HashMap . Store all pair of socks one by one in Map and check if any pair is present in Map or not. If pair is present then increment ans variable by 1 ...

Queen's Attack II HackerRank Solution in Java with Explanation

Queen's Attack II Problem's Solution in Java (Chessboard Problem)   Problem Description : You will be given a square chess board with one queen and a number of obstacles placed on it. Determine how many squares the queen can attack.  A queen is standing on an n * n chessboard. The chess board's rows are numbered from 1 to n, going from bottom to top. Its columns are numbered from 1 to n, going from left to right. Each square is referenced by a tuple, (r, c), describing the row r and column c, where the square is located. The queen is standing at position (r_q, c_q). In a single move, queen can attack any square in any of the eight directions The queen can move: Horizontally (left, right) Vertically (up, down) Diagonally (four directions: up-left, up-right, down-left, down-right) The queen can move any number of squares in any of these directions, but it cannot move through obstacles. Input Format : n : The size of the chessboard ( n x n ). k : The number of obstacles...