java coding adventure

First hut on the trail: Basics of Object Oriented Programming (OOP)

Object Oriented Programming

In Java, you can write code that represents real objects.

It’s called OOP = Object Oriented Programming. If the programming is object oriented it must have objects. You object is a calculator. In real life, you also have an object that is known as a calculator. 

Object

Objects in Java can easily map to real world “objects” or more precisely to nouns: they’re used when you need a person, a place or a thing. You can have objects like calculator, but also a printer, or oldDaddysCar.

The object has to be created before you can use it. It’s the same as in real life: someone has to manufacture a car before you can ride it. A specific project, blueprint or template is needed to create an object. A ship blueprint for a ship or a baking form for a cake.

Class

It’s the same with objects. To create an object you need a template that describes how the object looks like, what it has inside and what it does. To create an object in Java you need a class.

When you look back at the project calculator you see there a line like this:

 public class Calculator 

It’s called a class definition and it tells you that the code describes a class. You will use the class to create an object this way:

 Calculator calculator = new Calculator(); 

Methods

The things an object can do in Java world are called methods (for other languages those may be functions or procedures). Methods do various stuff. Calculator’s methods do addition, multiplication, subtraction etc.

Methods names should be verbs (because they describe an action). Methods names are written from a small letter and the method should do only one thing.

Below is an example of a method that does the addition:

 public int add(int summand1, int summand2){
    return summand1+summand2;
}

Attributes

How the object looks like or what it has inside is described by attributes (class attributes). It can be for example color, shape, size, a number of digits, name… Everything really.

The calculator doesn’t have any attributes right now (you don’t need them to do the math), but we can specify the model or company name if needed:

String producentName = "Casio";
String model= "fx-350ES PLUS";

Taking it all together

A full class can look like this:

 public class Calculator{
    String producentName = "Casio";
    String model= "fx-350ES PLUS";

    public int add(int summand1, int summand2){
        return summand1+summand2;
    }
}

Take a next step on the path and read more about the methods and attributes at Potato Coding Adventure blog.

Advertisements

1 thought on “First hut on the trail: Basics of Object Oriented Programming (OOP)”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s