Two Paradigms process-oriented model object-oriented programming Object Oriented Programming
An essential element of object-oriented programming is abstraction . Humans manage complexity through abstraction. Abstraction
All object-oriented programming languages provide mechanisms that help you implement the object-oriented model. They are encapsulation, inheritance, and polymorphism. The Three OOP Principles
Encapsulation
Inher itance
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 floatingpoint 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. Polymorphism
The way Java works The goal is to write one application (in this example, an interactive party invitation) and have it work on whatever device your friends have.
What you’ll do in Java You’ll type a source code file, compile it using the javac compiler, and then run the compiled bytecode on a Java virtual machine .
Anatomy of a class When the JVM starts running, it looks for the class you give it at the command line. Then it starts looking for a specially written method that looks exactly like: public static void main (String[] args ) { // your code goes here } Next, the JVM runs everything between the curly braces { } of your main method. Every Java application has to have at least one class , and at least one main method (not one main per class ; just one main per application ).
Writing a class with a main() In Java, everything goes in a class . You’ll type your source code file (with a .java extension), then compile it into a new class file (with a .class extension). When you run your program, you’re really running a class. Running a program means telling the Java Virtual Machine (JVM) to “Load the MyFirstApp class, then start executing its main() method. Keep running ’til all the code in main is finished.” The main() method is where your program starts running. public class MyFirstApp { public static void main (String[] args ) { System.out.println ("I Rule!"); System.out.println ("The World"); } }
A Second Short Program /* Here is another short example. Call this file "Example2.java". */ class Example2 { public static void main(String args []) { int num ; // this declares a variable called num num = 100; // this assigns num the value 100 System.out.println ("This is num: " + num); num = num * 2; System.out.print ("The value of num * 2 is "); System.out.println ( num ); } }
Using Blocks of Code Java allows two or more statements to be grouped into blocks of code , also called code blocks . if(x < y) { // begin a block x = y; y = 0; } // end of block
The Java Keywords
The Java Class Libraries println ( ) and print( ) , these methods are available through System.out . System is a class predefined by Java that is automatically included in your programs. the Java environment relies on several built-in class libraries that contain many built-in methods that provide support for such things as I/O, string handling, networking, and graphics. The standard classes also provide support for a graphical user interface (GUI). Thus, Java as a totality is a combination of the Java language itself, plus its standard classes.