Skip to content

OOP Basics

Ken edited this page Jun 14, 2018 · 12 revisions

OOP stands for Object-Oriented Programming.

OK. Why are you telling me this?

Because if you know what OOP means, it will help you get into the right mindset when planning your code.


Here is an example of a something coded in a "procedural" manner:

Find car keys

  1. Move to Bedroom
  2. Search Room
  3. if Keys are Found then Go_To_Work and End Find_car_keys
  4. Move to Living Room
  5. Search Room
  6. if Keys are found then Go_To_Work and End Find_car_keys
  7. Move to Kitchen
  8. Search Room
  9. if Keys are Found then Go_To_Work and End Find_car_keys
  10. Move to Bathroom
  11. Search Room
  12. if Keys are Found then Go_To_Work and End Find_car_keys

Pretty simple right? Just follow the steps. But what if we have more rooms? The number of steps could get very high. What if we want to change it to look for something other than keys?


Here is the same example in a OOP manner:

Class FindCarKeys
variable room
variable keysFound
variable searching

Method Search(parameter room)

  1. set searching to true
  2. if the current room is not equal to room then move to room
  3. search the room
  4. if keys are found then set keysFound to true
  5. if keys are not found then set searching to false

Class Main
variable rooms

On Startup

  1. set rooms to a list of the rooms in the house
  2. create an instance of the class FindCarKeys

Every 10 seconds

  1. if FindCarKeys.searching is false and FindCarKeys.keysFound is false. . .
  2. then run FindCarKeys.Search(a unsearched room in rooms)

Now this looks more complicated. Here we are using two "objects". The Class Main will start automatically (don't question it). Using this logic, it doesn't matter how many rooms we have. And if we wanted to change what we are looking for, we should only need to change a couple lines. And if we wanted to add another task, we would just create another Class and call it.


Important bits to understand

  • Class
  • Variable
  • Method
  • Parameter

I will explain this as if you are God. This seems reasonable and has little chance of offending anyone. As God you decide to create something new. It just starts to exist because you will it. You would also like all of your current things in the universe to be able to see it and interact with it.

public class Something()

If you decided that you wanted it to be secret and hidden from the five senses you would say:

private class Something()

Why did you put parentheses after it?

I will get to that later, just know that they're there and they need to be. Pay attention and stop interrupting me.


So let's talk about Fields.

Fields are variables that are stored in the Class. I am going to assume that you understand basic algebra. It's the same concept. When you say X = 10.1, you are actually saying:

This variable is a decimal with the value of 10.1. I shall name it "X".

Now let's see how that would look in a Class. Instead of a decimal, we are going to make it a "float". A float is just like a decimal. Just don't question it for now. In fact, from now on, when you are thinking of a decimal you are mistaken. You use float.

public class Something()
{
    float X;
}

The squiggly brackets just mean that the code within them belongs to the Class.

You mean curly braces?

Yeah, sure. Whatever.

I thought it was exposed to look like this:

public class Something(){
    float X;
}

Well, for one; It doesn't matter. I like to place my curly braces on a new line because it's easier to understand what's going on when you're knee-deep in code. For two; I think what you meant to say was: "I thought it was supposed to look like this". So, how about we learn how to spell first?

I'm not actually another person. You just pointed out your own spelling mistake.

Alright Mr. Smart-Ass. It looks like the student has become the teacher. I guess we are done here.

Clone this wiki locally