Why Jetpack Compose in Android?
Factors that lead to the invent of Jetpack compose
The above diagram represents the hierarchy of Android View Components. Each of these classes have thousands of lines of code. CheckBox extends Button, which extends TextView, which in turn extends View. But in fact, CheckBox doesn’t really use each line of code in Button class. So we’re at a disadvantage by using Inheritance. The situation is the same for most of the components.
So Jetpack Compose is invented which uses composition rather than inheritance. It offers:
- Consistent look across devices
- Uses Material Design by default.
Composition Vs Inheritance
Composition is a has-a relationship. It means one class can use the functionality of another class using the object of that class.
Though both composition and inheritance provide code reusability, main difference between them is that composition allows code reuse without extending it but for inheritance, you must extend the class for any reuse of code or extensibility.
Another difference which comes from this fact is that by using composition you can reuse code for even final classes which are not extensible.
Flexibility- You can reuse code from multiple classes using composition whereas you can only extend a class by a single parent class.
You can read more about the differences between composition and inheritance in the below blogpost:
You can watch an entire presentation at DroidCon San Francisco on Jetpack compose here: