Guys welcome back , you already know that I develop mobile apps and use the kotlin programming language in calloboration with the other 2 languages 'JAVA' , 'DART' with flutter. And today I'm gonna be talking about a confusing topic called Inheritance and classes in an object orriented programming language.
I've been focusing on getting the google mobile development certicate for the last two weeks and my purpose is to get the certicate and review my old information to make myself wiser in programming.
Let's get started...
2. What is a class hierarchy?
It is natural for humans to classify items that have similar properties and behavior into groups and to even form some type of hierarchy among them. For example , you can have a broad category like automobiles, and within that you can have more specific types like cars and buses. Within Cars or Buses , you can have even more specific types like Ford cars , Bmw cars or Toyota buses or Mitsubishi buses. This can be represented as a hyerarchy because car and bus categories inherit all the properties of Automobile. Similarly Ford , Bmw , Toyota , Mitsubishi all seperately have the properties of car and bus plus their own unique properties.
Inheritance in Android Classes
While you can write Kotlin code without using classes, Understanding class hierarchies is therefore fundamental to Android app development and allows you to take advantage of features provided by the Android framework.
For example, there is a View
class in Android that represents a rectangular area on the screen and is responsible for drawing and event handling. The TextView
class is a subclass of the View
class, which means that TextView
inherits all the properties and functionality from the View
class, plus adds specific logic for displaying text to the user.
Taking it a step further, the EditText
and Button
classes are children of the TextView
class. They inherit all the properties and methods of the TextView
and View
classes, plus add their own specific logic. For example, EditText
adds its own functionality of being able to edit text on the screen.
Instead of having to copy and paste all the logic from the View
and TextView
classes into the EditText
class, the EditText
can just subclass the TextView
class, which in turn subclasses the View
class. Then the code in the EditText
class can focus specifically on what makes this UI component different from other views.
On the top of a documentation page for an Android class on the developer.android.com website, you can see the class hierarchy diagram. If you see kotlin.Any
at the top of hierarchy, it's because in Kotlin, all classes have a common superclass Any.
As you can see, learning to leverage inheritance among classes can make your code easier to write, reuse,
also you can see my homework myself this the the link : My Code
No comments:
Post a Comment