Java Abstract Class In Hindi

📔 : Java 🔗

abstract class को incomplete class भी कहते हैं , ऐसी class में हम सिर्फ अपने logic का architecture तैयार करते हैं उसे implement किसी और class में करते हैं। अगर class में एक भी method as a abstract defined हुआ तो class abstract ही होगी। abstract class और methods को abstract keyword का use करके define किया जाता है।


Abstract class को समझने से पहले हमें Abstraction को समझना होगा।

java Abstraction In Hindi

end user से implementation details को छुपाना और सिर्फ functionality दिखाना ही Abstraction कहलाता है। means किसी functionality की internal details hide करना और सिर्फ working parts को ही show करना ही Abstraction कहते हैं। Abstraction का main purpose security enhance करना है।


Suppose कीजिये आजकल आप एक दूसरे से communicate करने के लिए chat करते हैं , आप just किसी message को लिखकर enter करते हैं और वह message दूसरे user को show हो जाता है। यहां आपको नहीं पता है कि internally ,message को send करने के लिए क्या process follow की गयी है और न ही यह जानते हैं कि implementation कैसा होगा।

Ways to achieve Abstraction In Java

Java में Abstraction achieve करने के दो तरीके हैं -

  1. Abstract class (0 to 100%)
  2. Interface (100%)

इस topic में आप Abstract class के बारे में पढ़ेंगे।

Abstract Class

java में किसी class को abstract define करने के लिए abstract keyword का use किया जाता है। abstract class में abstract और non-abstract methods हो सकते हैं। Abstraction को achieve करने के लिए आपको इस class को inherit करना पड़ेगा जिसमे define किये गए abstract methods का implementation provide करना होगा।

Java Abstract Class Example

File : Bike.java

CopyFullscreenClose FullscreenRun
// abstract class.
abstract class Vehicle {  
   // abstract method.
  abstract void run();
}  

// main class.
public class Bike extends Vehicle {
  // provide implementation for run() method.
  void run() {
    System.out.println("Bike is running");
  } 
  
  public static void main(String args[]){  
    Bike b = new Bike();  
    b.run(); 
  }
}
Output
javac Bike.java
java Bike
Bike is running

Important Points About Abstract Class In Java

Abstract class के बारे में कुछ important points जिन्हे ध्यान में रखना बहुत ही जरूरी है।

  • abstract class को abstract keyword के साथ define किया जाता है।

  • किसी भी abstract class में abstract और non-abstract दोनों तरह के methods हो सकते हैं।

  • चूँकि abstract class एक incomplete class होती है इसलिए इसका object नहीं बनाया जा सकता है।

  • हालाँकि abstract class में आप constructors define कर सकते हैं, और static methods भी define कर सकते हैं।

  • abstract class में non static methods को ही abstract method define कर सकते हैं static methods को abstract method define नहीं कर सकते हैं।

  • abstract class में final methods भी define कर सकते हैं , अगर आप चाहते हैं कि इसकी sub class उस method की body change न करे।

चूंकि abstract class में सभी तरह के methods define कर सकते हैं इसलिए Abstract class का use करके 100% Abstraction achieve नहीं का जा सकती है।

Let's see another example

File : Bike.java

CopyFullscreenClose FullscreenRun
// abstract class.
abstract class Vehicle {  
  // constructor.
  Vehicle(){
   	System.out.println("Abstract class constructor");
  }
   
   // abstract method.
  abstract void run();
  
  // non-abstract method.
  void nonAbstractMethod() {
    System.out.println("Abstract class : nonAbstractMethod()");
  }
  
  // static method.
  static void staticMethod() {
    System.out.println("Abstract class : staticMethod()");
  }
  
  // final method.
  final void finalMethod() {
    System.out.println("Abstract class : finalMethod()");
  }
}  

// main class.
class Bike extends Vehicle {
  void run() {
    System.out.println("Bike is running");
  } 
  
  public static void main(String args[]){  
    Bike b = new Bike();
    b.run();
    b.nonAbstractMethod();
    b.finalMethod();
    Vehicle.staticMethod(); // class static method.
  }
}
Output
javac Bike.java
java Bike
Abstract class constructor
Bike is running
Abstract class : nonAbstractMethod()
Abstract class : finalMethod()
Abstract class : staticMethod()

I Hope, अब आप Java में Abstract class के बारे में अच्छे से समझ गए होंगे।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers