The last article, I wrote about Important Java Polymorphism interview Questions and in this article, I am going to share Java Abstract Class and Interface Interview Questions and Answers frequently asked on core Java Interview. Abstract class and interface are the most important topics for the interview purpose. You can expect many questions from this topic in the interview. This topic is one of the favorites of the interviewer.
Q1. What do you understand by Abstract Class in java?
Ans: An abstract class is a class which can have a concrete and abstract method. If you are not writing an implementation of any method then that method should be defined with abstract keyword. A class contains one or more abstract method than that class must be declared with an abstract keyword. A class is abstract then it is not necessary that it must have an abstract method. We can write almost all the member of a class inside an abstract class.
Q2. Can we create the object of the Abstract class?
Ans: An object of an Abstract class cannot be created.
Q3. What the class should do. If any class is extending any abstract class?
Ans: If a class is extending an abstract class, then that class must do the following things:
• A subclass should override all the abstract method of the superclass.
• If the subclass is not overriding all the abstract method then subclass must be declared as abstract.
Q4. What are the Illegal combinations for an abstract class in java?
Ans: we cannot write this combination for the abstract class with abstract keyword.
- static abstract method()
- final abstract method()
- private abstract method()
Q5. What is an Interface of the Java?
Ans: An interface is a special type of class which contains only two types of members an abstract method and public static final variable. In order to declare an interface, we need to use the ‘interface’ keyword.
Q6. What are the default members of the interface?
Ans: Any member which you are declaring inside interface will be by default:
- If it is method then it will be a public abstract method
- For a variable, it will be public static final variable
Q7. How to implement an interface in Java?
Ans: If any class trying to use any interface then they need to use ‘implements keyword. When a class is implementing an interface then that subclass has to do the following things:
• A subclass must override all the method of an interface if not then subclass must be declared as an abstract.
Note: Interface cannot be instantiated i.e, create an object but reference can be created.
Q8. Can we write an interface without any members?
Ans: Yes we can write, Interface without any member are called as a marker interface. A marker interface is used to make the eligibility of class. There are many built-in markers
interface available like
• Java.lang.clonable interface
• Random interface
• Serializable interface
Q9. Can we write a custom marker interface?
Ans: Yes, we need to write the implementation of a marker interface by the reflection mechanism.
Q10.List out the difference between abstract class and interface?
|If we don’t know anything about implementation just we have requirement specification then we should go for an interface||If we are talking about implementation but not completely(partial implementation) then we should go for abstract class|
|Inside interface, every method is alway public and abstract whether we are declaring it or not it is considered as 100 percent pure abstract class||Every method inside an abstract class doesn’t need to be public and abstract. In addition to the abstract method we can take concrete methods also|
|We can’t declare an interface method with the following modifiers.|
Private, protected, final, static, synchronized, native, strictf
|There are no restriction on abstract class methods modifiers|
|Every variable present inside interface is always public static final whether we are declaring or not.||The variable present inside an abstract class need not be public static final.|
|We cannot declare interface variable with the following modifiers- private, protected, transient, volatile||There is no restriction on the abstract class variable modifier.|
|In an interface, it is compulsory to initialize the variable at the time of the declaration. Otherwise, we will get a compile-time error.||For abstract class variable, it is not required to perform initialization at the time of the declaration of the variable.|
|Inside interface, we can’t declare instance and static blocks otherwise will get a compile-time error.||Inside the abstract class, we can declare an instance and static blocks.|
|Inside interface we can,t declare a constructor.||Inside an abstract class, we can declare constructor, which will be executed at the time of child object creation.|
Q11. Can a class extend only one class?
Q12. Can a class extend one or more interface?
Q13. Can an Interface extend one or more interface?
Q14. Can an Interface extend one or more class?
Q15. Can a class implements one or more class?
Q16. Can a class implements one or more interface?
Q17. Can an Interface implement one or more class?
Q18. Can an Interface implement one or more interface?
Q20. Can We Define Abstract Class Without Abstract Method?
Ans: Yes, we can define an abstract class without abstract method.
Q21. Can We Declare Abstract Method In Non-abstract Class?
Ans: No, we can’t declare an abstract method in a non-abstract class.
abstract void showXadmin();
Above example will throw compile time error.
Q22. When Is An Abstract Method Used?
Ans: 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 the child class.
Q23. Why We Use Interface In Java?
Ans: In java, we use interface so that we can achieve fully abstraction because through abstract class we can’t achieve full abstraction.
Q24. Can We Use Public, Protected And Default Modifiers With Abstract Method?
Ans: Yes, we can use public, protected and default modifiers with an abstract method.
Q25. Can we write an inner class in an interface in java?
Ans: Yes, an inner class can be written in the interface.
Q26. Can we declare abstract methods as synchronized in java?
Ans: No, abstract methods can not be declared as synchronized. But methods which override abstract methods can be declared as synchronized.
Q27. Can we declare local inner class as abstract in Java?
Ans: Yes. A local inner class can be abstract in java.
Q28. Can an abstract method declaration include throws clause?
Ans: Yes. Abstract methods can be declared with a throws clause.
Q29 Can we use “abstract” keyword with a constructor, Instance Initialization Block, and Static Initialization Block?
Ans: No. Constructor, Static Initialization Block, Instance Initialization Block, and variables can not be abstract.
Q30. Why final and abstract keyword can not be used at a time?
Ans: Because the final and abstract are totally opposite in nature. A final class or method can not be modified further whereas abstract class or method must be modified further. “final” keyword is used to denote that a class or method does not need further improvements. “abstract” keyword is used to denote that a class or method needs further improvements.
This article will help you with Important Java Abstract Class and Interface Interview Questions to the freshers and as well as to the experienced candidates. Read the article carefully, I believe this article is going to help you a lot to tackle the question fired by the interviewer. Stay tuned! for the more interview question. Happy Learning! All the best!
Recommended interview questions:
- Core Java Interview Questions on Constructors
- Top 30+ Important Interview Questions On Inheritance
- Important Java Polymorphism Interview Questions