Skip to main content

Blog: Code Sanity Rules - Mastering Advanced Inheritance in Kotlin

Blog: Code Sanity Rules - Mastering Advanced Inheritance in Kotlin

Day 48! Today I explored Inheritance Rules in Kotlin. In traditional Object-Oriented Programming, inheritance can easily turn into a sprawling, fragile mess if the language doesn't enforce strict boundaries. Kotlin addresses this by introducing explicit compilation rules that ensure subclass relationships remain clean, intentional, and robust.

Meet Any: The True Ancestor Root

The first major architectural change to note is that Kotlin’s root class is Any, not java.lang.Object. Every class you declare implicitly inherits from Any. It provides exactly three core methods: equals(), hashCode(), and toString(). It contains no background threading synchronization locks or garbage collection cleanup references, keeping your class metadata extremely lean.

Overriding Properties is Legal!

In many older frameworks, overriding only applies to methods; field properties are treated as separate variables. In Kotlin, because properties are actually compiled as implicit getters and setters, you can explicitly override a property field:

open class CoreBaseAccount {
    open val interestRate: Double = 0.01
}

class PremiumSavingsAccount : CoreBaseAccount() {
    override val interestRate: Double = 0.05 // Upgrading the property value!
}

You can even override a base val property with a subclass var property (since you are simply adding a setter). However, you can never override a var with a val, as you cannot remove an established setter contract.

Putting a Stop to Changes: final override

By default, whenever you override an open function inside a subclass, that function remains open for subsequent grandchildren classes to override again. If you want to cut off this chain and declare that your subclass implementation is the absolute final word, you mark it as a final override. It locks the method down securely, keeping downstream architectures stable.

Untangling Multiple Inheritance Conflicts

Kotlin allows you to inherit from a base class while implementing multiple interfaces concurrently. But what happens if both your base class and an interface contain an identical method signature? The compiler intercepts this immediately and forces you to write an explicit override.

Inside your custom block, you map out exactly how calls should be routed using qualified super brackets:

override fun draw() {
    super<RectangleShape>.draw() // Route to base class logic
    super<TextOverlay>.draw()    // Route to interface default logic
}

Summary

By enforcing strict rules around property overriding, final overrides, and multi-inheritance conflict resolution, Kotlin keeps object structures readable, secure, and resilient against fragile base class bugs.

Check out the full technical breakdown!

Kotlin #Inheritance #OOP #CleanCode #AndroidDev #JVM #SoftwareArchitecture

Comments

Popular posts from this blog

Mastering Per-App Language Preferences: From Android 10 to Android 13+

  One of the most requested features by users is the ability to use an app in a language different from the system language. While Android 13 (API 33) introduced "Per-App Language" settings at the system level, implementing this backward-compatibly for older devices like Android 10 (API 29) used to be a challenge involving manual configuration wrapping. Today, thanks to AppCompat 1.6.0+ , we have a unified, standard way to handle this. Here is the modern guide to implementing seamless language switching. The Architecture: How it Works On Android 13 and above, the system handles the storage and application of your app's locale. On older versions, the AppCompat library manages this behavior by storing your preference in an internal XML file and injecting the resources during the activity lifecycle. Step 1: The Locale Helper Utility Instead of scattering logic across your app, use a clean LocaleHelper object. This handles the distinction between the system LocaleManager (...

Beyond Scanning: Meet the All-in-One AI Document Assistant by Digitify Vision Technology

In an era where efficiency is the ultimate currency, information is scattered everywhere—on physical paper, within QR codes, and inside lengthy digital documents. The challenge isn't just capturing this data; it’s making it work for you. At Digitify Vision Technology , we believe your smartphone should be more than just a camera—it should be a high-performance engine for your daily workflow. Today, we are proud to unveil our most powerful update yet: a total evolution in AI-driven document management. 💡 Key Features of the New Update We’ve bridged the gap between physical information and digital action by integrating advanced vision and audio intelligence into a single, seamless experience. 🔍 Precision Scanning for Everything Our advanced vision engine is now more versatile than ever. Intelligent Document Scanning: Capture crisp, professional-grade scans of physical papers, contracts, or handwritten notes. QR Code Extraction: Instantly scan any QR code to extract text, read URL...
  Optimising Android Launch: Mastering App Startup & The Modern Splash Screen The first few seconds of an app’s life determine a user's first impression. A slow start or a flickering white screen can lead to immediate uninstalls. In this guide, we’ll look at how to streamline your initialisation using the App Startup Library and ensure a seamless visual transition with the Android 12 Splash Screen API (with full backward compatibility for API 29). Part 1: The App Startup Library Traditionally, apps initialised multiple components using separate content providers. This adds overhead and slows down launch time. The Jetpack App Startup library allows all components to share a single content provider, significantly improving performance. Why use it? Performance: Shared content provider reduces overhead. Order: Explicitly set the initialisation sequence. Simplicity: Both library creators and app developers use a unified interface. Part 2: The Modern Splash Screen API Starting ...