abstract class and Interface interview questions and answers in java
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:-
- When you want to your class have abstract as well as non abstract methods.
- When you want to use methods that contains datatype other than public like private, protected.
- when you want to use variables other than public static final.
- Share code among several closely related classes. It establishes is a relation.
- 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 :-
- When you want to extends another class. because we can extends only one class in java and implement interfaces as many as we want.
- When you want to achieve multiple inheritance in java.
- To link unrelated classes with has a capabilities.
- When you want to achieve more abstraction then Abstract class.
Read more about this :-
- https://www.mindprod.com/jgloss/interfacevsabstract.html#TABLE
- https://stackoverflow.com/questions/16781329/when-do-i-have-to-use-interfaces-instead-of-abstract-classes
- https://javabypatel.blogspot.com/2017/07/abstract-class-vs-interface-in-java.html
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
If you want to learn more about it check out stackoverflow and quora link :-
- https://stackoverflow.com/questions/260666/can-an-abstract-class-have-a-constructor
- https://www.quora.com/Why-interfaces-in-Java-dont-have-constructor-while-abstract-classes-do-have
6. Can Interface contains Constructor in Java?
No, Interface can not contains constructor.
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.
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.
- Implements all unimplemented methods
- 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.
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.
- Double Brace Initialization in Java
- BiFunction in Java
- Lambda Expression in Java
- Functional Interface in Java
- Abstraction in Java
- Static in Java (Static class, Method, Variable and Block)
- Final keyword in java
- Difference between Final, Finally and Finalize in Java
Comments
Post a Comment