Object Oriented Programming — Part 1
Writing about all concepts around my studies of programming using and learning Kotlin, and the first doc is about OOP.
Abstraction
Abstract something means hiding implementation details inside something.
Classes and Objects
The classes are declared to mold the objects and instance them and these objects represent in general, an abstraction of something in the real world or something in the technology world.
Objects are composed of states and behaviors, to be more specific and clear, the states are the data held on attributes and the behaviors manipulate data.
The class Post molds the Post object, this object instantiates states, such as likes on the post, and behaviors such as getting metadata.
class Post {
var likes: Int = 5
fun getMetadata(): Int = likes
}Encapsulation
Is used to restrict our data/state to another class or the rest of the system, below we encapsulate the number of reports of a post.
class Post { private val reports: Int = 0
}
Inheritance
This step is used to inherit behaviors, types, or states from a superclass, interface, or abstract class, it isn’t recommended use inheritance to develop our system(avoid it), is recommended to use the composition of objects.
Get Matheus Serafim’s stories in your inbox
Join Medium for free to get updates from this writer.
The composition is about the relationship “TEM UM” when a class needs to use the behavior of another, it’s usually better to use composition.
Object PostObject Photo() -> Post().like() // Object Photo use the behaviorObject Video() -> Post().like() // Object Video use the behavior
Polymorphism
Here we have a different implementation in each class for the same method inherited.
abstract class Post { open class fun getMetadata()
}
class Photo : Post { override fun getMetadata() = "Likes"
}class Video : Post { override fun getMetadata() = "Views"
}
I hope you liked this doc :)








