🟣 OOP  ·  Lesson 21

Abstraction and Interfaces

Abstraction and Interfaces

What is Abstraction?

Abstraction hides complex details and shows only the essential features. Java achieves it with abstract classes and interfaces.

Interface Example

interface Shape {
    double area();   // no body - abstract
}
class Circle implements Shape {
    double r = 5;
    public double area() { return 3.14 * r * r; }
}

Shape s = new Circle();
System.out.println(s.area());
78.5

Abstract Class vs Interface

Abstract ClassInterface
can have method bodiesmainly abstract methods
extends (one)implements (many)

Summary

  • Abstraction hides details, shows essentials.
  • Use abstract classes (extends) or interfaces (implements, many allowed).

Abstraction क्या है?

Abstraction जटिल details छुपाकर सिर्फ ज़रूरी features दिखाता है। Java इसे abstract classes और interfaces से करता है।

Interface Example

interface Shape {
    double area();   // बिना body - abstract
}
class Circle implements Shape {
    double r = 5;
    public double area() { return 3.14 * r * r; }
}

Shape s = new Circle();
System.out.println(s.area());
78.5

Abstract Class बनाम Interface

Abstract ClassInterface
method bodies हो सकती हैंमुख्यतः abstract methods
extends (एक)implements (कई)

सारांश

  • Abstraction details छुपाता है, essentials दिखाता है।
  • abstract classes (extends) या interfaces (implements, कई) use करें।
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n