Câu hỏi phỏng vấn Java
Câu hỏi

What is the difference between an Interface and an Abstract class?

Câu trả lời

The difference between an interface and an abstract class in Java is fundamental to understanding Java's approach to abstraction and polymorphism, and it's a common topic in programming interviews.

Key Differences

  1. Abstraction Level:

    • Abstract Class: Can achieve partial abstraction (0 to 100%). It can have both abstract methods (which do not have a body) and concrete methods (which have a body).
    • Interface: Achieves full abstraction (100%). Prior to Java 8, interfaces could only have abstract methods (no method bodies). From Java 8 onwards, interfaces can also have default and static methods with a body[1][2][3].
  2. Method Implementation:

    • Abstract Class: Can provide a default behavior and can also declare method implementations.
    • Interface: Originally, interfaces could not provide method implementations. However, since Java 8, interfaces can provide implementations through default and static methods, but the primary intent is still to define methods that must be implemented by the classes that use the interface[1][2][3].
  3. Constructor:

    • Abstract Class: Can have constructors.
    • Interface: Cannot have constructors because interfaces are not meant to be instantiated on their own[2][3].
  4. Instance Variables:

    • Abstract Class: Can have instance variables that can be non-final and non-static.
    • Interface: All variables are implicitly public, static, and final (constants)[1][2][3].
  5. Inheritance vs. Implementation:

    • Abstract Class: A class can extend only one abstract class, which means Java does not support multiple class inheritance.
    • Interface: A class can implement multiple interfaces. This feature helps to overcome the limitation of single inheritance in Java by allowing multiple types to be inherited through interfaces[1][2][3].
  6. Access Specifiers:

    • Abstract Class: Members can have any access specifiers (public, protected, private).
    • Interface: All members are public by defau...
junior

junior

Gợi ý câu hỏi phỏng vấn

middle

What’s the difference between Enumeration and Iterator interfaces?

junior

What is the design pattern that Java uses for all Swing components?

middle

What are the steps involved to make work a RMI program?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào