Hi TA!
Please mark this for my week 7 evaluation!
The topic of inheritance wasn't too difficult to grasp for me. It is
pretty straight forward and Danny made life easy on me in understanding
it by starting off with simple examples.
This
was an in-class example of two classes - Class A and Class B, where
Class A had a method g(self, n), where it returned 'n' and another
method in f(self, n), which returned the self.g(n). Note the 'self' is
included in the parameter of those respective methods to ensure that the
first parameter is in the instance of the Class A. Also note that the
methods can be re-written as self.f(n) and self.g(n), which makes it a
lot less complicated and confusing for me. This is because the first
parameter is still an instance of that class.
Class B
was a subclass of Class A, which means it inherited the
characteristics(methods) of Class A. Class B also had a method
self.g(n), but this one returned '2 * n'.
The result? The two print statements which called a.f(1) and b.f(1), printed 1 and 2 respectively.
a.f(1)
is quite simple to understand - it directly calls self.f(1) from Class
A, which returns self.g(n). self.g(1) simply returns n, which in this
case is 1.
b.f(1) is a lot trickier. Despite Class B
not directly containing self.f(n) as a method, it inherits self.f(n)
from Class A, and then calls self.g(n) within its own class instead of
Class A's g(self.n), thus printing 2 * 1, which is 2. In this case,
Class B's self.g(n) is overriding Class A's self.g(n), only focusing on
the method that is occurring in Class B
I found an intriguing way to remember the concept of inheritance:
I
was sitting next to a buddy of mine and he explained to me to think of
the subclass as the son who inherits the characteristics of a father and
has his own techniques to surpass him. Needless to say, I'm a big fan
of Naruto and in particular the father-son relationship between Naruto
and Minato immediately popped to mind! Naruto(son) eventually learns the
'Rasengan' that his father mastered, but manages to surpass his father by learning the powers of the technique through his tailed beast powers. Similarly, Class B inherited Class A's methods, but managed to 'surpass' Class A's self.g(n) by return 2 * n instead of the n that was inherited.
OOP
was also heavily reinforced in Assignment 1, where inheritance played a
key role in the proper implementation of future games. There were three
main classes - GameState (GS), ComputerStrategy (CompStrat) and
GameView - the first two both were inherited by SubtractSquareGameState
and SubtractSquareComputerStrategy respectively. Even though me and
partner could've completed the assignment without either subclass, the
profs emphasized the use of inheritance to help us design and implement
future games for A2 and A3.
As such, we planned
accordingly and used python's 'NotImplementedError' on various methods
for the generic GameState and ComputerStrategy classes with a message
stating that certain games were not implemented.
Me and partner designed the specific SubtractSquareGameState and
SubtractSquareComputerStrategy so that the specific classes were
subclasses to the generic ones. This meant that the methods with
'NotImplementedError' were also passed down, but we used the concept of
overriding to complete our specific methods as requested from the
assignment sheet. It was a fantastic learning experience.

No comments:
Post a Comment