Object Oriented Programming

Object-Oriented Programming 101

Overview & Purpose
The purpose of this lesson is to introduce the concepts of object-oriented programming in the most abstract way possible without any specific code examples.
Key Concepts
- Classes and Objects
- State
- Inheritance
Classes and Objects (whats the difference)
- A Class is a way to describe a certian type of object, its data and behavior.
- An object is described as an individual "instance" of a Class with its own dynamic data.
- The behavior of an object is defined by the functions of its Class.
Example:
- Imagine a video game with three wizard characters. Each character has superficial differences, but many common attributes and abilities.
- In this context, the abstract concept of a “wizard” is the Wizard Class and the individual wizard entity (Merlin) is a wizard object.
- The Wizard Class has the behaviors “Heal”, “Lightning” and “Blizzard”, therefore each individual wizard will also have these behaviors.
*note Once created, an object occupies a persistent spot in memory, and can be revisited at later points in your program. This is particularly useful in games, 3D applications and user interfaces. {.is-success}
State
Each individual object holds data relevant to its behavior. This dynamic, changing information is known as an object's “state”.
Example:
- Continuing with the video game example, imagine, a Warrior Class with three instances, Alucard, Link and Dragonborne.
- Each of these individual characters will need to keep track of data relevant to their current situation such as Health and stamina levels.
- If Alucard takes damage (a behavior), his state will be directly affected in the form of his health being depleted.
Inheritance
Classes are often very similar. They share common logic, but they’re not entirely the same. Consider the case of the Wizard and Warrior. While each has exclusive behaviors such as chop, charge, heal, lightning, etc., many behaviors such as run, walk, and jump are commonly shared between the Wizard and Warrior. This shared, branching functionality can be achieved without reinventing the wheel each time through “class inheritance”
Example:
- Here we first define an Abstract Class called “Player” in which we implement the shared behavior run, walk, jump.
- We then create two subclasses of Player (Wizard and Warrior) and implement exclusive behavior for each: heal, lightning, blizzard and chop, charge, yell
{.is-info}
Summary
In summary, objects are a usefull way to package up data and behavior, and classes are a usefull way to describe standardized Types of these objects.


{.is-info}