✏️ Explanatory Question

What is the difference between abstract class and interface in X++ Language

👁 16 Views
📘 Detailed Answer
🟢 Easy
No previous question
No next question
💡

Answer with Explanation

Feature Abstract Class Interface
Definition A class declared with abstract keyword. A contract defined with interface keyword.
Implementation Can have both abstract methods (no body) and concrete methods (with body). Can only declare method signatures (no method body).
Variables Can have fields (member variables). Cannot have fields, only methods.
Access Modifiers Methods can have different modifiers (public, protected, private). All methods are implicitly public.
Inheritance A class can inherit (extend) only one abstract class. A class can implement multiple interfaces.
Constructors Abstract classes can have constructors. Interfaces cannot have constructors.
Use Case Use when you want to provide base functionality + enforce certain methods. Use when you want to enforce a contract only, with no default implementation.

🔑 Abstract Class → “Provide base functionality + enforce certain methods”

This means:

  1. Provide base functionality → You can write some methods with actual logic inside the abstract class. All child classes will inherit this logic (so you don’t repeat code everywhere).

  2. Enforce certain methods → You can also declare abstract methods (no logic, just a declaration). All child classes must implement these methods in their own way.


When we say “contract”, it means:
👉 You are forcing any class that implements the interface to provide those methods.
👉 But the interface itself does not contain the logic (it only declares the methods).


No previous question
No next question