Javatpoint Logo
Javatpoint Logo

Kotlin Abstract class

A class which is declared with abstract keyword is known as abstract class. An abstract class cannot be instantiated. Means, we cannot create object of abstract class. The method and properties of abstract class are non-abstract unless they are explicitly declared as abstract.

Declaration of abstract class

Abstract classes are partially defined classes, methods and properties which are no implementation but must be implemented into derived class. If the derived class does not implement the properties of base class then is also meant to be an abstract class.

Abstract class or abstract function does not need to annotate with open keyword as they are open by default. Abstract member function does not contain its body. The member function cannot be declared as abstract if it contains in body in abstract class.

Example of abstract class that has abstract method

In this example, there is an abstract class Car that contains an abstract function run(). The implementation of run() function is provided by its subclass Honda.

Output:

Honda is running safely..

A non-abstract open member function can be over ridden in an abstract class.

Output:

Car is running..
Honda City is running..

In above example, An abstract class Honda extends the class Car and its function run(). Honda class override the run() function of Car class. The Honda class did not give the implementation of run() function as it is also declared as abstract. The implementation of abstract function run() of Honda class is provided by City class.

Example of real scenario of abstract class

In this example, an abstract class Bank that contains an abstract function simpleInterest() accepts three parameters p,r,and t. The class SBI and PNB provides the implementation of simpleInterest() function and returns the result.

Output:

SBI interest is 150.0
PNB interest is 135.0

Next TopicKotlin Interface





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA