👨🏼💻Kotlin Abstraction
The concept of Abstraction and Abstract Classes in Kotlin
Introduction
Hello, Dear Android and Kotlin lovers! Welcome to my article! Today, I would like to talk about the concept of Abstraction, which is indispensable in the software world and must be internalized at advanced levels. Likewise, I am going to talk about the use of the concept of abstraction in detail.
Abstraction:
Abstract classes are template classes. They act as a blueprint. And they help us to establish is-a relationships with other classes.
Furthermore, you cannot create instances of Abstract classes. Moreover, functions defined as abstract do not have a body. Same way, values defined in an abstract way cannot have default values. Let’s understand this better with an example.
As you can see above, we have already created a template for some of the features that companies should have. We have to override all abstract structures in this abstract class in all classes that inherit this class.
Now we inherit this abstract class from other classes that we created.
As you can see above, we created a single Company template class and inherited this abstract class to all our other company classes.
Likewise, an abstract class can inherit from another abstract class. In addition, when it inherits, there is no obligation to override the abstract structures of the inherited class.
As we saw in the example above, if we inherit another abstract class, we don’t have to override its abstract values or methods.
But as you can see in the example below, we can override these abstract values and methods in our new abstract class if we want.
In addition to all this information, if an abstract class inherits from another abstract class and overrides a method of the class it inherits, it becomes completely optional for other classes that inherit this child abstract class to override that overridden method. It may sound a little confusing, but let’s examine this with an example.
As we saw in the example above, overriding the method we previously overridden in the child abstract class has become optional in classes that inherit our child abstract class.
Apart from all these, we can define values inside Abstract classes without using the abstract keyword if we want. So we can keep the state inside these classes. Likewise, we can create functions without using the abstract keyword and define bodies for these functions. Moreover, we can add visibility modifiers to these functions.
In fact, if there are optional structures that we want to add to our Abstract classes, and we do not have to override these structures in all of our child classes, we mark them as open.
However, it should not be forgotten that it is unreasonable to create functions and values that do not contain the abstract keyword in order not to violate the general logic of abstraction. Because if we want to create something without following the abstract definition, we don’t need such an abstract class anyway. Therefore, the internalization of abstraction is very important for advanced development.
Furthermore, we can write an extension function on an abstract class. We can even write this extension function infix. (Additionally, we can write our own abstract functions in infix form if we follow the infix rules.)
Lastly, each class can inherit one Abstract class. There are no multiple inheritances in Kotlin. In short, a class can only have an is-a relationship with a single Abstract class.
Conclusion
In conclusion, Abstract classes are template classes that allow us to establish an is-a relationship with our classes. Therefore, before using them in our projects, we need to think about them thoroughly and create these structures in accordance with our needs. It should not be forgotten that abstraction will be a topic that you can internalize more over time. The sooner you start internalizing this topic, the better it will be for you!