LibraryInheritance and `open` keyword

Inheritance and `open` keyword

Learn about Inheritance and `open` keyword as part of Kotlin Android Development and Play Store Publishing

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

code
open
keyword.

The `open` Keyword: Enabling Inheritance

By default, all classes in Kotlin are

code
final
, meaning they cannot be inherited from. To allow a class to be inherited, you must explicitly mark it with the
code
open
keyword. This signifies that the class is intended to be a base class (superclass or parent class).

What keyword in Kotlin is required to make a class inheritable?

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

code
open
class, you use a colon (
code
:
) followed by the name of the superclass. If the subclass has its own constructor, it must call the superclass's constructor using
code
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

code
override
keyword before the member declaration in the subclass. Members marked as
code
open
in the superclass can be overridden.

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

code
super
keyword is used within a subclass to refer to members (properties or methods) of its immediate superclass. This is particularly useful when you want to call the superclass's implementation of an overridden method or access a superclass's property.

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,

code
Activity
and
code
Fragment
are base classes provided by the Android SDK. Your custom activities and fragments will inherit from these, overriding methods like
code
onCreate()
or
code
onViewCreated()
to implement your app's specific logic. Understanding
code
open
and inheritance is crucial for building robust and maintainable Android applications.

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.

What keyword is used to call the superclass's implementation of an overridden method?

The super keyword.

Learning Resources

Kotlin Inheritance - Official Documentation(documentation)

The official Kotlin documentation provides a comprehensive overview of inheritance, including the `open` keyword, overriding, and constructors.

Kotlin Classes and Objects - Inheritance(blog)

GeeksforGeeks offers a clear explanation of Kotlin classes and objects with a focus on inheritance, providing code examples.

Android Development: Inheritance and Polymorphism(tutorial)

This Android Basics in Kotlin tutorial covers inheritance and polymorphism in the context of Android development.

Kotlin `open` Keyword Explained(documentation)

TutorialsPoint provides a concise explanation of the `open` keyword in Kotlin and its role in inheritance.

Understanding Inheritance in Kotlin(tutorial)

Ray Wenderlich offers a beginner-friendly tutorial on Kotlin inheritance and polymorphism with practical examples.

Kotlin Inheritance: Superclass and Subclass(blog)

Javatpoint explains the concepts of superclass and subclass in Kotlin inheritance with illustrative examples.

Kotlin `override` Keyword(blog)

KotlinWorld details the usage of the `override` keyword in Kotlin for overriding members from a superclass.

Object-Oriented Programming in Kotlin(blog)

Baeldung provides a comprehensive guide to Object-Oriented Programming in Kotlin, including a section on inheritance.

Kotlin Inheritance and Polymorphism Explained(video)

A YouTube video explaining Kotlin inheritance and polymorphism with visual aids and code demonstrations.

Kotlin `open` vs `final` Classes(documentation)

Programiz clarifies the difference between `open` and `final` classes in Kotlin and how they relate to inheritance.