Kotlin Inheritance: Building on Existing Code
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows you to create new classes based on existing ones. This promotes code reusability and establishes a hierarchical relationship between classes. In Kotlin, inheritance is achieved using the
open
The `open` Keyword: Enabling Inheritance
By default, all classes in Kotlin are
final
open
The open
keyword.
Creating a Derived Class (Subclass)
A class that inherits from another class is called a derived class, subclass, or child class. To inherit from an
open
:
super()
Subclasses inherit properties and methods from their superclasses.
A subclass automatically gains access to the non-private members of its superclass. This allows you to extend functionality without rewriting existing code.
When a class B
inherits from class A
, B
becomes a subclass of A
. B
can then access all public and protected members of A
. If A
has a primary constructor, B
must provide arguments for it in its own primary constructor. If A
has secondary constructors, B
must call one of them using super()
.
Overriding Members
Derived classes can provide their own specific implementation for methods or properties inherited from the superclass. This is called overriding. To override a member, you must use the
override
open
Consider a Vehicle
class that is open
. It has an open
function drive()
. A Car
class can inherit from Vehicle
and override
the drive()
function to implement car-specific driving logic. This demonstrates how subclasses can specialize behavior inherited from their parent classes, a core principle of polymorphism.
Text-based content
Library pages focus on text content
The `super` Keyword
The
super
Remember: You must call the superclass constructor using super()
if the subclass has a primary constructor and the superclass has a primary constructor.
Inheritance in Android Development
In Android development, you'll frequently encounter inheritance. For instance,
Activity
Fragment
onCreate()
onViewCreated()
open
Play Store Publishing and Inheritance
While inheritance itself doesn't directly impact Play Store publishing, the code structure and design patterns you employ, including effective use of inheritance, contribute to the overall quality, maintainability, and scalability of your app. A well-structured app is easier to update, debug, and manage, which indirectly supports a smoother publishing and update process.
The super
keyword.
Learning Resources
The official Kotlin documentation provides a comprehensive overview of inheritance, including the `open` keyword, overriding, and constructors.
GeeksforGeeks offers a clear explanation of Kotlin classes and objects with a focus on inheritance, providing code examples.
This Android Basics in Kotlin tutorial covers inheritance and polymorphism in the context of Android development.
TutorialsPoint provides a concise explanation of the `open` keyword in Kotlin and its role in inheritance.
Ray Wenderlich offers a beginner-friendly tutorial on Kotlin inheritance and polymorphism with practical examples.
Javatpoint explains the concepts of superclass and subclass in Kotlin inheritance with illustrative examples.
KotlinWorld details the usage of the `override` keyword in Kotlin for overriding members from a superclass.
Baeldung provides a comprehensive guide to Object-Oriented Programming in Kotlin, including a section on inheritance.
A YouTube video explaining Kotlin inheritance and polymorphism with visual aids and code demonstrations.
Programiz clarifies the difference between `open` and `final` classes in Kotlin and how they relate to inheritance.