Preface

I remember the exact moment when I’ve decided to write this book — this was in my first year Introduction to Computer Science course at 2000. The topic was polymorphism and I just can’t get it. The language of the course was Java, I did understand how inheritance work in Java, but this how-to didn’t “lay down to” the “theory” that I was toughed about polymorphism. I thought the problem was in my understanding of “theory”, so I’ve read alternative description of what polymorphism is (I want to tell you secretly, that there is many types of polymorphism and I’m telling you about subtyped polymorphism).

Looking back on this course I think that the main problem was, it was highly theoretical. You can’t explain “theory” around what “object” is and why it is “cool” to use them without providing any idea how this concept is translating to the “usual stuff” that you’ve learned before.

As I side note, I want to tell that I heard that in 1996 the exact same course was toughed in C++. What I heard that in home assignment they’ve implemented the “single inheritance” case using C. This is also overkill.

Some delicate balance should be provided. So, in my book I will not dig too deeply in the implementation detail, but I will provide you some model on what is going on low-level. This should be enough to understand what and why the program is doing.

For every topic there will be up to 4 chapters: simplified implementation, simplified theory, more on implementation, more theory. Sometimes I will omit some parts. Note the order: implementation first. Occasionally, I will provide you more than one implementation.

You can safely skip this paragraph. If you wounder what helped me understand subtyped polymorphism, it was actually simplified C++ implementation of the (single) inheritance. The virtual method was part of vtables (virtual tables). So, for compiler they kind of invisible (they are not part of the “struct"that defines the object). But in runtime compiler invoke some machinery to get “correct” version of method from vtable. So, the object has kind of two phases — the static one — that compiler use in compile time (no virtual method available) and dynamic one — one that compiler invoke in runtime (uses vtables).

You can even more safely skip this paragraph. In Java rolevtable plays .class object and looking up in vtables corresponds to traversing .class object hierarchy and searching in, so it was easy to me to translate this implementation to Java’s. The reason, that I can’t figure out it in Java was too many moving parts-compiler&JVM, while in C++ there is only compiler (that emits the code corresponding to vtablein compile time, while in Java case, this is part of JVM, but compiler also does type checks in compilation type).

You can safely skip this paragraph. The main example was Shape. You know Circle, Rectangle all are Shape. But it has a lot of more sense to see them as tagged union — Shape can be Circle or Rectangle or etc (this is called Sum type). For example, f you have some Painting Editor it is likely that you have fixed type of Shape and you can’t just add new Shape to it. Aha-moment for was when I’ve read about Door and LockableDoor that inherit from Door.Door has method open(). When 3rd-party object calls the object “the correct” method will be called (you can check it traversing .classhierarchy) this corresponds to intuition, when we opens the door, we usually do it automatically without noticing whether it lockable or not, we just opens the door. Inheritance is not only IS relationship, it is specialization.

In this book I will describe the following topics: Procedural programming, Object-oriented programming, some very basics of Functional Programming, basics of data-structures (dict and list).

This is not my first attempt to write such a book. My first attempt was in Java, than I’ve tried translate it to Kotlin. They where very detailed and I even didn’t get to Object Oriented. In this attempt I will just give a high-level overview of what Computer Science has to order.

About me: I have 20+ year experience in Java/Python. I have GitHub with some of my toys projects, just check for alex-ber in GitHub. Most of my experience is in backend both in Java and in Python.

You can ask me, why Python? Python is very popular language and it’s popularity is rising over the years. The main audience that pick it are data science. While this book provides good introduction for everybody, I think the data scientist may benefit from it — it provide the missing gap in the programming foundation. For example, what is generator? How it works internally? What is the simplest mental model of it? Does it function? What the statement “generator is also an iterator” really means?

This is not a book about Python. So, I will not cover all features that Python can present to us. Most of examples will be in Python, while in Object definition section I will some C code. No prior knowledge of C\C++ is assumed, but I will do explain what’s happening in C\C++ (only occasionally showing the actual code ), so knowing a bit C\C++ is a bonus. There will be also optional section with inheritance implementation in Java that you can skip.

Another point that I want to address here is the setup. For the beginners it is very difficult to set things up. The things getting more complicating providing that there is macOS, Linux, Windows, etc. Most of the books just avoids pointing out how to set things up or pointing to some resource that becomes not relevant any more (for example, never Python version has released and this books rely explicitly on Python 3.9). Likely in the last decade we have Docker technology that I can’t “write in stone” the code and it’s environment that I’m using. More on this, in the next chapter .

This story is part of my new book.

--

--

Senior Software Engineer at Pursway

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store