← Back to Core Java Course | Chapter 9: OOP Advanced | Lesson 3 of 8

Java की Abstract Classes

एक abstract class एक अधूरा blueprint है जो extend होने के लिए है, एक sketch जिसे दूसरों को पूरा करना चाहिए। इसमें कुछ finished parts और कुछ blanks हो सकते हैं।
Syntax
java
abstract class ClassName {
    abstract returnType methodName();  // no body
    returnType concreteMethod() { }
}

class Child extends ClassName {
    returnType methodName() { }
}

What is an Abstract Class?

एक abstract class abstract keyword से declared है और इसे कभी सीधे new से instantiate नहीं किया जा सकता — यह विशुद्ध रूप से एक partial blueprint के रूप में काम करती है जिसके ऊपर concrete subclasses बनती हैं।

उदाहरण: What is an Abstract Class?

java
abstract class Shape {
}
public class Main {
	public static void main(String[] args) {
		// Shape s = new Shape(); // would not compile: cannot instantiate abstract class
		System.out.println("Abstract class cannot be instantiated");
	}
}

Defining Abstract Methods

एक abstract method बिना किसी method body के declared है, बस एक semicolon पर खत्म होने वाला एक signature; यह हर concrete subclass को अपना specific implementation supply करने पर मजबूर करने के लिए मौजूद है, गारंटी देते हुए कि वह method hierarchy में कहीं मौजूद होगा।

उदाहरण: Defining Abstract Methods

java
abstract class Shape {
	abstract double area(); // no body, just a signature
}
public class Main {
	public static void main(String[] args) {
		System.out.println("Abstract method declared");
	}
}

Implementing Abstract Methods

कोई भी concrete (non-abstract) subclass जो एक abstract class extend करती है उसे इसके inherited हर abstract method implement करना चाहिए, नहीं तो subclass को खुद भी abstract declare करना होगा, obligation को chain में आगे deferring करते हुए।

उदाहरण: Implementing Abstract Methods

java
abstract class Shape {
	abstract double area();
}
class Square extends Shape {
	double side = 4;
	double area() { // must implement, or Square would need to be abstract too
		return side * side;
	}
}
public class Main {
	public static void main(String[] args) {
		System.out.println(new Square().area());
	}
}

Combining Abstract and Regular Methods

एक pure interface के विपरीत, एक abstract class abstract methods (कोई body नहीं, override होना चाहिए) को fully implemented concrete methods (एक body के साथ, जैसा है वैसा inherited) के साथ freely mix कर सकती है, आपको enforced contracts के साथ असली working code share करने की flexibility देते हुए।

उदाहरण: Combining Abstract and Regular Methods

java
abstract class Shape {
	abstract double area();
	void describe() { // concrete method with a body
		System.out.println("Area is " + area());
	}
}
class Square extends Shape {
	double side = 4;
	double area() { return side * side; }
}
public class Main {
	public static void main(String[] args) {
		new Square().describe();
	}
}

Abstract Classes with Constructors

Abstract classes constructors define कर सकती हैं, जो जब भी एक concrete subclass object बनता है अपने आप चलते हैं (normal parent-constructor-runs-first sequence के हिस्से के रूप में), भले ही abstract class खुद कभी अकेले instantiate न हो सके।

उदाहरण: Abstract Classes with Constructors

java
abstract class Shape {
	String name;
	Shape(String name) {
		this.name = name; // runs when a concrete subclass is created
	}
}
class Square extends Shape {
	Square() {
		super("Square");
	}
}
public class Main {
	public static void main(String[] args) {
		System.out.println(new Square().name);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक abstract class के लिए new Shape() से एक object बनाना, जो एक compile error है।
  2. एक concrete subclass में सभी abstract methods implement न करना, जो एक compile error है।
  3. एक abstract method को private या final मार्क करना, जो allowed नहीं।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.