Entry no. 4, Week 6:
Topics
1. Summary of Object-Oriented Programming concepts
This week, the topic is about object-oriented programming which is something that I am already very familiar with. Since this is a very broad topic, I will be talking about Instances, Abstract classes and Inheritance. The examples will be in Python3.
First, let's define a class. A class is a representation of an object. They are given names and are composed of attributes and operations (methods) that can be used by several Instances of the same class.
Instances are objects and are occurrences of specific classes. They use the methods that are defined within the class, and also have unique variables that store information. (disclaimer: that does not mean that there can only exist unique variables, but that there are also exist variables which can be accessed throughout all instances of a class.) Instances of classes are frequently used in programming and play a large role in creating interfaces.
For example, we can create a class Student that manages the information and actions of a student. We can give that Student a name and attributes by creating an instance of Student, we can tell the student to enroll in courses and we can take a look at their grades.
Abstract classes are classes that aren't meant to be used. Within these abstract classes, there exist properties and methods as well. Even though these are defined, they represent the framework for subsequent classes. That is, they are meant to be used in subclasses that will extend the functionality of the abstract class. The way that these subclasses extend functionality is called Inheritance.
Suppose we had a class called Vehicle which is a thing used for transporting people or goods. This is an example of an Abstract class because there are many vehicles, but they are all different in their own respects. Subsequent classes may include Cars, Boats, Planes...etc but they are all derived from the same Abstract class Vehicle.
In one of my previous post's I briefly talked about Inheritance which mentioned method Over-riding, Extending, and Inheriting. Over-riding is a process where an entire method is re-written to fulfill the needs of the current class. In abstract classes, methods that were not fully defined would be over-written and defined in the sub-class. Extending is a process where a sub-class would use a methods from the super-class in the definition of the current method or define a completely new method that is semi-related to the super-class. Inheriting is taking a method that is already defined in the super-class and using it in the sub-class, such that it does not need to be defined in the sub-class.
Still using the Vehicle class, suppose we made a subclass Bus. We would over-ride the __str__ method because the string representation for the two would be completely different. We could also extend the Bus by using the __init__ method from Vehicle, but by adding fare and totalFare attributes, and adding methods passengerOn and passengerOff. The bus would inherit the method move, since Vehicles Buses both move.
