[Java] A class is an OS, and an instance is a virtual computer.

0. The "new concept" of object orientation

It's harder than you think to teach Java to anyone who is new to programming and has only experience coding in C. (Assuming an information university. It seems that you often do C in the first year and Java in the second year.) </ Sub> </ sub> When asked "What is a class?", I explain that it is "object-oriented." What if you were asked, "What is object-oriented?"

"There are various monogoto in the world, and monogoto has a state and behavior. Therefore, for various monogoto, we collect states and behaviors and code them in the class. This is object-oriented. " The other party may be convinced. However, the other party may be confused by the explanation of "encapsulation" and "inheritance" that follows. If you suddenly learn that you "collect states and behaviors to express monogoto", then you learn "to confine the state to behavior" and "concrete monogoto is an abstract monogoto". There are many. It would be "pain" for those who are "practice rather than theory". In this way, it seems that there is no end to the number of people who are not good at object-oriented coding and move away from the programming area.

The true nature of "pain" is that "what is object-oriented, that is, what" is not explained in one word.

It is speculation, but people think of new concepts by comparing them to existing ones, and learn the error part by practice. In Laputa, the castle in the sky, the main characters Pazu and Theta follow the government aircraft Goliath to discover the legendary island Laputa floating in the sky, and it seems that Goliath will be lost once. At that time, collaborator Dora attempts to continue the chase using a kite called a "watch stand". At this time, Pazu is instructed as follows.

You can learn how to fly (a kite) with your body!

Although it is a scene where the courage of Pazu who manages his life-threatening unreasonableness without difficulty stands out, Dora did not instruct Pazu without thinking. Before Goliath's tail, Dora set out a small boat to Theta's whereabouts with Pazu in order to rescue Theta, who was half-threatening and taken to the government. At this time, Dora fainted due to the impact of the accident, and the small ship was about to crash, but Pazu suddenly took over the maneuver, and he died in nine deaths. The instructions for kite maneuvering (new concept) may have been in anticipation of Pazu's track record of properly maneuvering small boats (existing concept) in extremely tense situations.

Isn't object-oriented the same thing? You can compare object-orientation with something that everyone knows, and the difference can be learned by each person based on practice. At least it's better than not being able to even move your hands without getting an image. In this article, I will explain Java programming and object-oriented programming with this approach.

Finally, if you go back to the definition of "what is a program" and "what is a computer (or a computer)", ** It is logically provable that the class is a "program that can configure a computer" and that the instance is a computer **.

1. Java is "a language that creates'virtual computers'"

** To explain a class in one word, it is a "program that can configure a computer". ** ** This can be proved logically.

Similarly, it can be proved that * an instance is a "virtual computer" *.

1-1. Proof that the class is a "program that can configure a computer"

First, the three elements of the program in Böhm Jakopni's theorem [1] You can do "sequential", "iteration", and "branch" all with the methods in the class. Of course, ** methods can also be called programs **, Unfortunately the method has no "storage". (Jabashi: A "local variable" that is valid only within a method cannot be said to be "remembered" because it expires at the end of the method) </ sub> Therefore, it is not possible to create a "header" in the Turing machine [2]. ** Methods can be programs, but not computers **. In that respect, a class has a "field" (called a "property" in a language other than java), and this can be used as a storage area. ** Classes can build computers **. (End of proof) In other words, a class is a blueprint for a computer.

1-2. Proof that the instance is a "virtual computer"

The class itself is a type for creating an "entity" called an instance. Because the instance is created based on the blueprint called class An instance is "something created based on a blueprint called a class." Applying what we proved in Section 1-1 here, An instance is "Something made based on a blueprint called'computer blueprint'" Is. It's a computer (if made according to the blueprint). (End of proof)

2. Create an OS and a virtual computer and play with it

From Chapter 1, a class is a "program that can configure a computer". It turns out that an instance is a "computer". The "program that can configure a computer" is exactly the OS. It may be strange, but every time you program in java, the programmer creates an OS, and every time you new, you create a virtual computer.

For example, consider the following code.

"Windows" OS and "computer" computer


public class Mindows
{
    private byte[] ram;
    public byte getRam(int address){return this.ram[address];}
    public void setRam(int address, byte value){this.ram[address]=value;}
    public Mindows(int ram_size){this.ram = new byte[ram_size];}
}

class Main{ public static void main(String...args)
{
    Mindows computer = new Mindows(64);//Creating a computer with 64 bytes of RAM
    int i=0;
    for(char each_char : "hello world".toCharArray())
        computer.setRam(i++, (byte)each_char);
        //0~Write "hello world" at address 10

    for(int j=0; j<11; j++)
        System.out.print((char)computer.getRam(j));
        //0~Address 10(As char)display
        //→ "hello world" is displayed on the console.
}}

This code defines the Mindows OS, creates a virtual computer computer containing this OS, and You are manipulating RAM. It is the OS (class) that defines what can be done using computer (instance), but the OS does not do anything directly, and the actual operating entity is computer (instance). is there.

Besides this, Make a ROM with the final constant field, Create a BIOS with the final method It is conceivable to use the file input / output function to have storage.

3. Think by applying to the "actual" of java

3-1. ArrayList class

A ʻArrayList type instance is a virtual computer that can store T` type objects in order in the storage area.

3-2. List interface

The List <T> interface is just a "declaration" of a program that stores T type objects in storage. ** It can be said that it "roughly" determines how to use the storage area **. ʻArrayList andLinkedList "implement" this interface. This fact means that ʻArrayList <T> and LinkedList <T> declare that "the storage area will be used to store T type objects ". The word ... </ sub> </ sub> class that implements the interface must overload the empty methods defined in the interface and define them specifically. ... execute </ sub> </ sub>

The declaration by the interface is easy to understand if you imagine home appliances. Rice cookers and washing machines (and vacuum cleaners in our day) can all be called computers. The rice cooker calculates the hardness and remaining time of cooked rice from the weight of rice and water. There are also scenes where the washing machine makes an automatic decision. You can see the vacuum cleaner by looking at Roomba. It is important to clearly state whether the class (program) you are about to make is a rice cooker, a washing machine, or a vacuum cleaner. In some cases, it is possible to say, "Only the class that implements a specific interface is allowed to use it specially." (For example, an exception class.)

3-3. Encapsulation

"I don't want to show the circuit inside the computer to the customer" -that's encapsulation. Let's take a look at the code shown in Chapter 2 again. When writing to the RAM of computer, I purposely call the methodsetRam (address, value to be written). Even if I don't do that, is ram [address] = value to be written useless? Of course, that's fine in principle, but it's actually harder to do. The developers of "Mindows" have created setRam (address, value to write) as a function to write to memory, but they have not exposed the memory itself. The actual computer puts the access modifier private in the java code so that the memory is locked inside the iron. By doing so, ** the storage area is not tampered with by the user **, and the possibility of malfunction is reduced.

4. Object-oriented programming is possible if the OS and computer can be freely created.

Unfortunately, you can't always think of an OS as a class. This is because there are (or most) OSs that have concepts that classes do not have. (Example: multitasking OS, virtual memory, etc. Also, if you have to think about clocks and CPUs, it will be difficult to express this in classes (practically)) Therefore, let's call the OS that can represent a class "class OS" and the computer that can represent an instance "instance computer" or "computer of class OS".

On the contrary, there is no OS that does not have any of the concepts of the class (only fields and methods). (Clear from the proof that the class can be considered an OS)

** In an environment where you can freely design an OS and create and use a virtual computer that contains that OS, object-oriented development is possible at least partially **. (Let's call it the "semi-object-oriented theorem") This is not surprising as every OS has all the concepts of classes. When an error occurs, it is easy to identify the error because it is sufficient to confirm which computer (OS) has the problem, and it is possible to flexibly respond to specification changes. However, encapsulation cannot be used without an access restriction mechanism, and inheritance is impossible without a copy mechanism.

<!-Implementation of private fields using md5->

n. Reference

[1]http://s150001.skr.u-ryukyu.ac.jp/lectures/index.php?ProgrammingAbility [2]http://e-words.jp/w/%E3%83%81%E3%83%A5%E3%83%BC%E3%83%AA%E3%83%B3%E3%82%B0%E3%83%9E%E3%82%B7%E3%83%B3.html

Recommended Posts

[Java] A class is an OS, and an instance is a virtual computer.
What is a class in Java language (3 /?)
[Java] Differences between instance variables and class variables
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
How to check if an instance variable is defined in a Ruby class
What is the difference between a class and a struct? ?? ??
Write a class in Kotlin and call it in Java
[Java Silver] What are class variables instance variables and local variables?
[Ruby] Does self refer to a class or an instance?
What is a wrapper class?
About an instance of java
What is the LocalDateTime class? [Java beginner] -Date and time class-
Java class definition and instantiation
What is a Java collection?
[Ruby] What is an instance?
Difference between class and instance
What is the difference between an action and an instance method?
[Java] What is class inheritance?
[Java basics] What is Class?
[Java] Introductory structure Class definition Relationship between class and instance Method definition format
What is the difference between a web server and an application server?
What is JVM (Java Virtual Machine)?
Create an immutable class with JAVA
Difference between instance method and class method
How slow is a Java Scanner?
Difference between instance variable and class variable
[Java] Declare and initialize an array
StringBuffer and StringBuilder Class in Java
Java Calendar is not a singleton.
What is a lambda expression (Java)
[Swift] What is "inheriting a class"?
[Java] Conditional branching is an if statement, but there is also a conditional operator.
Activity transition with JAVA class refactoring and instance experimented on Android side
I made a class that can use JUMAN and KNP from Java
How to make an app with a plugin mechanism [C # and Java]
Ubuntu 18 is the OS that adds a NIC to a server instance in Sakura's cloud and assigns a local IP.
[Java] Create and apply a slide master
A look at Jenkins, OpenJDK 8 and Java 11
[Java] Inheritance and structure of HttpServlet class
What is Java and Development Environment (MAC)
Why does Java call a file a class?
A Java engineer compared Swift, Kotlin, and Java.
Java programming (static clauses and "class variables")
Creating a matrix class in Java Part 1
[Java] Instance method, instance field, class method, class field, constructor summary
[Java] Difference between equals and == in a character string that is a reference type
Cause of is not visible when calling a method of another class in java
When using a list in Java, java.awt.List comes out and an error occurs