Object-Oriented Programming (OOP)


CodeBooks

Object-oriented programming (OOP) is a type of programming in which you create objects. Examples of object-oriented languages are Java and C#.

Objects in an object-oriented language typically have four main components: a class, fields, methods, and one or more constructors.

In this example, we will walk through how to represent a a high-school course in an object-oriented programming language (Java).

Class

A class is like a template for an object. A class specifies what fields, methods, and constructors an object will have when it is created.

For our example, we want a standard class named Course. When you create a class, it obviously starts out empty.


        

Inside the brackets is where the other three types of components are specified. Anything in the brackets belongs to the class.

Fields

Fields are pieces of data that are stored in an object. You declare a field just like you create a variable in a method, but with public or private before the type. We want our Course class to store the course name, teacher, room number, and period.


        

Because the field is private, the only way to access it is within the class. Other classes will not be able to access the name, teacher, etc. We will solve this problem in the next section.

Methods

Methods define actions that an object can do. In our case, the first set of actions we want our course object to do is retrieve its fields so that other classes can access them.

A method that simply gets a value is called a getters. (The opposite is called a setter, and it just takes a value and puts it directly to the corresponding field. Getters and setters together are called accessors.)


        

The next method we want to add will enroll a student by name. To do that, we need to first create a list of students, and then add a method to add the new student to the list.


        

The last thing we need to do is add a method to check whether a particular student is enrolled. This will simply delegate (hand off the processing) to students.contains(), because that's all we really need to do - check if the list of enrolled students contains the given name.


        

Constructors

Constructors are similar to methods, but they are called once and only once for each object that is created, as it is created. Constructors often construct the object, funnily enough.

The syntax for a constructor is very similar to a method, except for two differences: there is no return type, and the name is the same as the class name (including casing).

As you may have realized, the name, teacher, roomNumber, and period fields are never initialized. Normally, they would just be initialized in the declaration, but because want there to be different values for each Course object, we need to set them in the constructor.


        

As you can see, we have a field and a parameter both named name. If we just do name = name;, won't it set the parameter name to itself, and do nothing?

Yes, but the difference here is this.name = name;. this.name always refers to a field rather than a parameter or variable. So, we're actually setting all of the fields to the parameter values.