Abstract Class

Reading Time: 3 minutes

In Kotlin, abstract class is declared using the abstract keyword in front of class. An abstract class can not instantiated means we can not create object for the abstract class.

Important points

      • We can’t create an object for abstract class.
      • All the variables (properties) and member functions of an abstract class are by default non-abstract. So, if we want to override these members in the child class then we need to use open keyword.
      • If we declare a member function as abstract then we does not need to annotatate with open keyword because these are open by default.
      • An abstract member function doesn’t have a body, and it must be implemented in the derived class.
      • An abstract class can contain both abstract and non-abstract members.


Example abstract class

Kotlin program using both abstract and non-abstract members
in an abstract class.


Example abstract class
      • In the above program, Engineer class is derived from the Employee class.
      • An object eng is instantiated for the Engineer class.
      • We have passed two parameters to the primary constructor while creating it.
      • This initialize the non-abstract properties name and experience of Employee class.
      • Then employeeDetails() method is called using the eng object.
      • It will print the values of name, experience and the overridden salary of the employee.
      • In the end, dateOfBirth() is called using the eng object and we have passed the parameter date to the primary constructor.
      • It overrides the abstract fun of Employee class and prints the value of passed as parameter to the standard output.

Overriding a non-abstract open member with an abstract one

In Kotlin we can override the non-abstract open member function of open class using the override keyword followed by abstract in the abstract class. In the below program we will do it.


Overriding

Multiple derived classes

An abstract member of abstract class can be override in all the derived classes. In the program, we overrides the cal function in three derived class of calculator


Multiple derived classes

Example


Example

Here, a Teacher class is derived from an abstract Person class .

An object jack of Teacher class is instantiated. We have passed “Jack Smith” as a parameter to the primary constructor while creating it. This executes the initializer block of the Person class.

Then, displayJob() method is called using jack object. 

Note, that the displayJob() method is declared abstract in the base class, and overridden in the derived class.

Finally, displaySSN() method is called using jack object. The method is non-abstract and declared in Person class (and not declared in Teacher class).

Questions

I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉

Have a nice a day! 🙂

 


 

Follow and like us:
Click to rate this post!
[Total: 0 Average: 0]

Leave a comment

stay informed!

Subscribe to receive exclusive content and notifications