# The concept of Object in OOP
:::info
This is a part of preparation for Paper 2 in IB Computer Science diploma. The lessons are
1) [Start point with Java](/S3oXtcWLQduOy_R5_03Z2g)
2) [The concept of Object in OOP](/6FsqJbw3SKq5m1NKk56U3Q)
3) [The POJO (Plain Old Java Object) and their common methods](/zJgmWQUbSLqf3JIiVehBNA)
4) [Relationship between objects and UML diagrams](/5rUt1ACuTQGF7nuLhvx7TA)
:::
## Concept of object
An object is a an abstract entity that includes data AND instructions. In java the name of the data is members or attributes and the instructions are called methods.
(If someone wants to expand this or illustrate with more details, I'd be glad to add mor info here)
### Morse example
Printing java with public static methods
Details:
:::spoiler
```java=
public class Morse {
public static void morseDot() {
System.out.print(".");
}
public static void morseDash() {
System.out.print("_");
}
public static void morseJ() {
morseDot();
morseDash();
morseDash();
morseDash();
System.out.print("/");
}
public static void morseA() {
morseDot();
morseDash();
System.out.print("/");
}
public static void morseV() {
morseDot();
morseDot();
morseDot();
morseDash();
System.out.print("/");
}
}
```
In the driver class we write this:
```java=
import java.util.*;
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
Morse.morseJ();
Morse.morseA();
Morse.morseV();
Morse.morseA();
}
}
```
:::
The output is
```
Hello, World!
.___/._/..._/._/
```
:::warning
* The name of the class Morse is in capital and you need to rename the file to Morse.java (with the capital letter)
* You need to remember to use the one compiler with java (not javascript)
:::
Now, let's discuss what happens if we don't want other parts of the code to do dots or dashes without doing a full letter. We have to go to the line of the methods of morseDot and morseDash and declare their access private (before they were public)
```java=
private static void morseDot() {
System.out.print(".");
}
private static void morseDash() {
System.out.print("_");
}
```
Now from the driver class we cannot write `Morse.morseDot();` because it's going to give us a compile error

### Morse 2. Difference between outputting and returning
In the first case we have done public static void methods that they output the dots and the dashes. Now they're going to return that String.
Here is the Work In Progress code (to be finished by the students)
Main class
```java=
import java.util.*;
public class Main {
public static void main(String[] args) {
System.out.println(Morse.morseJ());
}
}
```
Morse.java
```java=
public class Morse {
private static String morseDot() {
return ".";
}
private static String morseDash() {
return "_";
}
public static String morseJ() {
String answer = morseDot() + morseDash() + morseDash() + morseDash();
return answer+"/";
}
}
```
### Access modifiers, public, private, protected and default
For now we're doing classes that only have instructions this is because I think is easier to understand the modifiers with methods and then, apply this modifiers to attributes.
Continuing with the morse example, let's say that we want to have total control of when the "morseDot" and "morseDash" is called.

_[source](https://codepumpkin.com/access-modifiers-in-java/)_
### Static variable example
Modification //TO-DO
```java=
public class Morse {
public static String spaceBetween = " ";
public static void morseDot() {
System.out.print(".");
System.out.print(spaceBetween);
}
public static void morseDash() {
System.out.print("_");
System.out.print(spaceBetween);
}
public static void morseJ() {
morseDot();
morseDash();
morseDash();
morseDash();
System.out.print("/");
}
public static void morseA() {
morseDot();
morseDash();
System.out.print("/");
}
public static void morseV() {
morseDot();
morseDot();
morseDot();
morseDash();
System.out.print("/");
}
}
```
### Instantiation
Plato's cave

_[source](https://montessorium.com/blog/plato-s-allegory-of-the-cave-part-i)_

_[source](https://medium.com/@qdjhzqy/platos-theory-of-perfect-forms-655cc2414990)_
//TO-DO+
Main class
```java=
import java.util.*;
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
Morse morseFaaaast = new Morse();
morseFaaaast.spaceBetween = " ";
morseFaaaast.morseJ();
morseFaaaast.morseA();
morseFaaaast.morseV();
morseFaaaast.morseA();
System.out.println("Slow one!");
Morse morseSloooooooooow = new Morse();
morseSloooooooooow.morseJ();
morseSloooooooooow.morseA();
morseSloooooooooow.morseV();
morseSloooooooooow.morseA();
morseFaaaast.morseJ();
}
}
```
Morse.java
```java=
public class Morse {
public String spaceBetween = " ";
public void morseDot() {
System.out.print(".");
System.out.print(this.spaceBetween);
}
public void morseDash() {
System.out.print("_");
System.out.print(this.spaceBetween);
}
public void morseJ() {
morseDot();
morseDash();
morseDash();
morseDash();
System.out.print("/");
}
public void morseA() {
morseDot();
morseDash();
System.out.print("/");
}
public void morseV() {
morseDot();
morseDot();
morseDot();
morseDash();
System.out.print("/");
}
}
```
### Instantiation now with students
First version of the student:
```java=
public class Student {
public String sayHi(){
return "Hi";
}
}
```
To make the student say hi, we're using a concrete (dynamic) method. So we need to instantiate it.
We need to write this in the driver class
```java=
import java.util.*;
public class Main {
public static void main(String[] args) {
Student student = new Student();
System.out.println(student.sayHi());
}
}
```
The output will be Hi
### Instantation analysis
When we have `Student student = new Student();`
The first Student refers to the type of the variable (in this case the class "Student") that's why goes in Capital because it's an object.
The second student refers to the name the variable. This can be changed to other names like pepito o juanita. This is usually written in lowercase to differenciate it.
The new is a keyword to create a new instance.
The third Student() refers to the constructor method (more on that later)