What are Classes for in programming? (an answer for beginners)

chefvivica
3 min readApr 14, 2020

Beginners are taught to use classes and object-oriented programming techniques very early. But are never told why.

Instead of telling us why, explanations always start describing generic real life objects — cars are a popular example… and students start discussing makes and models.

But WHY? What are classes for?

The simple answer,

Programmers use classes whenever they need more than one item.

Take this example from a popular game, Candy Crush.

You see seven blue candies on the screen? A game developer wouldn’t code a blue candy seven times. You create one Blue Candy class and multiply it seven times.

Classes are just candy factories. Or car factories. Or baby factories. Or koala factories. You define the thing once, and let your code create as many as you want!

What is Object-oriented programming (OOP)?

OOP = Using Classes in your programming

A few official definitions we need to talk about first.

1. What is an object?

In code, we combine a group of related variables and methods (functions) into a unit, we called that unit an object.

2. What are classes?

Classes are the blueprints that define the behavior and information our objects will contain. They let us manufacture and instantiate new instances.

3. What is an instance method?

An instance method is the behavior of Objects.

We know that classes act as a factory for our objects, capable of instantiating new instances. what if we want to ask this object to do something?

Here we will use an example to better display this abstract concept:

Let’s say we are helping the Australian government build an app to adopt koalas. Koalas will all have a name, age, weight, etc. Imagine we have 50 koalas for adoption, and we describe each with 5 characteristics. Without OOP, it would take you 50x5 = 250 variables and complex referencing. With OOP, we can describe it easily with less code - one koala class x 50 instances. To keep it simple, we don’t want to keep repeating the same code over and over.

class Koala      attr_accessor :name, :weight, :age      @@all =[]

def initialize(name)
@name = name @weight = weight @age = age Koala.all << self end
def self.all
@@all end def donation

puts " Thank you for helping me! "
endend

So by doing this, we can create as many koalas as you want (just call Koala.new method), and can make all of them say “Thank you for helping me” .( just call the donation method on the instance koala)

The principle for OO design:

  1. The single responsibility and Separation of Concerns

do one thing each time

2. Abstraction and Don’t Repeat Yourself(DRY)

Make your code reuse-able instead of copy/paste over and over

3. Line limits on Methods and classes

that methods should not exceed 5 lines of code and classes should not exceed 100

The benefits of OOP :

  1. Encapsulation
  2. Abstraction
  3. Inheritance
  4. Polymorphism

I believe that there are a lot of resources for explaining the four main concepts for OOP, here is the best video that I found on youtube which will be great for you to know more about OOP.

Please follow me on twitter @chefvivica

--

--