What is the main reason for OOP? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the main reason for OOP?

Main Reason For OOP(Object Oriented Programming)? Please have honest answers, ty for the answers!😉😀😁

5th Nov 2018, 2:01 AM
Potato Hacker
Potato Hacker - avatar
3 Answers
+ 2
You would find the uses of OOP if you have worked with languages like C or lua. OOP allows you to make classes which act as the blueprint for a certain object. For example think you are making a game with zombies. Each zombie has properties like health, damage, favouriteFood(maybe brains😂) etc. So you just start to code those zombies on scratch. Let me try it in python - because I cant even think about java without OOP. zombie1 = {health:100,damage:85,favFood:"brains"} zombie2 = {health:10,damage:65,favFood:"blood"} This will be OK if you just going to deal with only these 2 zombies. But the player wants more zombies. So we have to code 1000 of xombies to satisfy the user. And also the zombie should have its behaviours like walk, eat, attack, poop, etc. But classes makes the task more easier and efficient. To be continued....
3rd Dec 2018, 12:36 PM
Seniru
Seniru - avatar
+ 2
So we can make a class of zombies like this. class Zombie: def __init__(self,health,damage): self.heath = health self.damage = damage def attack(self,opp): opp.health = opp.health - self.damage #and then storing our zombies to zombieTruck = [] for x in range(100000): zombieTruck.append(Zombie())
3rd Dec 2018, 12:38 PM
Seniru
Seniru - avatar
0
It allows you to keep your code in a tight little package. Make a car put all variables and methods that are for a car all in a neat little block called class Car. It is a blueprint builder. So you will always have a working car and the plans for a new one. It is a nice structure easy to create multiple objects etc.
5th Nov 2018, 2:31 AM
James
James - avatar