Java - University of Technology - 3

For example, Shape, we can use inheritance to build classes that model more specific concepts, such as Circle, Triangle. This way, we can use the interface and implementation of the old class for the new class.

Polymorphism, in the general sense, is the ability to exist in more than one form. In object orientation, polymorphism goes hand in hand with inheritance, and it means that the same name can be interpreted in different ways depending on the situation. Objects of different derived classes can be treated the same, as if they were objects of the base class, for example, Triangle and Circle objects can be placed in the same data structure for Shape, or the same rotate function call can be made for Triangle and Circle objects. And when the same message is received, objects of different classes interpret it in different ways. For example, when the message "rotate" is received, Triangle and Amoeba objects implement different rotate() methods.

Exercise

Maybe you are interested!

1. Fill in the blanks in each of the following sentences with the appropriate word:

a) The relationship between a house and a blueprint is similar to the relationship between awith one layer.

Java - University of Technology - 3

b) When each object of a class holds its own copy of an attribute, the data field representing that attribute is called

2. Focusing on important properties while ignoring less important details is called

A. Abstraction

B. Polymorphism

C. Packaging

D. Concealment of information

3. "The same message is understood in different ways depending on which class the recipient of the message belongs to" is a characteristic of which concept?

A. Packaging

B. Polymorphism

C. Inheritance

D. Reuse

4. "Objects of a child class can be treated like objects of a parent class" is a characteristic of which concept?

A. Abstraction

B. Polymorphism

C. Packaging

D. Concealment of information

E. Inheritance

5. "Hiding the implementation details and showing only the module interface" is a characteristic of which concept?

A. Abstraction

B. Polymorphism

C. Packaging

D. Reuse

Chapter 2. Java Programming Language


Java was designed by Sun Microsystems in 1991 as a language for embedded programs that run on consumer electronics such as microwave ovens and home security systems. However, the growth and spread of the Internet and the World Wide Web (WWW) led Sun to shift Java from an embedded programming language to a Web application programming language. Today, Java has become a popular application programming language and one of the most important languages ​​for developing Web and Internet applications.


2.1. CHARACTERISTICS OF JAVA


Java is an object-oriented language. Object-oriented languages ​​divide a program into discrete modules, called objects, which encapsulate the program's data and operations. The concepts of object-oriented programming and object-oriented design refer to a style of program organization that is increasingly being chosen for building complex software systems. Unlike the C++ language, where object-oriented features were added to the C language, Java was designed from the outset as an object-oriented language.

Java is a robust language. Unlike many other programming languages, errors in Java programs do not cause system crashes. Some features of the language also allow many potential errors to be detected before the program runs.

Java is platform independent. A platform here means a computer system with a specific operating system, such as a Windows or Macintosh system. Java's trademark is "Write once, run anywhere." This means that a Java program can run on different platforms without having to be recompiled. Some other high-level languages ​​do not have this feature. Portability, or the ability to run on virtually any platform, is also why Java is so well suited for Web applications.

Java is a distributed language. Programs can be designed to run on a network of computers, a program consisting of classes scattered across different machines in the network. In addition to the language, Java also has a rich collection of code libraries that have been designed to be used directly for specific types of applications, creating


2 Sun Microsystems has been part of Oracle since 2010.

favorable conditions for building software systems for the Internet and WWW.

Java is a secure language. Designed for use on computer networks, Java has features that protect against untrusted code – code that could introduce viruses or otherwise disrupt the system. For example, when a Java Web program is downloaded to a computer browser, it is prohibited from reading or writing information to the computer.


2.1.1. Java Virtual Machine

Machine language consists of very simple instructions that the computer's CPU can execute directly. However, most programs are written in high-level programming languages ​​such as Java or C++. A program written in a high-level language needs to be translated into machine language before it can be run on a computer. This translation is done by a compiler. To run on computers with different machine languages, compilers suitable for that type of machine language are needed.

There is another option instead of compiling a program written in a high-level language. Instead of using a compiler to translate the entire program, you can use an interpreter, which translates each instruction one by one and only translates when needed. An interpreter is a program that works much like a CPU with a sort of fetch-and-execute cycle. To execute a program, the interpreter repeats the sequence of tasks: reading an instruction from the program, determining what needs to be done to carry out that instruction, and then executing the appropriate machine code instructions to carry out that instruction.

One use of interpreters is to execute programs written in high-level languages, such as Lisp. The second use is that they allow you to run a machine language program written for one type of computer on a completely different type of computer. For example, there is a program called "Virtual PC" that runs on Mac OS computers, which is an interpreter for machine code programs written for IBM PC compatible computers. If you run "Virtual PC" on a Mac OS computer, you can run any PC program, including programs written for Windows.

The designers of Java chose a combination of a compiler and an interpreter. Java programs are compiled into machine code, but this is machine code for a non-existent computer – this “virtual” machine is called the Java Virtual Machine (JVM). The machine language for the Java virtual machine is called Java bytecode, or bytecode for short. To run Java programs on any type of computer, one only needs an interpreter for Java bytecode, which emulates the Java virtual machine in the same way that Virtual PC emulates a PC. The Java virtual machine is also the name given to the interpreter.

bytecode interpreter performs the emulation task, so we say that a computer needs a Java virtual machine to run Java programs.



Figure 2.1: Compiling and interpreting for Java programs


Of course, each type of computer needs a different Java bytecode interpreter, but once it has one, it can run any Java bytecode program. And that same Java bytecode program can run on any computer that has a Java bytecode interpreter. This is one of the important features of Java: a compiled program can run on many different types of computers.

There are several reasons why it is preferable to use Java bytecode as an intermediate code rather than distributing the Java source code and having people compile it into the machine code of their computers. First, a compiler is a complex program, whereas an interpreter is a small, simple program. It is easier to write an interpreter for a new type of computer than to write a compiler. Second, many Java programs need to be downloaded from a network. This raises obvious security concerns: we do not want to download and run a program that will damage our computer or the files on our computer. The bytecode interpreter acts as a buffer between our computer and the program we downloaded. It can protect us from potentially malicious actions by that program.

When Java was a new language, it was criticized for being slow. Since Java bytecode is executed by an interpreter, it seemed that bytecode programs could never run as fast as programs that had been compiled into the machine language of the computer on which the program was running. However, this problem has been largely solved by using a just-in-time compiler (JIT) for executing Java bytecode. A JIT compiler translates Java bytecode into machine code. It does this while the program is running. Like a regular interpreter, the input to a JIT compiler is a Java bytecode program, and its job is to execute that program. But while the program is running, it translates parts of the program into machine code. These compiled parts can then be executed faster than

since a part of a program is often executed multiple times while the program is running, a JIT compiler can significantly improve the total running time of the program.


2.1.2. Java platforms

Sun has defined and supported four versions of Java aimed at different application environments. Many of Java's APIs (application programming interfaces) are also grouped by platform. The four platforms are:

1. Java Card for smartcards and similar small memory devices. SIM cards and ATM cards use this platform.

2. Java Platform, Micro Edition (Java ME) is intended for embedded system environments, such as mobile phones.

3. Java Platform, Standard Edition (Java SE) is the standard, workstation-based platform commonly used for developing Java applications and Java applets. It is a widely used platform for deploying lightweight general-purpose applications. Java SE includes a Java virtual machine and a set of libraries necessary for using the file system, networking, graphical interface, etc. in the program.

4. Java Platform, Enterprise Edition (Java EE) is intended for large, distributed enterprise or Internet environments, typically used to develop servers. This platform differs from Java SE in that it has additional libraries that enable the deployment of fault-tolerant, multi-tier distributed software.

This book will only use Java as an illustration language for object-oriented programming, so it is limited to Java SE and Java application.


2.1.3. Java programming environment

A Java programming environment typically consists of a number of programs that perform different tasks to serve the purpose of composing, compiling, and running a Java program.

Any text editor can be used to write Java source code. A Java program consists of one or more class definitions. By convention, each class definition is placed in a separate file. By rule, a Java source file can contain at most one class definition with the keyword public – the meaning of this keyword will be discussed later. The file containing the class definition must have the same name as the public class in that file, for example, the file HelloWorld.java contains a public class named HelloWorld, the file HelloWorldApplet.java contains a public class named HelloWorldApplet.

Java is a case sensitive language. So if the HelloWorld class is placed in the helloworld.java file, it will cause a compilation error.

Beginners to Java should start by writing programs in a simple text editor and using the command-line tools in the JDK to compile and run the program. Even experienced programmers sometimes use this approach.

Basic steps to build and execute a Java program:

Editing: The program source code is written in a text editor and saved on disk. We can use the simplest text editors such as Notepad (in Windows environment) or emacs (in Unix/Linux environment), or editing tools in the integrated environment to write the program source code. Java source code is placed in files with the extension .java.

Translation: The Java compiler ( javac ) takes the source code file and translates it into bytecode instructions that the Java virtual machine understands, resulting in files with the .class extension.

Load and run: The Java loader ( java ) will use the Java virtual machine to run the program that has been translated into bytecode.

To facilitate and increase productivity in programming, people use integrated programming environments (IDEs). In which, the steps of compilation and execution are often combined and performed automatically, all the steps for the user are just running the features in a single software. Among the most popular IDEs for Java are Eclipse, NetBean and JBuilder.

Although IDEs are very useful for programmers, those new to the language should perform the steps of compiling and running the program themselves instead of going through the functions of the IDE. This way, new learners can grasp the essence of the steps of the program building process, understand the nature and common features of IDEs, and avoid being dependent on a specific IDE. Therefore, this book does not guide on any IDE but only uses the command-line tool in the JDK.


2.1.4. Java source code structure

Each source file (filename ending in .java) contains a class definition. Each class represents a piece of the program, a small program may contain only one class. The class definition must be enclosed in a pair of braces { }.

Each class has several methods. In the Car class, the break method contains instructions that describe how the car should brake. Class methods must be declared inside the class definition.

Inside the { } brackets of a method, we write a series of statements that define the operation of that method. We can temporarily consider Java methods to be similar to functions or subroutines.

Comment


Agree Privacy Policy *