




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































Prepared By: BENIMANA PASCAL IPRC-Kigali i
Prepared by:
All object-oriented programming languages provide mechanisms that help you implement the object-oriented model. They are encapsulation, inheritance, and polymorphism.
Encapsulation
Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse.
One way to think about encapsulation is as a protective wrapper that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper.
In Java, the basis of encapsulation is the class. A class defines the structure and behavior (data and code) that will be shared by a set of objects.
When you create a class, you will specify the code and data that constitute that class. Collectively, these elements are called members of the class. Specifically, the data defined by the class are referred to as member variables or instance variables. The code that operates on that data is referred to as member methods or just methods.
Encapsulation is a set of mechanisms for hiding the complexity of the implementation inside the class. Each method or variable in a class may be marked private or public. The public interface of a class represents everything that external users of the class need to know, or may know. The private methods and data can only be accessed by code that is a member of the class. Therefore, any other code that is not a member of the class cannot access a private method or variable. Since the private members of a class may only be accessed by other parts of your program through the class‘ public methods, you can ensure that no improper actions take place. Of course, this means that the public interface should be carefully designed not to expose too much of the inner workings of a class.
Inheritance
Inheritance is basically reusing code from one class into another related class. It is sharing of common characteristics from one class into other related classes. In our daily lives, we use the concept of classes being divided into subclasses. The class of vehicles is divided into cars,
trucks, buses and motorcycles. The class of computers is divided into personal, mini, mainframe and super computer.
Polymorphism
Polymorphism (from Greek, meaning ―many forms‖) is a feature that allows one interface to be used for a general class of actions. The specific action is determined by the exact nature of the situation.
Consider a stack (which is a last-in, first-out list). You might have a program that requires three types of stacks. One stack is used for integer values, one for floating-point values, and one for characters. The algorithm that implements each stack is the same, even though the data being stored differs. In a non–object-oriented language, you would be required to create three different sets of stack routines, with each set using different names. However, because of polymorphism, in Java you can specify a general set of stack routines that all share the same names.
More generally, the concept of polymorphism is often expressed by the phrase ―one interface, multiple methods.‖ This means that it is possible to design a generic interface to a group of related activities. This helps reduce complexity by allowing the same interface to be used to specify a general class of action. It is the compiler‘s job to select the specific action (that is, method) as it applies to each situation. You, the programmer, do not need to make this selection manually. You need only remember and utilize the general interface.
Polymorphism, Encapsulation, and Inheritance Work Together
When properly applied, polymorphism, encapsulation, and inheritance combine to produce a programming environment that supports the development of far more robust and scalable programs than does the process-oriented model. A well-designed hierarchy of classes is the basis for reusing the code in which you have invested time and effort developing and testing.
Encapsulation allows you to migrate your implementations over time without breaking the code that depends on the public interface of your classes. Polymorphism allows you to create clean, sensible, readable, and resilient code.
Programs
You probably have some idea of what a program is. You use programs all the time. For example, text editors and word processors are programs. When you give the computer a program and some data and tell the computer to follow the instructions in the program, you are running, or executing, the program on the data.
When you first turn on a computer, you are already running and interacting with a program. That program is called the operating system. The operating system is a kind of supervisory program that oversees the entire operation of the computer. If you wont to run a program, you tell the operating system what you want to do. The operating system then retrieves and starts the program. The program you run might be a text editor, a browser to surf the World Wide Web, or some program that you wrote using the Java language. You might tell the operating system to run the program by using a mouse to click an icon, by choosing a menu item, or by typing in a simple command.
Programming Languages, Compilers, and Interpreters
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely.
Computer programming language innovation and development occurs for two fundamental reasons: To adapt to changing environments and uses To implement refinements and improvements in the art of programming
Most modern programming languages are designed to be relatively easy for people to understand and use. Such languages are called high-level languages.
Java is a high-level language. Most other familiar programming languages, such as C, C++, COBOL, Visual Basic, C# … are also High-level languages. Unfortunately Computer hardware cannot understand high-level languages. Before a program written in a high-level language can be run, it must be translated into a language that the computer can understand.
The language that the computer can directly understand is called machine language.
The translation of a program from a high-level language, like Java, to a low-level language is performed entirely or in part by another program. For some high-level languages, this translation is done as a separate step by a program known as a compiler.
A compiler is a program that translates a program written in a high-level language, such as Java, into a program in a simpler language that the computer can more or less directly understand.
Some high-level languages are translated not by compilers but rather by another kind of program called an interpreter. Like a compiler, an interpreter translates program statements from a high- level language to a low-level language. But unlike a compiler, an interpreter executes a portion of code right after translating it, rather than translating the entire program at once.
Recall that compilation is done once, and the resulting object program can be run over and over again without engaging the compiler again. This implies that a compiled program generally runs faster than an interpreted one.
An interpreter is hence a program that alternates the translation and execution of statements in a program written in a high-level language.
Java programming lineage
Java is related to C++, which is a direct descendant of C. Much of the character of Java is inherited from these two languages. From C, Java derives its syntax. Many of Java‘s object- oriented features were influenced by C++. In fact, several of Java‘s defining characteristics come from or are responses to its predecessors. Moreover, the creation of Java was deeply rooted in the process of refinement and adaptation that has been occurring in computer programming languages for the past several decades.
The Creation of Java
Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. It took 18 months to develop the first working version. This language was initially called ―Oak,‖ but was renamed ―Java‖ in 1995. Between the
First, you use the compiler to translate your Java program into bytecode. Then you use the particular JVM for your computer system to translate each bytecode instruction into machine language and to run the machine-language instructions. The whole process is shown in Figure 1.
Figure 1: Compiling and Running a Java Program
The Java compiler translates your Java program into a language called bytecode. This bytecode is not the machine language for any particular computer, but it is similar to the machine language of most common computers. Bytecode is easily translated into the machine language of
a given computer. Each type of computer will have its own translator called an interpreter that translates from bytecode instructions to machine-language instructions for that computer.
How Java program works
Figure 2: Running a java application on one computer
For different platforms
Figure 3: Running java application on different computers
Java Development Kit (JDK)
In order to write a Java program, an editor, a Java compiler and a Java Runtime Environment are needed. The easiest way to get a Java compiler and a Java Runtime Environment is to download Oracle‘s Java Development Kit.
class is a keyword (all the programs in java starts with class) and Prog is the class name
the main method in java using public static and the arguments passed in to that program will be considered as Strings
System.out.println() is a method to print the output.
Program Statements
A program statement is where all the action takes place. There are three categories of statements:
Simple and compound statements are compiled and become part of your application‘s binary image; all compiled statements must end with a semicolon (;). Comments are not compiled and are visible only in the source code, but they can be invaluable several months later when you (or someone maintaining your code) are trying to determine the original purpose of that code.
Simple Statements
Simple statements can assign values, perform an action, or call a method.
A method is the term used in Java to indicate any subprogram (subroutine, function, or procedure).
Here are some examples of simple statements:
currpoint = new Point(x,y);
if (i==8) j = 4;
g.drawString(―Hello world!‖, 10, 30);
repaint();
Block Statements
You generally can use a block anywhere that a single statement is valid, and the block creates a new local scope for the statements within it. This means that you can declare and use local variables within a block, and those variables will cease to exist after the block is finished executing.
A block statement (or simply, a block ) is a group of other program statements surrounded by braces ({}). This is the same way braces are used in C and C++ and is analogous to the begin..end pair in Pascal. It is also sometimes known as a compound statement.
Here‘s an example that highlights the differences in scope. In the following method defi-nition, x is declared for the method as a whole, and y is declared within the block:
void testblock() { int x = 10; { // block begin int y = 50; System.out.println(“inside the block:”); System.out.println(“x:” + x); // prints 10 System.out.println(“y:” + y); // prints 50 } // block end System.out.println(“outside the block:”); System.out.println(“x:” + x); // prints 10 System.out.println(“y:” + y); // error, outside the scope }
\. The \ tells the compiler you mean a real backslash, , not an escape sequence, and " means a quote character, not the end of a string constant.
" Double quote. ' Single quote. \ Backslash. \n New line. Go to the beginning of the next line. \r Carriage return. Go to the beginning of the current line. \t Tab. White space up to the next tab stop.
Operators
Operators are special symbols used in expressions. They include arithmetic operators, assignment operators, increment and decrement operators, logical operators and comparision operators.
Arithmetic Operators : Java has five basic arithmetic operators. Each of these operators takes two operands.
One on either side of the operator (see table2).
Operator Meaning Example
The subtraction operator is also used to negate a single operand. Integer division results in an integer. Any remainder due to integer division is ignored. Modulus gives the remainder once the division has been completed. For integer types, the result is an int regardless of the type of the operands. If any one or both the operands is of type long, then the result is also long. If one operand is an int and the other is float , then the result is a float.
The main assignment operator is ‗=‘. Other complex assignment operators used in C and C++ are also used in Java. A selection of assignment operators is given in the table below.
Expression Meaning x + =y x=x+y x - =y x=x-y x =y x=xy x /=y x=x/y x %=y x=x%y Table 3: Assignments Operators
Incrementing and Decrementing
To increment or decrement a value by one, the ++operator and -- operator are used respectively. The increment and the decrement operators can be prefixed or postfixed. The different increment and decrement operators can be used as given below:
++a (Pre increment Operator): Increment by 1, then use the new value of a in the expression in which a resides. a++ (Post Increment Operator): Use the current value of a in the expression in which a resides and then increment a by 1. --b (Pre decrement operator): Decrement b by 1, then use the new value of b in the expression in which b resides. b-- (Post decrement operator): Use the current value of b in the expression in which b resides and then decrement b by 1.
Relational operators : The relational operators determine the relationship that one operand has to the other. Specifically, they determine equality and ordering. The relational operators are shown here: Operators Meaning == Equal to != Not equal to