Object Oriented Programming-Classes and Objects
Previous
|< Arrays in Java |
Next
Objects and methods >| |
One of the ideas behind the object-oriented software is that the software is organized in a way that suits the way of thinking an object-oriented brain. Instead of the machine instructions that change the bits in the main memory, we want "things" that something "works". Of course, nothing has changed at the machine level, bit patterns continue to change via machine instructions. The change is that we do not have to think that way.
Question: List several objects.
Pencil, keyboard, shoes, table
Question: What is the characteristic of an object?
Question: List several objects.
Pencil, keyboard, shoes, table
Question: What is the characteristic of an object?
- The building is made of tangible material
- The building behaves as a whole, composed of parts
- The building has features
- The facility can do some things and some actions can take place on it.
- has an identity (it behaves as a whole)
- has a state (has properties that can be changed)
- has behavior (can do some things and some actions can be done on it)
VIDEO LESSON
Video 1: Classes and objects -part 1
In this presentation, we will show how to create classes and objects in the JAVA programming language.
The idea of object-oriented programming is to present data in software in a similar way as in nature.
The idea of object-oriented programming is to present data in software in a similar way as in nature.
Complex data-objects
To reserve the memory of the object we use the following form:
type_name data_name = new constructor (parameters_constructor);
For the data type, the name of the Class to which the object belongs is used. For example. if the object belongs to the Scanner class then the definition of the object that we call the reader would be:
Scanner reader=new Scanner(System.in);
Defining a complex data.
Software objects imitate objects from nature, so that both have:
- Properties
- Behavior
- State
Class-as a description of the object
The object class is actually a description of all objects that belong to it, and these are objects that have the same:
- Properties
- Behavior-described by methods
By defining an object we created it by description (Class) and it will have the properties and methods listed there:
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
The following figure shows the image of that object in the memory
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 variables, the width of the Dialog, the height of the Dialog, and the textUsual, as well as some methods that control its behavior
- The software object consists of variables and methods
Object class in java
- Class is a set of objects that have a common structure and behavior.
- One object is a copy of a class
- A set of common features of an object and methods that represent the behavior of these objects are described in their class
Classes and objects on the example of points in the plane:
text of the 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
Objects and Methods
A method is a set of commands, a part of a program that represents a separate entity by itself and performs a certain task, usually elementary.
We distinguish:
We distinguish:
- Defining the method
- Using the method
Using the Method.
We can use:
- The method that is already defined in an existing class
- The method that we create (define) ourselves
Using the existing Method
For instance:
System.out.println("A("+A.x+","+A.y+")");
System.out.println("A("+A.x+","+A.y+")");
Here, the println method is called for the out object, which represents the space where the text will be printed. It's standard output.
The method name is always followed by parentheses"( )"
They are used to send one or more data, method parameters, to the method as needed.
In this example, the method is passed one parameter representing the text to be printed.
This text is obtained by concatenating several smaller texts within which variable values are inserted.
The method name is always followed by parentheses"( )"
They are used to send one or more data, method parameters, to the method as needed.
In this example, the method is passed one parameter representing the text to be printed.
This text is obtained by concatenating several smaller texts within which variable values are inserted.
Defining the Method.
A method consists of a title line and a method body that contains the program commands that are executed in that method:
Example defining a Method.
- In the previous example, let's create a method that calculates the distance between points
- The formula for calculating the distance between points 1 and 2, whose coordinates are x1,y1 and x2,y2
- d=Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y1-y2,2))
Two methods from the Math class are used here:
sqrt - which calculates the square root where the square root is passed as a parameter
pow- method that calculates the power, whose base and exponent are sent as parameters
sqrt - which calculates the square root where the square root is passed as a parameter
pow- method that calculates the power, whose base and exponent are sent as parameters
- Commands that make up a method are written in the body of the method, between brackets"{ }"
- there is also a method header
Example: Defining a Method for calculating the distance between two points.
We can see that the method has been added to the main class because it does not refer to any single point but to a set of points
This method calculates the distance and then sends the calculated value using the return command.
This method calculates the distance and then sends the calculated value using the return command.
Method header.
public - privacy modifier. When it is public then the method can be called from another class.
double in second place - This is the return value type. The value returned from the method is double distance is the name of the method.
double in second place - This is the return value type. The value returned from the method is double distance is the name of the method.
Methods that do not return a value.
The word void is the second word in the header. This is the return value type. If void is written in this place, then the method does not return anything and does not have a return command.
A method that prints data
This method takes no parameters and returns nothing from the method
When it is called, it will print the coordinate values and finish the work, and continue the program at the place where it was called
When it is called, it will print the coordinate values and finish the work, and continue the program at the place where it was called
Method calls in Java
Here we see that the distance method is underlined in red (error). This is because the method needs to belong to an object and be called via an object of the main class, and that object has not been created
There is only a class and the method must then be bound to the class itself. This is achieved by adding the word static
There is only a class and the method must then be bound to the class itself. This is achieved by adding the word static
The parameters sent in the call are in turn copied to the parameters in the method definition and thus passed to the method.
Class constructors
- These are special methods that are called when the object is created. They do not have a return value and the word void is not written in the second place in the header. They serve to give initial values to the properties of the object.
- They can be without or with parameters
- They are named the same as the class they are in
Let's add a constructor in the Point class
Constructors with parameters
They are used when we know the data about the object in advance. Since here we know the coordinates of the points in advance, we will replace the empty calls with the constructor calls with parameters.
Previous
|< Arrays in Java |
Next
Objects and methods >| |