B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Abstract Class in Java | Login Technologies

Abstract Class in Java

What is an Abstract Class?

An abstract class in Java is a class that cannot be instantiated. It can have both abstract methods (without body) and concrete methods (with body). Subclasses must implement all abstract methods.

Java में abstract class वो class होती है जिसे हम directly object बनाकर use नहीं कर सकते। इसमें abstract methods (जिनका body नहीं होता) और concrete methods दोनों हो सकते हैं। Subclasses को abstract methods implement करना जरूरी है।

When to Use an Abstract Class?

- When you want to define a common template for all subclasses.
- When you want to enforce certain methods to be implemented by subclasses.
- When you want to share common code but still leave parts for subclasses.

- जब आप subclasses के लिए एक common template बनाना चाहते हैं।
- जब आप subclasses से कुछ जरूरी methods implement करवाना चाहते हैं।
- जब आप कुछ common code share करना चाहते हैं और बाकी subclasses पर छोड़ना चाहते हैं।

Advantages of Abstract Class

  • Supports abstraction by hiding complexity.
  • Enforces standardization across subclasses.
  • Enables code reusability.
  • Helps design extensible systems.
  • Complexity को छुपाकर abstraction को support करता है।
  • Subclasses में standardization enforce करता है।
  • Code को reuse करना आसान बनाता है।
  • Extensible systems design करने में मदद करता है।

What is an Abstract Method?

An abstract method is a method declared without an implementation (no body). It only has the method signature and ends with a semicolon. Subclasses must provide its implementation. Abstract methods are used to enforce that all subclasses follow a standard contract but can have their own specific behavior.

Abstract method वो method होता है जिसमें कोई body नहीं होती, सिर्फ declaration होती है (semicolon के साथ खत्म होता है)। Subclasses को इसका implementation देना जरूरी होता है। Abstract methods का use इसलिए होता है ताकि सभी subclasses एक standard method को implement करें लेकिन अपने तरीके से behavior दें।

Example: Employee Class using Abstract Class

In this example, Employee is an abstract class with abstract method work(). Subclasses Manager and Developer provide their own implementation of work().

इस उदाहरण में Employee एक abstract class है जिसमें abstract method work() है। Subclasses Manager और Developer इस method को अपने हिसाब से implement करते हैं।

abstract class Employee {
    abstract void work();
}

class Manager extends Employee {
    void work() {
        System.out.println("Manager is planning and coordinating work.");
    }
}

class Developer extends Employee {
    void work() {
        System.out.println("Developer is writing code and fixing bugs.");
    }
}

public class AbstractClassDemo {
    public static void main(String[] args) {
        Employee e;

        e = new Manager();
        e.work();

        e = new Developer();
        e.work();
    }
}

Output:

Manager is planning and coordinating work.
Developer is writing code and fixing bugs.

This demonstrates how an abstract class in Java can enforce a method to be implemented in all subclasses while allowing different implementations.

यह दिखाता है कि Java में abstract class कैसे subclasses से जरूरी method implement करवाती है और अलग-अलग implementation की सुविधा देती है।