🌱 Tim's Dev Wiki

Search IconIcon to open search

Object Oriented Programming

Last updated December 19, 2022.

Object-oriented programming is a programming paradigm that sees programs as a collection of objects interacting with each other. Objects are just containers for some data (called ‘fields’) and functions (called ‘methods’).

Object-oriented programming involves heavy usage of classes and enable us to make use of concepts like encapsulation, inheritance, polymorphism, etc.

# Classes


# Static Classes


# Encapsulation

Encapsulation is about hiding the implementation details of a class as much as possible. Doing so will tend to minimise coupling.

Access modifiers Access modifiers are used to explicitly apply different levels of visibility to the members of a class. OO programming languages will usually give you the following 3 access modifiers.

Languages might have variations on how these access modifiers behave, as well as what the default visibility is when you omit the access modifier. Eg. In Java, a protected member is also visible to classes in the same package.

In general, prefer the most restricted visibility possible.

# Inheritance

Inheritance (also called ‘subclassing’) is when you make one class derive from a base class, causing the child class to inherit the parent class’ non-private members.

Differences between languages Languages will have different syntax for inheritance and ways for subclasses to access their parent class. They may also differ in whether they support multiple inheritance or not.

# Polymorphism


# Static Polymorphism


# Dynamic Polymorphism


How does the program know which method is the correct one to invoke at runtime? tl;dr — in C++, every class with virtual functions has its own virtual function table (an array of function pointers) created at compile-time that holds pointers to that class’ virtual methods. Every object, on creation, gets a pointer to the function table of its class. ( sourced from Trail of Bits)

# Method Overidding

Method overriding is when you make a subclass provide an implementation for a method that’s defined in its base class. Method overriding enables runtime polymorphism.

Differences between languages In many languages, overriding is implicitly done, but it’s recommended to explicitly label it.

# Abstract Class

An abstract class is just one you can’t instantiate, forcing the user to choose and instantiate a specific concrete subclass that inherits from the abstract class.

Abstract classes let you define both concrete methods and abstract methods. If you find that you don’t need to define concrete methods, which is usually the majority of the time, then consider using an interface instead.

Differences between languages Making a class abstract differs between programming languages.

# Abstract Method

You can only define abstract methods inside an abstract class. Abstract methods are ones that must be overridden and implemented by the subclasses. If you want to provide a default implementation and allow for subclasses to optionally override a method, use virtual methods instead.

You cannot make abstract methods static.

Differences between languages To define abstract methods:

# Virtual Method

Virtual methods are methods that can be overridden by subclasses. To force subclasses to implement a method, use abstract methods instead.

Differences between languages To define virtual methods:

# Interface

An interface is a ‘contract’ that defines what a user can do with the classes that implement that interface. You define the methods (and sometimes constants) that the interface supports, and then leave the implementation details to the subclasses. An interface is not a class, so you cannot instantiate it.

You cannot supply any implementation inside interfaces. If you want to provide some concrete methods, then consider using an abstract class instead. That being said, some programming languages like C# let you implement a static members inside an interface.

Differences between languages

# Criticisms of OOP

See Object-Oriented Programming is Bad.