Master Object-Oriented Programming in Python

Part 2: Designing Class Hierarchy

Christian Yurianja
Python in Plain English

--

I want to give you an explanation using my “News Scraper”. But unfortunately, the web that I scraped already change its webpage into a dynamic page. It still can be scraped using other methods, but for OOP explanation I decide to write other simple code for better understanding.

Class Diagram

On above UML class diagram, it is the simplest form of using OOP. I create a class with the name Book, which has 5 attributes which are: title, author, pages, price, and bookList and 1 behavior: showBook.
Remember, attributes same with properties, behavior same with methods/function.
After creating a class, you might want to group classes into one more general category. So, let see below an example of a diagram with some classes and their connection.

This diagram shows that there are five classes that I created which are: Publication, Book, Periodical, Magazine, and Newspaper.
If you see the arrow that connects the classes

  • The Magazine and Newspaper connected to Periodical class
  • Book and Periodical class connected to Publication class.

It means that:

  • Magazine and Newspaper inherited from Periodical class,
  • Book and Periodical classes are inherited from the Publication class.

You can also say that

  • Publication is the parent/superclass of Book and Periodical classes
  • Periodical class is parent/superclass of Magazine and Newspaper.

You might be wondering, What has inherited means? What is the parent/superclass means?

Okay, let me explain the 4 fundamental ideas in Object-Oriented Programming first, A PIE (Abstraction, Polymorphism, Inheritance, and Encapsulation). Each of them has meaning:

  • Abstraction
    Abstraction means the idea or concept of a person is completely separate from any specific instance. It is the heart of Object-Oriented programming because it’s what we are doing when we make a class. So, when creating a program you should think, what is the same attribute of several objects so I can group them together?
    Without abstraction, you will never create a class, because you don’t think about it.
    In the above diagram, I have created 5 classes, which means I can create as many objects as I want without defining their attributes or behavior one by one, and it's easier to manage.
  • Polymorphism
    The simple means of polymorphism is having many forms. In programming, we can recognize it by using term overriding and overloading. Overriding means that you inherit the method from the superclass, then you modify the method exclusive for the class itself. Although the output is the same, the attribute might have a different value. While overloading means that, you inherit the same method but modify it by using different kinds of input parameters which might change the output.
  • Inheritance
    When creating a new class, it might be easier to use another class that has some attributes and/or the same methods as a base instead of creating it from scratch. Like saying “don’t reinvent the wheel”, this process of using already made classes will make your job more efficient and manageable. That is the idea of inheritance. You take advantage of the class that has the same attribute and method. This way, if you want to change some value or method, you don’t need to change it from a different place. Just change it on the parent class, and all will follow.
    In the above diagram, we implement the inheritance, marked by using the empty arrow that connects with its parent/superclass.
  • Encapsulation
    The idea of encapsulation is containing the elements of an object. Not just to keep them together, but to also protect them. This is to restrict access to some of the object’s components.

You shouldn’t always be forced to use all of these 4 ideas, as you can modify them along the way as needed.

After creating the class diagram, you’ll have a better insight into how you will create your program.

See you in the next article

All Mastering Object-Oriented Programming in Python series:

  1. Master Object-Oriented Programming in Python — Part 1: What is OOP?
  2. Master Object-Oriented Programming in Python — Part 2: Designing Class Hierarchy
  3. Mastering Object-Oriented Programming in Python — Part 3: Implementing OOP!

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Join our community Discord.

--

--

9K+ Views on Medium || I am a Technology and Software Developer. Contact me via Upwork here : https://s.id/chrisanja