← Back to Kotlin Course | Chapter 7: Inheritance & Interfaces | Lesson 3 of 7

Abstract Classes क्या हैं

एक abstract class एक blueprint है जिसे जान-बूझकर अधूरा छोड़ा गया है, इसलिए इसे कभी सीधे नहीं बनाया जा सकता -- सिर्फ इसकी पूरी हुई subclasses बनाई जा सकती हैं।
Syntax
kotlin
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

markup
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()}")
}

Abstract Members Have No Body

एक abstract fun या abstract val सिर्फ एक signature declare करता है, बिना किसी implementation के -- हर concrete subclass को override उपयोग करके एक देना ज़रूरी है।

उदाहरण: Abstract Members Have No Body

markup
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()}")
}

Mixing Abstract and Concrete Members

एक abstract class abstract वालों के साथ-साथ पूरी तरह implemented members भी define कर सकती है, specific details subclasses पर छोड़ते हुए common behavior share करते हुए।

उदाहरण: Mixing Abstract and Concrete Members

markup
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())
}

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

markup
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))
}
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 को AbstractClass() से सीधे instantiate करने की कोशिश करना, जिसे compiler हमेशा reject करता है।
  2. यह भूल जाना कि abstract members implicitly open हैं और उन्हें अतिरिक्त open keyword की ज़रूरत नहीं।
  3. किसी 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:

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.