Abstraction in Java जावा में एब्सट्रेक्शन, Abstract Class in Java जावा में एब्सट्रेक्ट क्लास

Abstraction in Java is a process of hiding unnecessary implementation details and showing only essential features of an object. It helps in focusing on what an object does rather than how it does it.

जावा में एब्सट्रेक्शन वह प्रक्रिया है जिसमें किसी वस्तु के केवल आवश्यक गुणों को प्रदर्शित किया जाता है और अनावश्यक विवरणों को छुपाया जाता है। इससे हम यह जान पाते हैं कि कोई वस्तु क्या करती है, न कि कैसे करती है।

Java provides two ways to achieve abstraction:
जावा में एब्सट्रेक्शन प्राप्त करने के दो मुख्य तरीके हैं:
a) Using Abstract Classes
b) Using Interfaces


Abstract Class in Java

An abstract class is declared using the abstract keyword. It can contain both abstract methods (without body) and concrete methods (with body).

एब्सट्रेक्ट क्लास वह होती है जिसे abstract कीवर्ड के साथ घोषित किया जाता है। इसमें सामान्य (concrete) और एब्सट्रेक्ट दोनों प्रकार की मेथड्स हो सकती हैं।

You can achieve 0% to 100% abstraction using an abstract class, depending on how many methods are abstract. However, objects of abstract classes cannot be created directly.
एब्सट्रेक्ट क्लास का उपयोग करके आप 0% से 100% तक एब्सट्रेक्शन प्राप्त कर सकते हैं। लेकिन इस क्लास का ऑब्जेक्ट सीधे नहीं बनाया जा सकता है।


Abstract Method

An abstract method is a method that has no body. It is declared using the abstract keyword and ends with a semicolon ;.
Abstract methods must be overridden in a subclass that extends the abstract class.

एब्सट्रेक्ट मेथड वह होती है जिसमें बॉडी नहीं होती। इसे abstract कीवर्ड के साथ घोषित किया जाता है और अंत में ; से समाप्त किया जाता है। इसे सब-क्लास में ओवरराइड करना आवश्यक होता है।


Syntax:

abstract class ClassName {
abstract return_type methodName();
return_type anotherMethod() {
// Method body
}
}

Example:

abstract class Animal {
abstract void breath();
void eat() {
System.out.println("I am eating as Animal...");
}
}
class Dog extends Animal {
void breath() {
System.out.println("Breathing as Dog...");
}
}
class AbstractTest {
public static void main(String args[]) {
Dog d = new Dog();
d.breath();
d.eat();
}
}

In the above program, Animal is an abstract class. The method breath() is abstract and is overridden in the Dog class.

इस प्रोग्राम में Animal एक एब्सट्रेक्ट क्लास है जिसमें breath() एब्सट्रेक्ट मेथड है और इसे Dog क्लास में ओवरराइड किया गया है।


✅ Advantages of Abstraction in Java

  1. Encourages code reuse. कोड के पुनः उपयोग को बढ़ावा देता है।

  2. Maintains privacy and internal logic can be changed without affecting the user. प्रोग्राम की गोपनीयता बनाए रखता है और आंतरिक कोड को बिना यूज़र को बताए बदला जा सकता है।

  3. Supports loose coupling between classes. लूज़ कपल्ड क्लास बनाने में सहायक होता है।

Comments

Popular posts from this blog

What is a Web Browser? वेब ब्राउज़र क्या है?

Java's Support System जावा का सहयोगी तंत्र

The Internet and Java इंटरनेट और जावा का सम्बन्ध