Search this site
Embedded Files
Squirrel of Rama
  • Home
  • Institute Coaching Programs
  • Android curriculum
    • Android Topics
      • Notification
      • Broadcast & Broadcast Receiver
      • Content Providers
      • Activities
        • Activity and Lifecycle
        • Saving state
        • Tasks and the back stack
      • Services
        • Services
        • Foreground Service
        • Bound Service
      • Dependency Injection
      • Workmanager
      • Fragment Findings*
      • Navigation findings*
      • Permissions Findings*
      • Room & Datastore findings*
    • Kotlin Topics
      • Nullables
      • Exceptions
      • Conditions and Loops
      • Arrays
      • Collections
      • Collection Functions
      • Class & Object
      • OOP Concepts
      • Scope Function
      • Visibility Modifiers
      • Generics
      • Kotlin Classes Types
        • Enums
        • Sealed Class
        • Data Classes
        • Nested and inner classes
        • Object expressions and declarations
      • Kotlin Functions Types
        • Functions
        • Extension functions
        • Higher-order functions and lambdas
      • Couroutines
      • Kotlin Flows
    • Interview Questions
      • Android Interview Questions
  • Products
    • Shabri
    • WedExhibit
  • About
Squirrel of Rama
  • Home
  • Institute Coaching Programs
  • Android curriculum
    • Android Topics
      • Notification
      • Broadcast & Broadcast Receiver
      • Content Providers
      • Activities
        • Activity and Lifecycle
        • Saving state
        • Tasks and the back stack
      • Services
        • Services
        • Foreground Service
        • Bound Service
      • Dependency Injection
      • Workmanager
      • Fragment Findings*
      • Navigation findings*
      • Permissions Findings*
      • Room & Datastore findings*
    • Kotlin Topics
      • Nullables
      • Exceptions
      • Conditions and Loops
      • Arrays
      • Collections
      • Collection Functions
      • Class & Object
      • OOP Concepts
      • Scope Function
      • Visibility Modifiers
      • Generics
      • Kotlin Classes Types
        • Enums
        • Sealed Class
        • Data Classes
        • Nested and inner classes
        • Object expressions and declarations
      • Kotlin Functions Types
        • Functions
        • Extension functions
        • Higher-order functions and lambdas
      • Couroutines
      • Kotlin Flows
    • Interview Questions
      • Android Interview Questions
  • Products
    • Shabri
    • WedExhibit
  • About
  • More
    • Home
    • Institute Coaching Programs
    • Android curriculum
      • Android Topics
        • Notification
        • Broadcast & Broadcast Receiver
        • Content Providers
        • Activities
          • Activity and Lifecycle
          • Saving state
          • Tasks and the back stack
        • Services
          • Services
          • Foreground Service
          • Bound Service
        • Dependency Injection
        • Workmanager
        • Fragment Findings*
        • Navigation findings*
        • Permissions Findings*
        • Room & Datastore findings*
      • Kotlin Topics
        • Nullables
        • Exceptions
        • Conditions and Loops
        • Arrays
        • Collections
        • Collection Functions
        • Class & Object
        • OOP Concepts
        • Scope Function
        • Visibility Modifiers
        • Generics
        • Kotlin Classes Types
          • Enums
          • Sealed Class
          • Data Classes
          • Nested and inner classes
          • Object expressions and declarations
        • Kotlin Functions Types
          • Functions
          • Extension functions
          • Higher-order functions and lambdas
        • Couroutines
        • Kotlin Flows
      • Interview Questions
        • Android Interview Questions
    • Products
      • Shabri
      • WedExhibit
    • About

OOP↗

Inheritance↗
Open keyword & Final class
Overriding
Any
Polymorphism
Abstract Classes↗
Interfaces↗

Inheritance↗

By default, Kotlin classes are final – they can't be inherited. To make a class inheritable, mark it with the open keyword:

All classes in Kotlin have a common superclass, Any, which is the default superclass for a class with no supertypes declared:

Open keyword & Final class

Like java we can not just extend the class. By default all class are final in kotlin.

By default, Kotlin classes are final: they can't be inherited. To make a class inheritable, it has to be marked with the open keyword. 

Parent constructor & init block called first

Overriding 

Ambiguity in parent child class. it suggest for the keyword ovverride

final methods can not be ovverriden. so make it open

Because of this experience in Java, the Kotlin designers decided to make all classes and methods final by default. That way, you can “design for inheritance” by marking classes and functions as open only when you really want them to be open to extension.

Any

Every kotlin class has Any as a superclass

Any class has following methods

  • open operator fun equals(other: Any?): Boolean

  • open fun hashCode(): Int

  • open fun toString(): String


All methods are open so child class can override it

whenever we write tostring method in our class it shows override suggestion. because Any is the parent and it has tostring method

Inheritance & Overriding Videos

Polymorphism

Polymorphism is achieved by inheritance. Abstraction is not mandatory to achieve polymorphism

To execute a common command in all the objects we need a array which has same data type for all the diff objects so to do so we make a parent class and use its name as a common data type for all child objects

so that in for loop we can fire the same command to all the objects

using abstraction we can make sure that all the child object implement same method which we call from the for loop

every child object behaves differently according to their implementation in common implemented method

Abstract Classes↗

Abstract class is incomplete so we can not create an object of it

Abstract methods are by default open


Abstract classes are always open. You do not need to explicitly use open keyword to inherit subclasses from them.

Interfaces↗

Even though classes are not in a same inheritance hierarchy still they can follow the same implementation using interface 

polymorphism with abstract class is possible only if the child object have same parent which is an abstract class. with same inheritance tree.

so if the objects are not in a same tree then to achieve polymorphism we have to use interface Or we can use interface superclass hierarchy to make it possible which is not possible with abstract class

Confusing

A brand by Jagrut Soni
Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse