When I first thought of Object-Oriented Programming, I had no clue what this term means. However, as I started to study Computer Science, my knowledge in Object-Oriented Programming is improved. Object-Oriented Programming is a programming where we mainly focus on objects themselves instead of its way to get to the solution. Some examples of Object-Oriented Programming are C++, Java and Python.
Some basic components of Object-Oriented Programming are class, object, method, instance and recursion.
We already learnt what class is and how to create a class. I will implement only the init method for each class. I will also try to explain instances in Python.
For example,
class Cake:
def __init__(self, candle, topping):
""" (Cake, int, str) -> NoneType
Initialize class Cake with candle and topping
"""
self.candle = candle # This can be considered as instance variable for specific instance
self.topping = topping
We can use inheritance to use class Cake in other class like a class CheeseCake to pass the information that class Cake contains to a new class CheeseCake. I will also demonstrate class variable in class CheeseCake. Object-oriented Programming can have multiple inheritance.
class CheeseCake(Cake):
Cake_List = [] #This is the variable class that is shared inside the class
def __init__(self, candle, topping):
""" (CheeseCake, int, str) -> NoneType
Initialize class CheeseCake with candle and topping
"""
Cake.__init__(self, candle, topping)
Method is based on object. For Python, we learnt that method is inside a class of an object.
class Feeling:
def tired(self, feeling):
#body implementation omitted
def tired(self, feeling) is considered a method since it is inside a class Feeling.
However, function is different compared to method. Function does not depend on object.
def hello(self, greeting):
#body implementation omitted
def hello(self, greeting) is considered a function as it is not inside class of an object.
We can think of a method as a function that depends on an object like class.
Interestingly, recursion is also one of the basic concepts in Object-Oriented Programming.
As I've already covered this topic deeply in previous SLOG posts, I will skip this one.
Object-Oriented Programming has now become a significant part of our CS society. Although the only OOP I know is Python, I hope to learn Java and C++ during my third and fourth years at UofT. Another thing I found interesting is the number of OOP. There are more than 20 OOPs in the world. I guess I cannot master all of them. Instead, I will study 2 or 3 OOPs in depth for my future career.
Learning OOP is so interesting to me. Maybe, in the future, I hope to create a new OOP that is much easier than Python so that anyone can learn OOP.
No comments:
Post a Comment