Object-Orientated Programming (OOP) was the main focus of our first unit in this course. One of the features of OOP is inheritance. Inheritance allows us to be able to take and use methods from a previously written class rather than rewriting it all over again in a similar class. Suppose we made a Shape class which may have a method to get its area. Then we decide to create a Square class, which will have new attributes such as having defined side lengths. However, Square is certainly a Shape and also has an area. By having Square inherit Shape, we are able to use the previously established method to get the area from Shape in Square.
OOP allows us to organize our classes that are similar. For example, a Square and Triangle class both can be organized under an abstract class Square. We can define things that all shapes possess in Shape or at least lay out the foundation to be overwritten in one of its subclasses by implementing a NotImplementedError. Then we will define things specific to Square and Triangle in their own class. By having both of these classes inherit Shape, we can simplify our code by having other parts of our code work for generic Shape objects. Since Square and Triangle are both also Shape objects, any of code that works for Shape objects will work for them. Imagine making thousands of specific card classes for a card game. We would not want to implement code to deal with every single type of card classes. Instead, we can have every specific card class inherit a superclass Card then implement code to handle Card objects.
In inheritance, a subclass inherits methods and attributes from the superclass. It is then possible to extend a method from the superclass in the subclass by reusing the same code from it buy altering it in a way to better suit the subclass. It is also possible to completely override a method from the superclass by rewriting the whole method if a way to alter the original code to fit the new class is not readily available.
I thought the course's approach to teaching us OOP was okay. I generally found the examples to help me better understand what is going on when I decide to read over the course notes. I didn't really like how fast and rushed the first assignment felt. We were still going over many things involved in the assignment during class when it came out. In the end, my group ended up finishing the assignment during the last few days before it was due.
No comments:
Post a Comment