Fundamentals of programming

Table of Contents

1 Programming paradigms - Object Oriented Programming

Object-oriented programming basics

  • OOP is a programming paradigm that models objects with attributes and behaviours. For example, a bicycle object could have attributes of speed, direction of movement while its behaviours could include brake, accelerate etc.
  • class, methods, and attributes
    • class is a blue print of an object defined by attributes and methods(model the behaviours). Examples include, people, car, animal.
    • a method of a class is the behaviour that type of object has. For example, people can speak, walk etc.
    • attributes are data members like age, name, model and year made.
    • an example of class in Python:

      oopBike.png

  • some terms:
    • instantiation : The creation of an object of a certain class.
    • instance: An individual object of a certain class.

Object-oriented programming principles

  • inheritance:
    • inheritance enables new objects/classes(child objects/classes) to take on the properties and behaviours of existing objects/classes(parent objects/classes).
    • A class that is used as the basis for inheritance is called a superclass or base class.
    • A class that inherits from a superclass is called a subclass or derived class
  • Encapsulation:
    • means wrapping up data member and methods together into a single Class.
    • it hides the internal details of an object and prevent its data members and methods being mis-used or accessed.
  • polymorphism:
    • literally means methods with the same name behaves differently depending on the input data type or object. You have used the Python function len() which can be applied to string, list or dictionary data types/objects.
    • Polymorphism is normally achieved by interface.
      • interface is a group of abstract methods (methods with no implementations).
      • a class implements the interface can then implement the methods however it suits but keep the methods' names.
      • the poragmming language know which implementation to use for a given data type/object.
  • Advantages of OOP:
    • Encapsulation: allows objects to be designed, implemented and tested independently.
    • Abstraction: no need for programmers to know the details of the implementation of objects in order to use them.
    • Inheritance: code re-use and new objects and new behaviours can be added easily from existing objects.
    • Easy maintanence.

Object scopes in OOP

  • There are three scopes for any object:
    • Public These objects(methods or attributes) can be freely used inside or outside of a class definition.
    • Protected Protected attributes/methods should not be used outside of the class definition, unless inside of a subclass definition.
    • Private This kind of attribute/method is inaccessible and invisible. It's neither possible to read nor write to those attributes/methods, except inside of the class definition itself.
  • Python notations(also known as name mangling) for scopes:
    • Public: just a plain identifier. For example, myName, can be defined using a specifier +, i.e. + myName in an class entity diagram
    • Protected: starting with one underscore and ending with one underscore. For example,_myAge_, can be defined using a specifier #, i.e, # myAge in a class entity diagram
    • Private: starting with two underscores and ending with two underscores. For example,__myAge__ in Python, can be defined using a specifier -, i.e. - myAge in a class entity diagram
  • Class variable and intance variable
    • A class variable is defined in a class and only one copy of this variable exists no matter how many instances have been created. A use case for this will be a number to be increased every time a new instance of the class has been created to act as a unique number for each instance.
    • An instance variable is defined in a class, a copy is created when an instance is created and is only visible, accessible and assciated with that instance.

Static, Abstract, Virtual Methods

  • Static methods are methods that are not bound to any specific instance. For example:
class People:
    def __init__(self, name):
	 self.name = name

     # non-static method with a self argument
    def sayHi(self):
	return 'Hello ' + self.name

    @staticmethod
    def getGreeting():
	return "Hello!"
  • Abstract methods are methods with no implementation. They are normally used to define a base class (thinking it as a class with just an empty shell). To use it, you MUST subclass the base class and override the abstract methods by implementing it. For example:
class abstractBaseClassExample(object):

    @abstractmethod
    def do_something(self):
	pass
  • Virtual methods are methods which have implementation, may be overridden. In Python, all methods are virtual.

click here for PPT used in lessons.