[Java] Object-oriented summary_Part 1

Purpose of this article

--Writing person: Understand the basic grammar of programming languages, do not understand object orientation, during engineer training ――Before learning the object-oriented way of thinking that is necessary for learning Java, ** the aim is to get angry with the object-oriented world view and promote understanding. ** Also, my knowledge will be firmly established. Also.

background

--Object-oriented peripheral knowledge is required to understand Java session scope. ――Because I had avoided it until now, I thought about deepening my understanding of object-oriented programming.

What was helpful

Thorough commentary for beginners! What is object orientation? ・ It helped me to understand the technical terms around object orientation.

Object-oriented Iroha -Qiita ・ The explanation of abstraction was very helpful in thinking about programming and object-oriented background.

[Java] What is a constructor? Meaning of this \ () \ | The easiest introduction to Java ・ After understanding the rough knowledge (basic Java) and abstraction so far, I read this explanation and it became very familiar. Thanks to this explanation, various things have been connected.

[Object Oriented -Wikipedia](https://ja.wikipedia.org/wiki/%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF % E3% 83% 88% E6% 8C% 87% E5% 90% 91) -The six object-oriented summaries were helpful as rules for not derailing from object-oriented.

Introduction to Object-Oriented ・ Later, it seemed to be good for checking the degree of understanding, so a memorandum.

Clear Java Primer -It helped me understand the difference between an object and an instance.

"Abstract" before object-oriented

But it is suddenly,

What do you think of when you think of an "apple"?

...

Speaking of "apples", "red, round and edible objects"

I think of you.

** I think I was able to understand it without specific information **.

This is called ** abstraction **.

As you can see from the example, ** abstractions are groups of similar features and given appropriate names for them. **

And I think the same thing can be said about programming.

In programming, there are many groups with similar characteristics.

The features are named variables, functions, and object-oriented.

If this is an apple, it can be easily explained as "a red, round and edible object", but in the case of programming, it takes time to understand because the abstracted object is very complicated and there are various patterns.

And object-orientation is one of those complex things.

However, I don't need to know various patterns now, so I would like to give a concrete example with one royal road pattern and try to abstract it to deepen my understanding of terms **.

What is an "object" before it is object-oriented?

~~ An object is an "object". ~~

~~ Object oriented ??? ~~

An object is generally an "object" In the world of programming, it is better to think of it as an "informed gathering" or an "abstracted object."

Why object-oriented?

In the real world, most are made up of "objects".

An "object" is assumed to exist in space with a concrete shape.

However, it is difficult to understand if everything is an "object", so I decided to classify each feature and give a name to the "object".

** Yes, it's an abstraction. **

In this way, it is more irritating to adopt ** object-oriented ** by incorporating the idea ** "it is easy to handle if you think of it as an object like the real world", so I like that. understood.

And it is object-oriented programming that applied this idea to programming.

I think this is also from the idea ** "because it is easier to understand the programming world in the same way as the real world".

Object-oriented to remember in the real world

Since an object means an object in the real world, I thought it would be easier to understand by taking the real world as an example, so I will explain it with a ** minimum configuration using a car **.

What I mainly do in object orientation

** I made a car design document. Make a car based on the car design document. **

Simply put, that's it.

Furthermore, ** Make a "design document" that describes the specifications (color, function, weight, price) of the car, Manufactured based on "design document". **

That's all.

And ** when you have a car, drive it. **

The main thing I do is ** "Create a design document to create an object, create an object, and move the object." **

(I was most hungry for this way of understanding.)

Think a little more like programming

This time, I would like to take the process of actually making a red car as an example.

In the world of programming, the code is loaded in order from the top.

For example, when the following sentence (the sentence is written instead of the code for easy understanding), it is read in order from the top.

** 1. Name the design document "Car". 2. Write information about the car "color is red" and "name is A" in the contents of the design document. 3. In the contents of the design document, write the actions related to the car such as "can make a call" and "can stop". 4. Make a "car" based on the "car design document". **

Then, in terms of programming, a "red car called A" will be completed.

In this way, ** Object-oriented programming is the idea of creating a "design document", writing "what kind of thing" and "what can be done" in it, and treating it in the same way as a real thing. **

However, there may be times when you want the color of the car to be "blue".

In that case, rewrite part 2 from "red" to "blue".

Then, "blue car called A" is completed.

This is how easy it is to make changes in programming.

In the real world, if you try to do this, you have to take the process of collecting the car, painting it in blue from above, and returning it (and it is very difficult if there are many cars, it is a so-called recall. …).

However, in the world of programming, all you have to do is rewrite the contents of the "car design document".

Somehow, I think I've learned how to write object-oriented.

Understand object-oriented jargon (Java)

It's a rough idea, but if you can understand the meaning so far

It's almost as if I knew about it.

** These terms are just names for each and every event in the previous example. ** (Actually it's more complicated, but ... it's easy for me to understand.)

-** Object-oriented → The idea of treating objects in the same way as real-world objects ** -** Object → Generic name for objects ** (car) -** Class → Name of design document ** (Car design document) -** Field → Object information ** (color is red, name is A) -** Method → Object movement ** (Can make or stop) -** Instance → Object made based on the design document ** (Car made based on the car design document) -** Instantiation → Creating an object based on the design document ** (Making a car based on the car design document)

It's difficult to put it in terms, but what I'm doing is ** "Create a design document to create an object, create an object, and move the object." **.

Try to figure out the relationship between terms

A simple diagram of this relationship is as follows. オブジェクト指向_用語関連図.png

Difference between object and instance

An object is a general term for dynamically generated entities. Instance is a general term for dynamically generated entities of class type.

It looks like this in the figure. オブジェクト指向_オブジェクトとインスタンス.png

When used in the context of just an entity, both objects and instances seem to have the same meaning.

Actually write in Java code

1. Name the design document "car".

Car.java


class Car {

}

[Program description] -The type is a class type and the class name "Car" is declared. -Class types are classified as reference types.

2. Write information about the car "color is red" and "name is A" in the contents of the design document.

Car.java


class Car {
	String carname = "A"; //Car name
	String color = "red"; //Car color
}

[Program description] -String type car name that represents the name of the car -String type color that represents the color of the car

3. In the contents of the design document, write the actions related to the car such as "can make a call" and "can stop".

Car.java


class Car {
	String carname = "A"; //Car name
	String color = "red"; //Car color

	void start() { //Car outgoing operation
		System.out.println("Take off");
	}
 
	void stop() { //Car stop operation
		System.out.println("Stop");
	}
}

[Program description] -Represents the outgoing operation of the car. Return type start method that does not return a value ・ Indicates the stop operation of the car. Return type stop method that does not return a value

4. Make a "car" based on the "car design document".

CarTest.java


class CarTest{
	public static void main(String[] args){
		Car car = new Car();
	}
}

[Program description] -The type is a class type and the class name "CarTest" is declared. -Instantiate the Car class with the new operator and assign an object to the Car type variable car (class type variable). → ** It looks like you are substituting the created object, but in fact, the variable Car just refers to (sees) the object called Car (the access method is part 2) **.

It looks like this in the figure. クラス型変数.png

Although it is a rough sketch, I made a design document for making an object and summarized it while referring to various things up to the point of making an object.

Continue to [Java] Object-Oriented Summary \ _Part 2 -Qiita.

Recommended Posts

[Java] Object-oriented summary_Part 1
[Java] Object-oriented summary_Part 2
[Java] Object-oriented
Object-oriented FizzBuzz (Java)
[Java] Object-oriented syntax-Constructor
Object-oriented (Java) basics
[Java] Object-oriented syntax-Package
Object-oriented (java) with Strike Gundam
Object-oriented summary by beginners (Java)
Java
Java 3 major elements (object-oriented) memorandum
Java
[Java] Object-oriented syntax --class method / argument
[Java] Object-oriented syntax-class / field / method / scope
Summary of object-oriented programming using Java
Java learning (0)
[Java] array
Java protected
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
Java array
[Java] ArrayDeque
java (method)
Object-oriented summary
Java Day 2018
Java string
java (array)
Object-oriented programming
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload