Object-Oriented Programming

The Fundamentals of OOP

Lesson Goals

  • Understand the basics of Object-Oriented Programming.
    • Classes
    • Objects
    • Fields
    • Properties
    • Methods
  • Create and Use Objects
  • Learning the benefits of scope.

Introduction of Objects

Object-Oriented programming is a way to structure our programs and begin the process of encapsulation based on functionality and tasks. This better helps us and others understand how a program should function, what different parts do and how we can use them.

Think of an object in the real world. Now, think about all the things it can do. Hold that thought in reserve for a moment, we'll get back to it.

Attributes of Objects

An object contains two things: State and Behavior

When we talk about objects we should talk about them in terms of their States and their Behaviors.

Remember that object we were thinking about? I'm thinking about a Cat.

=^._.^= ∫

System.out.println("*meow*")
Let's talk about it in terms of the State and Behavior it has.

State of an Object

=^._.^= ∫

The State of an object describes it. When we think about how to describe a Cat we get some of the following:

  • Name
  • Age
  • Fur Color

Of course, a Cat has its own name, but we're thinking platonically right now. Don't worry, we'll get there.

What other things could we use to describe this Cat or the objects you're thinking of?

Behavior of an Object

When we talk about an object's behavior, we're talking about the actions it can perform. Talking about our Cat again, what are some things it could do?

Here are a few:

  • Eat
  • Meow
  • Walk
  • Pee in Bed

Now that we can describe our Cat and talk about some of the things it can do, let's make a Cat.

It's Alive?

Not yet but, we're working on it.

Objects need to be created first. Let's make a Cat class. All classes use the keyword class. We should always name classes after the object they represent.

public class Cat{

/// =^._.^= ∫ Um there's nothing here?

}
Shush Cat. You don't even exist yet!

Above, we are defining a class Cat. As the technically non-existent cat said, it doesn't contain anything.

The Building-Blocks of Life

Now that we have our cat class we'll need to add some things

public class Cat{

    private String name;
    private String furColor;
    private int age;

    /// =^._.^= ∫ There I am?
}

Cats are so impatient.

These are fields. In most cases, they are normal variables but are very important. These fields hold data for the instance(remember that word) of the class. In this example, we have set them to private which means cannot be access from outside the class.

=^._.^= ∫ I'm trapped?

Let's say contained. To access these fields, and pass them data externally, we'll need another tool.

Properties

public class Cat{

    private String name;
    private String furColor;
    private int age;
    private int lives;

    public String getName(){
        return name;
    }

    //rest of getters

}

Our first tool is called a property or an accessor. It allows us to get the data in those private fields from outside the class.

Constructors

Now that we have Properties to access the fields, we still need a way to build our Cat.

=^._.^= ∫ We're not done?

No, patience. We haven't given you a way to be constructed yet.

public class Cat{

public Cat(String name, String furColor, int age, int lives){
    this.name = name;
    this.furColor = furColor;
    this.age = age;
    this.lives = lives;
    }
}
This is a constructor. A constructor is a special kind of method that is responsible for creating an instance of our Cat class. It uses the arguments given between the () to initialize our fields with some starting values

=^._.^= ∫ Wow! that was a lot of typing just to make me!

The Cat is right. That is a lot of work. We're developers, not typists.

We have our Cat class. Lets head over to the App class where the main method of our program is contained. Let's create our Cat.

Here, we start by declaring class Cat.

Cat annoyingCat = new Cat("Annoying", "blue", 4, 9); 
Next we give it a name. Just like declaring a variable.

Cat annoyingCat = new Cat("Annoying", "blue", 4, 9); 
Making use of the new keyword, we are saying this is a new instance of this class.

Cat annoyingCat = new Cat("Annoying", "blue", 4, 9); 
Finally, inside the ()that represents our constructor, we type information about our Cat. Name: Annoying, FurColor: Blue, Age: 4, and of course, Lives: 9

Cat annoyingCat = new Cat("Annoying", "blue", 4, 9); 

Here's our Cat. With the instance name and the . operator(dot operator) followed by a class property, we can learn about our cat.

    public class App
    {
        Cat annoying = new Cat("Annoying", "blue", 4, 9);

        public static void Main (String[]args){
         System.out.println(" Tell us about yourself.");
         System.out.println("I'm"  + annoying.getName() + "! I have "
         + annoying.getFurColor() + " fur."
         "I'm" + annoying.getAge() + " years old, and I have "
         + annoying.getLives() + " lives left!" );
        }

    }

=^._.^= ∫ That's really cool, but…I can't do anything.

The cat is right again. Our Cat has state but no behavior Let's make a method.

Methods

Methods are the behaviors of a class.

This is a lot of code junking up our program class.

public static void main (String[]args){
         System.out.println(" Tell us about yourself.");
         System.out.println("I'm"  + annoying.getName() + "! I have "
         + annoying.getFurColor() + " fur."
         "I'm" + annoying.getAge() + " years old, and I have "
         + annoying.getLives() + " lives left!" );
        }

We can make a method that does the same thing in our Cat class and then use it in our program class.

Below our constructor we've created a new method and simply taken the code from our program class and moved it to this method here.

public class Cat{

public Cat(String name, String furColor, int age, int lives){
    this.name = name;
    this.furColor = furColor;
    this.age = age;
    this.lives = lives;
    }

 public void greeting()
 {
    System.out.println("I'm" + name + "! I have "
    + furColor + " fur. I'm" + age + "years old, and I have "
    + lives + " lives left!");
 }
}

This might be a lot to unpack at a glance so let's break it down.

First we declare the method type. In this case, it's public. Which allows it to be accessed externally.

public void greeting()
Next is the return type. Our return type is void. This means the method doesn't return any new data.

public void greeting()
This is the name of Method. We should name this after the behavior we're modeling. This is a method for the cat to say hello. Let's call it Greeting.

public void greeting()
After throwing on a pair of {}, we're ready to place our code from the program class in between them.

public void greeting(){}

If the cat can speak English is it really a Cat? Never mind. We've gone way too far to think about that now.

Now that we have our method, we can use it in our program class.

public static void main (String[]args){
         System.out.println(" Tell us about yourself.");
         annoying.greeting();
        }

Look at how this cleans things up!

=^._.^= I can do stuff now! It's a little boring if all I can do is say hello, though.

How our cat greet()s has been moved outside of our main method. The main method still knows that our annoying cat greets us, but it no longer knows the details of how that's done. Instead, the knowledge of how a cat greets us lives inside the Cat class, right alongside all the data that's needed to actually Greet!

This is the core idea of OOP.

Group related state (fields) and the behaviors that act on that state (methods) into classes. Hide as much detail as you can inside the class's scope, and allow other code to work with that code using the methods and properties the class chooses to expose to outside code.

Let's look at the instance of our Cat class again. Before we used our constructor to determine all the values of the Cat every time we created an instance of it. Instead of doing that, we can hard code some of the values into our class..

public class Cat
{
   public Cat(String name, string furColor){
       this.name = name;
       this.furColor = furColor;
       age = 4;
       lives = 9;
   }
}

Now, we when make an instance of our cat class, we only have to give it a name and fur color. The other values are part of it from the beginning. All Cats have nine lives, right?

=^._.^= ∫ Sure do!