"Hi, Amigo. Today I'll tell you about a typical Java program. The big news is that every program written in Java consists of classes and objects."
"I already know what classes are. What are objects?"
"Let's start with an analogy. Suppose you want to build a small ship. You work on a design and then send the blueprint to a factory, where a ship will be assembled according to your design. Or a dozen ships, or as many ships as you want. My point is that dozens of identical ships can be made based on one blueprint."
"That's exactly how it works with Java."
"Java programmers are like design engineers, except instead of creating blueprints, they write classes. Ship parts are made based on blueprints, while objects are created based on classes."
"First, we write classes (make blueprints). Then, when the program is run, the Java machine creates objects based on these classes. It's exactly like how ships are built from a blueprint. One blueprint – many ships. The ships are different. They have different names and carry different cargo. But they are still similar. They all have an identical design, and are able to perform similar tasks."
"OK, I get your ship analogy. Could you give me a couple more to help me be sure I understand what you're saying?"
"Take, for example, bees..."
"No, scratch that. I've had a bad experience with bees. Let's take ants."
"An ant colony is a good example of how objects interact. Any ant colony consists of three classes: the queen, soldiers, and worker ants. The number of ants in each class varies. Usually a colony only has one queen, dozens of soldiers, and hundreds of workers. Three classes, hundreds of objects. The ants follow strict rules as they interact with ants in their own class and ants that belong to other classes."
"This is the perfect example. A typical program works exactly like that. There is a main object that creates objects in all the classes. The objects interact with each other and with the external world. The objects' behavior is hardwired (programmed) internally."
"I don't quite get it. I mean, I don't get it at all."
"These two explanations are two sides of the same coin. The truth is somewhere in between. The first example (about blueprints and ships) shows us the connection between a class and its objects. It's a powerful analogy. The ant colony analogy demonstrates the relationship between objects, which are described by classes and exist only while a program is running."
"You mean we need to write classes for all objects used in a program, and then describe their interactions?"
"Yes, but it's easier than it sounds. In Java, while a program is running, all entities are objects. Writing a program amounts to describing the various ways that objects can interact. The objects simply call each other's methods and pass the required data to them."
"It's a little fuzzy, but I think I almost get it."
"How do we know which methods to call and which data to pass?"
"Each class has a declaration, which indicates its intended use. Similarly, every method has a declaration that indicates what it can do and what data we need to pass to it. To use a class, you need to have a general understanding of what it does. You need to know exactly what each method does, but not exactly how it does it. It's like a magic wand."
"Huh! Sounds nice."
"Here. Have a look at the code of a class that copies files:"
package com.codegym.lesson2;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy
{
public static void main(String[] args) throws IOException
{
FileInputStream fileInputStream = new FileInputStream("c:\data.txt");
FileOutputStream fileOutputStream = new FileOutputStream("c:\result.txt");
while (fileInputStream.available() > 0)
{
int data = fileInputStream.read();
fileOutputStream.write(data);
}
fileInputStream.close();
fileOutputStream.close();
}
}
"I can't say I get it all, but I think I got the essence of it."
"Great. See you next time then."
"I almost forgot. Here's your task from Diego."
GO TO FULL VERSION