Skip to content

5) Objects and Object Oriented Programming

singhalmanu9 edited this page Dec 21, 2014 · 1 revision

Object-Oriented Programming

Java is an object-oriented programming language (OOP). This means that the basis of all programs in Java occur with Objects. An Object is anything that can do things. That's it. So, an Object could be a Car, or maybe an Elephant. Every Object is stored inside of a class, a file that describes what that Object is like and what it can do.

For example, the Car class would describe a Car, and the class might contain a model, a make, or a color. These descriptors are called fields and they simply describe some property of the Object.

Classes also contain "actions", known in Java as methods. Methods allow the Object to "act" and do something. For example, a Tank object might have methods fire(), moveForward(), or changeAngle(). The methods can act on the Object itself, or even another Object. For example, a Bug object might be able to eat() a Berry object. Methods can become very complex, so we will cover them in more detail in a later lesson.

Class Structures

Classes follow a typical structure. Following this structure allows people to easily understand your code.

Declaration

The first part of a class is the declaration, which follows this form:

public class YourClassName

{

/*actual code goes here*/

}

YourClassName is the name of the Object AND the name of the file you are storing the information in. IMPORTANT: If your file is not the same name as the class name, the compiler won't be able to find the code. For our purposes right now, this is the way all classes will start.

Fields

The next parts of the class are the fields, or the characteristics and properties that the Object will have. For a Car class, the fields may look like this:

public String model;

public int modelYear;

public String make;

NOTE: You do not have the initialize the fields immediately after the declaration. Save this step for the constructor.

Constructor

The constructor is the next part of the class, and this is where you initialize the fields. Constructors follow this pattern:

public YourClassName(whatever parameters you need)

{

/* initialize the fields */

}

A parameter is a value that you need for the constructor to work. For example, if you want to make a 2000 Toyota Corolla, your constructor would look like

public Car(int year, String company, String name)

{

model = name;

modelYear = year;

make = name;

}

This way, you can make whatever kind of Car you want, based on what parameters you enter.

Methods

Methods, as mentioned above, allow Objects to do things. Methods usually need parameters, but unlike constructors, methods often have a return statement. A return statement is what the method returns back to the program to use elsewhere. A method can return any Object. For example, you may have a method called increment, which takes in a number and returns that number plus 1. The increment method may look like:

public int increment(int number)

{

int newNumber = number + 1;

return newNumber;

}

The first line of the method tells the program what return type it will have, what its name is, and what parameters it will need. Inside the curly braces, the method increments the number and returns it.

And Finally...

COMMENTS! Comments are arguably the most important part of a class. If someone cannot read your code, then it is almost meaningless. Comments help users identify what does what in a program, and they are critical when you are working with multiple people on one project.