CLASSES AND OBJECTS IN JAVA
Now that you have a basic understanding of the core principles behind Object-Oriented Programming (OOP) in Java, it’s time to delve deeper into the building blocks that make OOP so powerful: classes and objects. These two concepts are fundamental to creating structured and reusable code in Java.
A class is essentially a blueprint or template that defines the properties and behaviors of objects. In other words, a class describes what an object will look like and what it can do, but it doesn’t represent the actual object itself. On the other hand, an object is an instance of a class, an actual entity that you can manipulate in your program.
In this section, we will explore how classes and objects work in Java, looking at their structure, how to create them, and how they interact with each other. You’ll also learn about key concepts such as constructors and the process of initializing objects, which set the foundation for creating effective and maintainable Java programs.
By the end of this section, you will have the knowledge to design your own classes, instantiate objects, and understand how these components form the backbone of object-oriented software. Let's get started!
A class is essentially a blueprint or template that defines the properties and behaviors of objects. In other words, a class describes what an object will look like and what it can do, but it doesn’t represent the actual object itself. On the other hand, an object is an instance of a class, an actual entity that you can manipulate in your program.
In this section, we will explore how classes and objects work in Java, looking at their structure, how to create them, and how they interact with each other. You’ll also learn about key concepts such as constructors and the process of initializing objects, which set the foundation for creating effective and maintainable Java programs.
By the end of this section, you will have the knowledge to design your own classes, instantiate objects, and understand how these components form the backbone of object-oriented software. Let's get started!
Memory Allocation and Object Creation
In Java, objects are created using the new keyword, which allocates memory for the object on the heap. The syntax is:
type_name data_name = new constructor (parameters_constructor);
For example, to create an object of the Scanner class for reading user input:
Scanner reader = new Scanner(System.in);
Example of a Scanner Object java
Scanner reader = new Scanner(System.in);
System.out.println("Enter a number:");
int number = reader.nextInt();
System.out.println("You entered: " + number);
System.out.println("Enter a number:");
int number = reader.nextInt();
System.out.println("You entered: " + number);
In this example, the Scanner object reads an integer from the user input and displays it. This demonstrates both the state (user input) and the behavior (reading and displaying data).
Class methods Scanner
There are some methods in the Scanner class, for example:
Software objects have a state. The part of the memory occupied by the software object is used for variables that contain values.
Software objects have behavior. Part of the memory they occupy is spent on storing methods (programs) that allow the facility to "do something." The object does something when one of its methods is executed
- nextInt () - this method takes from the standard input the data typed on the keyboard, converts it into an integer.
- nextDouble () - this method takes from the standard input the data typed on the keyboard, converts it into a real number.
- identity (acts as a whole)
- state (has properties that can change)
- Behavior (can do some things and some actions can be done on it)
Software objects have a state. The part of the memory occupied by the software object is used for variables that contain values.
Software objects have behavior. Part of the memory they occupy is spent on storing methods (programs) that allow the facility to "do something." The object does something when one of its methods is executed
Assuming that for an object, we take the initial dialog window from the displayed application
From the displayed image of that object in the memory we conclude:
- The tiles in the image represent the bytes in the memory that the object occupies.
- This object has some atributes(variables defined in the class), the size of the Dialog(width * height), and the title, as well as some methods that control its behavior
- The software object consists of variables and methods
An object is data that takes its place in memory, and given that an object is a set of data, the object's memory must have a place for all the attributes, as well as the methods of that object. Figure 3 shows an example of attributes and methods for an object representing the main window of the Inventory application. A set of bricks represents the memory fields in which the mentioned object will be placed
Classes and objects on the example of points in the plane:
Task: Create 3 objects representing three points in level A, B, C. Move point A to the new A1 position.
§Point the coordinates of the points after the move. Data points are given in the picture.
§Point the coordinates of the points after the move. Data points are given in the picture.
We note that all three objects belong to the same class because they have the same properties x and y.There will be 3 different expansions (objects) of the same class
The status of point A will change during the program, but not the properties.
To create objects, we first need to create a class that will describe them. Class we will call Tacka. In it we will list the common attributes x and y that will have all 3 objects.
The status of point A will change during the program, but not the properties.
To create objects, we first need to create a class that will describe them. Class we will call Tacka. In it we will list the common attributes x and y that will have all 3 objects.
Class Point
Example: Geometric Shapes
Task: Create objects, for two squares and one circle. enter the sides of the square and the radius of the circle and calculate their areas.
Solution:
Given that two objects that represent squares have the same properties: side a and surface P, we can say that they belong to the same class, the class that describes any square. We can name this class eg Square or Square_Description, and that "description" will apply to any square.
On the other hand, an object that represents a circle does not have the same properties as a square, therefore, it belongs to another class that we can call, for example, Circle or Description_Circle. There is also that "main" class, from which the program is started and which has the main method in it. Let that class be called the same as the project: GeometricShapes. Within this class, objects are created and the flow of the entire program is controlled. Classes are created as follows. The Square class has square attributes: a(the length of the sides of the square), P(area) and an empty constructor.
In the next lesson, we will talk about methods, as constituent parts of classes and class constructors: Methods and objects
Given that two objects that represent squares have the same properties: side a and surface P, we can say that they belong to the same class, the class that describes any square. We can name this class eg Square or Square_Description, and that "description" will apply to any square.
On the other hand, an object that represents a circle does not have the same properties as a square, therefore, it belongs to another class that we can call, for example, Circle or Description_Circle. There is also that "main" class, from which the program is started and which has the main method in it. Let that class be called the same as the project: GeometricShapes. Within this class, objects are created and the flow of the entire program is controlled. Classes are created as follows. The Square class has square attributes: a(the length of the sides of the square), P(area) and an empty constructor.
In the next lesson, we will talk about methods, as constituent parts of classes and class constructors: Methods and objects
The Square class is shown below:
package geometricshapes;
import java.util.Math;
public class Square
{
double a; //A square page as a field or class attribute
double P; //The area of a square as a field or class attribute
//Empty class constructor
public Square() {
}
}
import java.util.Math;
public class Square
{
double a; //A square page as a field or class attribute
double P; //The area of a square as a field or class attribute
//Empty class constructor
public Square() {
}
The Circle class is shown below
package geometrijskioblici;
import java.util.Math;
public class Circle
{
double r; //Circle radius as a field or class attribute
double P; //The area of a circle as a field or class attribute
//Empty class constructor
public Circle() {
}
}
import java.util.Math;
public class Circle
{
double r; //Circle radius as a field or class attribute
double P; //The area of a circle as a field or class attribute
//Empty class constructor
public Circle() {
}
First, you need to create a new project by clicking on the "New Project" icon.
Then give the name "GeometricShapes", set the desired location and click the "Finish" button, see the image below:
A new project has been created, which contains the generated package "geometricshapes" and in it the file GeometricShapes.java, which contains a class with the same name, which is the main class, see the picture below:
In order to add the remaining two mentioned classes "Square" and "Circle", you should right-click on the package of geometric shapes, and then click on "New Class" in the context menu, which can be seen in the following image:
The main class is shown below:
package geometricshapes;
import java.util.Math;
import java.util.Scanner;
public class GeometricShapes
{
import java.util.Math;
import java.util.Scanner;
public class GeometricShapes
{
public static void main(String[] args)
{
}{
Scanner scanner=new Scanner(System.in);//An object is created to enter the data
Square square1=new Square (); //The object of the first square is created
Square square2=new Square (); //A second square object is created
Circle circle1=new Circle(); //An object of class circle is created
/*Data entry*/
System.out.println("Enter both squares and the radius of the circle in the order of the pages");
square1.a=scanner.nextDouble(); //Calls the method of the object that returns the entered data of type double
square2.a=scanner.nextDouble(); //Calls the method of the object that returns the entered data of type double podatak
circle1.r=scanner.nextDouble(); //Calls the method of the object that returns the entered data of type double podatak
/*Calculation of areas of square and circle objects*/
square1.P=square1.a*square1.a; //Calculation of the area of the 1st square
square2.P=square2.a*square2.a; //Calculation of the area of the 2nd square
circle1.P=circle1.r*circle1.r*Math.PI; //Calculating the area of a circle
/*Print the results*/
System.out.println("Area of the 1st square: "+square1.P);
System.out.println("Area of the 2nd square: " +square2.P);
System.out.println("Circle area: " + circle1.P);
}Square square1=new Square (); //The object of the first square is created
Square square2=new Square (); //A second square object is created
Circle circle1=new Circle(); //An object of class circle is created
/*Data entry*/
System.out.println("Enter both squares and the radius of the circle in the order of the pages");
square1.a=scanner.nextDouble(); //Calls the method of the object that returns the entered data of type double
square2.a=scanner.nextDouble(); //Calls the method of the object that returns the entered data of type double podatak
circle1.r=scanner.nextDouble(); //Calls the method of the object that returns the entered data of type double podatak
/*Calculation of areas of square and circle objects*/
square1.P=square1.a*square1.a; //Calculation of the area of the 1st square
square2.P=square2.a*square2.a; //Calculation of the area of the 2nd square
circle1.P=circle1.r*circle1.r*Math.PI; //Calculating the area of a circle
/*Print the results*/
System.out.println("Area of the 1st square: "+square1.P);
System.out.println("Area of the 2nd square: " +square2.P);
System.out.println("Circle area: " + circle1.P);
Here we see that two objects, square1 and square2, are created according to the same description (template), which is the class that describes them called Square. It should be noted that it is the same class for two different objects. The third object, circle1, is created by using the class "Circle" as its description. When creating objects, the class constructor is called, which has the same name as the class.
So, for creating objects of the Square class, the constructor is called that, while when creating an object of the Circle class, the constructor is also called "Circle". It is a method that serves to give objects some initial values to their attributes at the time of creation. There will be no more talk about it in the next lesson.
For loading, the Scanner class object is used, which must be imported:
So, for creating objects of the Square class, the constructor is called that, while when creating an object of the Circle class, the constructor is also called "Circle". It is a method that serves to give objects some initial values to their attributes at the time of creation. There will be no more talk about it in the next lesson.
For loading, the Scanner class object is used, which must be imported:
import java.util.Scanner;
Below, after loading, the surfaces of all 3 objects are calculated, and then the results are printed. Access to data representing the attributes of individual objects is done using the references of those objects. For example, in order to "get" the data of the area of the second square, it must be done as follows:
square2.P
After starting the application and entering the data, the surface values are printed, see the picture below:
|
Next
Objects and methods >| |