Last Updated:

Object-Oriented Programming in Java

You are familiar with the concepts of a class, an object, a constructor, object methods, and object variables. Understand that the class defines the methods of an object and that the values of the variables of the instance (object) depend on the object. You know how to create classes and objects, and you know how to use objects in your programs.

Now we will begin our journey into the world of object-oriented programming. Let's start by describing concepts and data using objects. From now on, we will learn how to add functionality, that is, methods to our program.

Object-oriented programming is concerned with separating problem area concepts into separate entities and then using those entities to solve problems. Concepts related to a problem can only be considered after they have been identified. In other words, we can form abstractions from problems that make it easier to approach those problems.

 

Once the concepts associated with a given problem have been identified, we can also begin to create constructs that represent them in programs. These constructs and the individual instances that are formed from them, that is, objects, are used in solving the problem. The statement "programs are built from small, understandable and cooperative objects" may not make much sense yet. However, as you progress through the course, this will seem more reasonable, perhaps even self-evident.

 

Classes and objects

 

A class defines the attributes of objects, that is, the information associated with them (instance variables), and their commands, i.e., their methods. The values of the variables of an instance (i.e., an object) determine the internal state of an individual object, while methods define the functionality it offers.

A method is a piece of source code written inside a class that has been named and can be called. A method is always part of a class and is often used to change the internal state of an object created from a class.

For example, ArrayList is a class offered by Java, and we used objects created from it in our programs. Below, an ArrayList object named integers is created and some integers are added to it.

we create an object from the ArrayList class named integers
ArrayList<Integer> integers = new ArrayList<>();

let's add the values 15, 34, 65, 111 to the integers of the integers.add(15) object
;
integers.add(34);
integers.add(65);
integers.add(111);

we print the size of the integer
object System.out.println(integers.size());

An object is always created by calling the method that created the object, that is, the constructor using the new keyword.

Creating Classes

A class defines what objects created from it look like. Object variables (instance variables) define the internal state of an object. The methods of an object determine what the object does.
Now we'll be familiar with creating our own classes and defining the variable that belongs to them.

A class is defined as representing some significant entity, where a "significant entity" often refers to a real object or concept. If a computer program were to process personal information, it might be appropriate to define a separate person class consisting of methods and attributes associated with a person.

Let's get started. Suppose we have a project template with an empty main program:

public class Main {

public static void main(String[] args) {

}
}

To create a new class

In NetBeans, you can create a new class by going to the Projects section on the left, right-clicking New, and then clicking the java class. In the dialog box that appears, you give the class a name.

As with variables and methods, the class name should be as descriptive as possible. Usually, the class continues to live and takes on a different form as the program develops. Therefore, the class may need to be renamed at some later point.

Let's create a class named Person. For this class, we create a separate file named Person.java. Our program now consists of two separate files, as the main program is also in its own file. In Person.java file natively contains the definition of the publicclassPerson class and curly braces that limit the contents of the class.

publicclassPerson {

}

After you create a new file in NetBeans, the current state is as follows: In the image below, a class member has been added to SandboxExercise.

object oriented programming

You can also draw a class diagram to depict a class. Along the way, we will get acquainted with its designations. An empty class with a person's name looks like this:

Person

A class defines the attributes and behavior of objects created from it. Let's decide that every person object has a name and an age. It is natural to think of a name as a string and age as an integer.

We'll go ahead and add them to our plan:

publicclassPerson {
private String name;
privateint age;

}

Above, we point out that each object created from the Person class has a name and an age. Variables defined within a class are called instance variables, feature fields, or feature attributes. There seem to be other names.

Instance variables are written in the strings that follow the definition of the publicclassPerson { class. Each variable is preceded by the private keyword. The private keyword means that the variables are "hidden" inside the object. This is called encapsulation.

In a class diagram, variables associated with a class are defined as "variableName: variableType". A minus sign before the variable name indicates that the variable is encapsulated (it has the private keyword).

We have now defined a schema—a class—for the person object. Each new person object has variables of name and age, which can contain values that are specific to a particular object. A person's "condition" consists of meanings assigned to their name and age.

Designer Definition

We want to set the initial state for the created object. Custom objects are created in the same way as objects from pre-built Java classes, such as ArrayList, using the new keyword. It would be convenient to pass values to the variables of this object as it is created. For example, when you create a new person object, it is useful to be able to give it a name:

publicstaticvoidmain(String[] args) {
Person ada = new Person(«Ada»);
// …

}

This is accomplished by defining a method that creates the object, i.e., its constructor. The constructor is defined after the instance variables. The following example defines a constructor for the Person class that you can use to create a new Person object. The constructor sets the age of the object to be created to 0, and the string passed to the constructor as a parameter as its name:

publicclassPerson {
private String name;
privateint age;

public Person(String initialName) {
this.age = 0;
this.name = initialName;

}
}

The name of the constructor is always the same as the name of the class. The class in the example above is named Person, so the constructor must also be named Person. The constructor also provides as a parameter the name of the person object to create. The parameter is enclosed in parentheses and follows the name of the constructor.

Curly braces follow the brackets that contain optional parameters. Between these parentheses is the source code that the program executes when the constructor is called (for example, newPerson ("Ada")). Objects are always created using the constructor.

A few things to note are that the constructor contains the expression this.age = 0. This expression sets the age of the instance variable of the newly created object (that is, the age of "this" object) to 0. The second expression this.name = initialName similarly assigns the string passed as a parameter to the name of the variable instance of the created object.

Defining Methods for an Object

We know how to create an object and initialize its variables. However, an object also needs methods to be able to do anything. As we learned, a method is a named section of source code inside a class that can be called.

publicclassPerson {
private String name;
privateint age;

public Person(String initialName) {
this.age = 0;
this.name = initialName;

}

public void printPerson() {
System.out.println(this.name + «, age » + this.age + » years»);

}
}

The method is written inside the class below the constructor. The method name precedes publicvoid because the method is designed to be visible to the outside world (public), and it does not return a value (void).

Objects and the Static Modifier

We used the static modifier in some of the methods we wrote. The static modifier indicates that the method in question does not belong to the object and therefore cannot be used to access variables belonging to the objects.

In the future, our methods will not include the static keyword if they are used to process information about objects created from this class. If a method takes as parameters all the variables whose values it uses, it can have a static modifier.

In addition to the class name, instance variables, and constructor, the class diagram now also includes the printPerson method. Because the method comes with the public modifier, the method name has a plus prefix. No parameters are defined for the method, so nothing fits in the method brackets. The method is also marked with information indicating that it does not return a value, here void.

The printPerson method contains a single line of code that uses the name and age of the instance's variables—the class diagram says nothing about its internal implementations. Instance variables are named with the prefix this. All object variables are visible and accessible from the method.

Let's create three people in the main program and ask them to print themselves:

publicclassMain {

public static void main(String[] args) {
Person ada = new Person(«Ada»);
Person antti = new Person(«Antti»);
Person martin = new Person(«Martin»);

ada.printPerson();
antti.printPerson();
martin.printPerson();

}
}