We'll now begin our journey into the world of object-oriented programming. We'll start with focusing on describing concepts and data using objects. From there on, we'll learn how to add functionality, i.e., methods to our program. Show
Object-oriented programming is concerned with isolating concepts of a problem domain into separate entities and then using those entities to solve problems. Concepts related to a problem can only be considered once they've been identified. In other words, we can form abstractions from problems that make those problems easier to approach. Once concepts related to a given problem have been identified, we can also begin to build constructs that represent them into programs. These constructs, and the individual instances that are formed from them, i.e., objects, are used in solving the problem. The statement "programs are built from small, clear, and cooperative objects" may not make much sense yet. However, it will appear more sensible as we progress through the course, perhaps even self-evident. Classes and ObjectsWe've already used some of the classes and objects provided by Java. A class defines the attributes of objects, i.e., the information related to them (instance variables), and their commands, i.e., their methods. The values of instance (i.e., object) variables define the internal state of an individual object, whereas methods define the functionality it offers. A Method is a piece of source code written inside a class that's been named and has the ability to be called. A method is always part of some class and is often used to modify the internal state of an object instantiated from a class. As an example,
An object is always instantiated by calling a method that created an object, i.e., a constructor by using the Loading Loading Creating ClassesA class specifies what the objects instantiated from it are like.
We'll now familiarize ourselves with creating our own classes and defining the variable that belong to them. A class is defined to represent some meaningful entity, where a "meaningful entity" often refers to a real-world object or concept. If a computer program had to process personal information, it would perhaps be meaningful to define a seperate class Let's begin. We'll assume that we have a project template that has an empty main program:
Let's create a class named After creating a new
file in NetBeans, the current state is as follows. In the image below, the class
You can also draw a class diagram to depict a class. We'll become familiar with its notations as we go along. An empty person-named class looks like this:
A class defines the attributes and behaviors of objects that are created from it. Let's decide that each person object has a name and an age. It's natural to represent the name as a string, and the age as an integer. We'll go ahead and add these to our blueprint:
We specify above that each object created from the Instance variables are written on the lines following the class definition In the class diagram, the variables associated with the class are defined as "variableName: variableType". The minus sign before the variable name indicates that the variable is encapsulated (it has the keyword private).
We have now defined a blueprint — a class —
for the person object. Each new person object has the variables Loading Defining a ConstructorWe want to set an initial state for an object that's created. Custom objects are created the same way as objects from pre-made Java classes, such as
This is achieved by defining the method that creates the object, i.e., its constructor. The constructor is defined after the instance variables. In the following example, a constructor is defined for the Person class, which can be used to create a new Person object. The constructor sets the age of the object being created to 0, and the string passed to the constructor as a parameter as its name:
The constructor's name is always the same as the class name. The class in the example above is named Person, so the constructor will also have to be named Person. The constructor is also provided, as a parameter, the name of the person object to be created. The parameter is enclosed in parentheses and follows the
constructor's name. The parentheses that contain optional parameters are followed by curly brackets. In between these brackets is the source code that the program executes when the constructor is called (e.g., Objects are always created using a constructor. A few things to note: the constructor contains the expression
Loading Defining Methods For an ObjectWe know how to create an object and initialize its variables. However, an object also needs methods to be able to do anything. As we've learned, a method is a named section of source code inside a class which can be invoked.
A method is written inside of the class beneath the constructor. The method name is preceded by In addition to the class
name, instance variables and constructor, the class diagram now also includes the method
The method Let's create three persons in the main program and request them to print themselves:
Prints: Sample output Ada, age 0 years Antti, age 0 years Martin, age 0 years This as a screencast: Loading Loading Loading Changing an Instance Variable's Value in a MethodLet's add a method to the previously created person class that increments the age of the person by a year.
The method is written inside the The class diagram also gets an update.
Let's call the method and see what happens:
The program's print output is as follows: Sample output Ada, age 0 years Antti, age 0 years Ada, age 2 years Antti, age 0 years That is to say that when the two objects are "born" they're both zero-years old ( The method can also contain conditional statements and loops. The growOlder method below limits aging to 30 years.
Loading Loading Returning a Value From a MethodA method can return a value. The methods we've created in our objects haven't so far returned anything. This has been marked by typing the keyword void in the method definition.
The keyword void means that the method does not return a value. If we want the method to return a value, we need to replace the
The method above returns an
Sample output The grade received is 10 The method's return value is assigned to a variable of type
Sample output Grading average 10.0 All the variables we've encountered so far can also be returned by a method. To sum:
Let's continue with the Person
class and add a
The class in its entirety:
Let's illustrate how the method works:
Sample output Pekka's age 2 Antti's age 1 Pekka's and Antti's combined age 3 years Loading Loading As we came to notice, methods can contain source code in the same way as other parts of our program. Methods can have conditionals or loops, and other methods can also be called from them. Let's now write a method for the person that determines if the person is of legal age. The method returns a boolean - either
And let's test it out:
Sample output underage: Antti, age 1 years of legal age: Pekka, age 30 years Let's fine-tune the solution a bit more. In its current form, a person can only be "printed" in a way that includes both the name and the age. Situations exist, however, where we may only want to know the name of an object. Let's write a separate method for this use case:
The The class as a whole:
Let's mould the main program to use the new "getter" method:
The print output is starting to turn out quit neat: Sample output Antti is underage Pekka is of legal age Loading A string representation of an object and the toString-methodWe are guilty of programming in a somewhat poor style by creating a method for printing the object, i.e., the
The The method is used in a somewhat surprising way:
In principle, the
Java extends the call at run time to the following form:
As
such, the call We can remove the now obsolete The second part of the screencast: Loading Method parametersLet's continue with the
The instance variables
Prints: Sample output Matti, body mass index is 26.54320987654321 Juhana, body mass index is 20.897959183673468 A parameter and instance variable having the same name!In the preceding example, the
The parameter's name could also be the same as the instance variable's, so the following would also work:
In this case,
Loading Calling an internal methodThe object may also call its methods. For example, if we wanted the string representation returned by toString to also tell of a person's body mass index, the object's own
So, when an object calls an internal method, the name of the method and this prefix suffice. An alternative way is to call the object's own method in the form
The screencast's third part: Loading Loading What are objects used for in programming?An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.
What is object object program?Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or properties), and the code is in the form of procedures (often known as methods).
What is necessary before an object can be used in a program?In order to work with the object in code, you need some way to refer to the object. That's where the reference variable comes in. The reference variable doesn't hold an actual piece of data that your program will work with. Instead, it holds the object's memory address.
How objects are used in objectObject-oriented programming, or OOP, is an approach to problem solving where all computations are carried out using objects. An object is a component of a program that knows how to perform certain actions and how to interact with other elements of the program. Objects are the basic units of object-oriented programming.
|