Abstract Classes क्या हैं
In this page:
abstract class ClassName {
abstract fun method(): ReturnType // no body
fun concrete() { } // shared code
}
class SubClass : ClassName() {
override fun method(): ReturnType = value
}
Declaring an Abstract Class
abstract class एक ऐसी class को चिह्नित करता है जिसे अपने आप instantiate नहीं किया जा सकता -- यह सिर्फ subclass होने के लिए मौजूद है, अक्सर संबंधित types के लिए एक shared shape define करने के लिए।
उदाहरण: Declaring an Abstract Class
abstract class Shape {
abstract fun area(): Double
}
// Define a class `Circle`
class Circle(val radius: Double) : Shape() {
override fun area(): Double = Math.PI * radius * radius
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `circle`, set to `Circle(3.0)`
val circle = Circle(3.0)
// Print "Area: ${circle.area()}" to the console, with a trailing newline
println("Area: ${circle.area()}")
}
Login to try C/C++/Java/PHP code in the editor
Abstract Members Have No Body
एक abstract fun या abstract val सिर्फ एक signature declare करता है, बिना किसी implementation के -- हर concrete subclass को override उपयोग करके एक देना ज़रूरी है।
उदाहरण: Abstract Members Have No Body
abstract class Employee(val name: String) {
abstract fun monthlySalary(): Double
}
// Define a class `Manager`
class Manager(name: String, val baseSalary: Double) : Employee(name) {
override fun monthlySalary(): Double = baseSalary * 1.2
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `manager`, set to `Manager("Ana", 5000.0)`
val manager = Manager("Ana", 5000.0)
// Print "${manager.name}'s salary: ${manager.monthlySalary()}" to the console, with a trailing newline
println("${manager.name}'s salary: ${manager.monthlySalary()}")
}
Login to try C/C++/Java/PHP code in the editor
Mixing Abstract and Concrete Members
एक abstract class abstract वालों के साथ-साथ पूरी तरह implemented members भी define कर सकती है, specific details subclasses पर छोड़ते हुए common behavior share करते हुए।
उदाहरण: Mixing Abstract and Concrete Members
abstract class Shape {
abstract fun area(): Double
// Define the function `describe` with no parameters and returning a String
fun describe(): String = "This shape has area ${area()}"
}
// Define a class `Square`
class Square(val side: Double) : Shape() {
override fun area(): Double = side * side
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `square`, set to `Square(5.0)`
val square = Square(5.0)
// Print `square.describe()` to the console, with a trailing newline
println(square.describe())
}
Login to try C/C++/Java/PHP code in the editor
Abstract Classes vs Regular Open Classes
एक abstract class का उपयोग करें जब कोई sensible default implementation न हो और base type को सीधे instantiate करना समझदारी न रखे; एक regular open class का उपयोग करें जब एक default, उपयोग करने योग्य implementation मौजूद हो।
उदाहरण: Abstract Classes vs Regular Open Classes
abstract class PaymentMethod {
abstract fun pay(amount: Double): String
}
// Define a class `CreditCard`
class CreditCard : PaymentMethod() {
override fun pay(amount: Double): String = "Paid $$amount with credit card"
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `payment` (a `PaymentMethod`), set to `CreditCard()`
val payment: PaymentMethod = CreditCard()
// Print `payment.pay(49.99)` to the console, with a trailing newline
println(payment.pay(49.99))
}
Login to try C/C++/Java/PHP code in the editor
- किसी abstract class को
AbstractClass()से सीधे instantiate करने की कोशिश करना, जिसे compiler हमेशा reject करता है। - यह भूल जाना कि abstract members implicitly open हैं और उन्हें अतिरिक्त
openkeyword की ज़रूरत नहीं। - किसी concrete subclass में हर abstract member implement न करना, बिना समझे subclass को भी abstract छोड़ते हुए।
abstract classको सीधे instantiate नहीं किया जा सकता; सिर्फ इसकी concrete (non-abstract) subclasses किया जा सकता है।abstract fun/abstract valबिना implementation वाले members declare करते हैं जो subclasses को देने चाहिए।- Abstract members implicitly open हैं, इसलिए उन्हें override करने वाली subclass को सिर्फ
overrideचाहिए,openनहीं। - एक abstract class के पास अपने abstract members के साथ-साथ concrete (पूरी तरह implemented) members भी हो सकते हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: