DAY : 27 Abstract and Interface in Java – Simplified

Abstraction 1.Abstraction in Java is a fundamental concept in object-oriented programming (OOP) that hides implementation details and only shows essential features. 2.if at least one method is abstract in a class, then the entire class should be abstract Key features of abstraction: Abstraction hides the complex details and shows only essential features. Abstract classes may have methods without implementation and must be implemented by subclasses. By abstracting functionality, changes in the implementation do not affect the code that depends on the abstraction. Important rules for abstract methods are mentioned below: Any class that contains one or more abstract methods must also be declared abstract. If a class contains an abstract method it needs to be abstract and vice versa is not true. The following are various illegal combinations of other modifiers for methods with respect to abstract modifiers: final abstract native abstract synchronized abstract static abstract private abstract strictfp How to Achieve Abstraction in Java? Java provides two ways to implement abstraction, which are listed below: Abstract Classes (Partial Abstraction) Interface (100% Abstraction) Interface In Java, an interface defines a contract for classes to implement, specifying a set of abstract methods and constants. It's a blueprint for classes to adhere to, ensuring they provide certain functionalities. Interfaces allow for abstraction and enable multiple inheritance in Java. Why use interfaces? Interfaces fulfill two goals: They allow the programmer to be more abstract when referencing objects (for example, var vehicle : Vehicle, can reference any car, truck, etc... anything that is a vehicle (and not care what type it is.) This occurs at "program time".

Apr 17, 2025 - 04:23
 0
DAY : 27 Abstract and Interface in Java – Simplified

Abstraction

1.Abstraction in Java is a fundamental concept in object-oriented programming (OOP) that hides implementation details and only shows essential features.
2.if at least one method is abstract in a class, then the entire class should be abstract

Key features of abstraction:

  1. Abstraction hides the complex details and shows only essential features.
  2. Abstract classes may have methods without implementation and must be implemented by subclasses.
  3. By abstracting functionality, changes in the implementation do not affect the code that depends on the abstraction.

Important rules for abstract methods are mentioned below:

  1. Any class that contains one or more abstract methods must also be declared abstract.
  2. If a class contains an abstract method it needs to be abstract and vice versa is not true.

    The following are various illegal combinations of other modifiers for methods with respect to abstract modifiers:

    1. final
    2. abstract native
    3. abstract synchronized
    4. abstract static
    5. abstract private
    6. abstract strictfp

How to Achieve Abstraction in Java?

Java provides two ways to implement abstraction, which are listed below:

  1. Abstract Classes (Partial Abstraction)
  2. Interface (100% Abstraction)

Interface

In Java, an interface defines a contract for classes to implement, specifying a set of abstract methods and constants. It's a blueprint for classes to adhere to, ensuring they provide certain functionalities. Interfaces allow for abstraction and enable multiple inheritance in Java.

Why use interfaces?

Interfaces fulfill two goals: They allow the programmer to be more abstract when referencing objects (for example, var vehicle : Vehicle, can reference any car, truck, etc... anything that is a vehicle (and not care what type it is.) This occurs at "program time".