Java की Abstract Classes
In this page:
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?
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");
}
}
Login to try C/C++/Java/PHP code in the editor
Defining Abstract Methods
एक abstract method बिना किसी method body के declared है, बस एक semicolon पर खत्म होने वाला एक signature; यह हर concrete subclass को अपना specific implementation supply करने पर मजबूर करने के लिए मौजूद है, गारंटी देते हुए कि वह method hierarchy में कहीं मौजूद होगा।
उदाहरण: Defining Abstract Methods
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");
}
}
Login to try C/C++/Java/PHP code in the editor
Implementing Abstract Methods
कोई भी concrete (non-abstract) subclass जो एक abstract class extend करती है उसे इसके inherited हर abstract method implement करना चाहिए, नहीं तो subclass को खुद भी abstract declare करना होगा, obligation को chain में आगे deferring करते हुए।
उदाहरण: Implementing Abstract Methods
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());
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Abstract Classes with Constructors
Abstract classes constructors define कर सकती हैं, जो जब भी एक concrete subclass object बनता है अपने आप चलते हैं (normal parent-constructor-runs-first sequence के हिस्से के रूप में), भले ही abstract class खुद कभी अकेले instantiate न हो सके।
उदाहरण: Abstract Classes with Constructors
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);
}
}
Login to try C/C++/Java/PHP code in the editor
- एक abstract class के लिए
new Shape()से एक object बनाना, जो एक compile error है। - एक concrete subclass में सभी abstract methods implement न करना, जो एक compile error है।
- एक abstract method को
privateयाfinalमार्क करना, जो allowed नहीं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: