TURN INTO YOUR CLASS’ BASKET BY THE BEGINNING OF CLASS ON _________________ Name:
You down with OOP?
Date:
Period:
Object-Oriented Programming
Yeah, you know me! So, what exactly is Object-Oriented Programming? Object-Oriented Programming (OOP) is a programming method in which programmers build what are essentially computer-based simulations of real-world objects or abstract concepts. Before programmers widely adopting this paradigm, programmers employed procedural programming techniques that provided “Do this, then that” instructions to the computer. Now, OOP provides organization, simplifies large programs, and standardizes code so that it can be reused in a variety of ways. Since people deal with objects in the real world, it’s considered a more intuitive, and therefore easier, way to deal with things. Keep in mind, OOP may not necessarily seem valuable at first, but in the long run, it is an incredibly powerful way to program! Objects and Classes Classes provide the blueprint or template for what are called objects. From a class, zero or more objects may be created. As we can see with the cars below, we don’t have to create the car objects, just because we have the car class. Let’s go ahead, though, and create three specific car objects. These are often referred to as instances because each object is a separate instance—an occurrence—of a particular class.
Take a minute to think about cars. As a general class, there are certain things we know they all have in common: every car will have a make, model, year, and color. What makes each object, or instance, of a car unique, though, is the combination of these traits, along with many others. Your Chevy Nova is a very different car than your mom’s Honda minivan. That’s what makes them unique instances!
Page 1 of 6
TURN INTO YOUR CLASS’ BASKET BY THE BEGINNING OF CLASS ON _________________
The same could be said of each gender. There are certain traits and behaviors that we would attribute to boys or girls, but each individual boy or girl is just that - an individual!
The same could be said about apples: one general class, with a wide number of objects that are capable of being created. In this case, Red Delicious, Fuji, and Granny Smith are our three objects created from the Apple class.
Page 2 of 6
TURN INTO YOUR CLASS’ BASKET BY THE BEGINNING OF CLASS ON _________________ Properties and Behaviors Everything in the real world has properties, i.e., attributes, and behaviors. Think of properties as what the attributes or traits of an object are; it describes what it is. The behaviors are what an object can do.
Properties/Attributes: feather color size is a duck
Behaviors: can fly can fart can quack
At its most basic, an object consists of variables, which represent the object’s properties, and methods, which represent its behaviors. For the Car class below, take a look at the properties and behaviors.
Page 3 of 6
TURN INTO YOUR CLASS’ BASKET BY THE BEGINNING OF CLASS ON _________________ To recap: ■ OOP - Object Oriented Programming, a more real-world approach to programming, since it’s based on the idea of simulating real-life objects. ■ Class - A template or blueprint from which individual objects are created (or instantiated). ■ Object - An individual instance created from a class. ■ Properties - Traits or characteristics that describe what an object is; in an object, variables are used to represent its properties. ■ Behaviors - What an object can do; in an object, methods are used to represent its behaviors. Check yourself, before you wreck yourself... 1. What acts as the template or blueprint for an object? 2. What is another term for the objects that are created? 3. How many objects can be created from a class? 4. Finish the sentence: An object’s properties describe... 5. Finish the sentence: An object’s methods describe… 6. Refer to the Girl class above, then you decide, property or behavior? a. height b. eye color c. can twerk 7. Why is goFast()considered a behavior, and not a property?
Vroom! Vroom! (A.K.A. Creating an object) Let’s go back and take another look at our Car example. Remember, a class is made up of attributes and behaviors, as we’ve said before. When writing the class, though, variables are used to represent the attributes, and methods are used to represent the behaviors. Read the code on the following page, and pay attention to the variables and methods used. Also, as you read, see if there are any new terms used that you can figure out!
Page 4 of 6
TURN INTO YOUR CLASS’ BASKET BY THE BEGINNING OF CLASS ON _________________ public class Car { /* Instance variables the Car class' attributes */ private String make; private String model; private String color; private int year; /* Methods the Car class' behaviors */ public void goFast(){ System.out.println(“Vroom! Vroom!”); } public void honk(){ System.out.println(“beep beep”); } public String getMake(){ return make; } public void setMake(String make){ this.make = make; } public String getModel(){ return model; } public void setModel(String model){ this.model = model; } public String getColor(){ return color; } public void setColor(String color){ this.color = color; } public int getYear(){ return year; } public void setYear(int year){ this.year = year; } } Page 5 of 6
TURN INTO YOUR CLASS’ BASKET BY THE BEGINNING OF CLASS ON _________________ Take a look at these notes to clarify a bit more what you’ve been looking at.
As you can see, there are four “instance variables” that belong to our Carclass. You probably noticed the privateaccess modifier. In this case, privatemeans that no one else, other than the Car class itself, can access these variables! Another twist is that the variables we created here do not have values assigned to them. Yes, you can do that!
Count ‘em up! We have 10 methods in our Car class. Take a closer look at the thiskeyword followed by the dot found throughout. The this keyword is used to refer to an object’s instance variables. The keyword thisis used to reference the current object’s values. This is necessary because each object may have different values for their instance variables. When you say this, you are asking for the values set for the current object, not for the class or for any other instances of it. Now, once again, it’s time to check yourself... 8.
What is the name of the class?
9.
How many “instance variables” are there in the code? List them.
10.
What is the access modifier for these instance variables?
11.
How many methods are in this class?
12.
What does the keyword this refer to?
Page 6 of 6