object orientated programming

29
Object Orientated Programming An Introduction to Java

Upload: brady-logan

Post on 03-Jan-2016

37 views

Category:

Documents


2 download

DESCRIPTION

Object Orientated Programming. An Introduction to Java. For the most accurate and up-to-date tutorials, please access the latest version from Sun's official website for the Java SE Tutorials (Last Updated 9/23/2009 ), which can be found at: http://java.sun.com/docs/books/tutorial. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Object Orientated Programming

Object Orientated Programming

An Introduction to Java

Page 2: Object Orientated Programming

• For the most accurate and up-to-date tutorials, please access the latest version from Sun's official website for the Java SE Tutorials (Last Updated 9/23/2009), which can be found at: http://java.sun.com/docs/books/tutorial.

Page 3: Object Orientated Programming

Features of OOP

• Classes

• Objects

• Instantiation

• Inheritance

• Membership Functions

Page 4: Object Orientated Programming

More OOP Features

• Data Encapsulation

• Polymorphism

• Operator Overloading

• Message Passing

• Software Re-Use

Page 5: Object Orientated Programming

History of Java

• Java

• Java, first released by Sun Microsystems in 1994

– Originally for intelligent consumer-electronic devices

– Then used for creating Web pages with dynamic content

– Now also used for:

• Develop large-scale enterprise applications

• Enhance WWW server functionality

• Provide applications for consumer devices (cell phones, etc.)

Page 6: Object Orientated Programming

The Emergence of Java

• In the past decade Java has emerged in wide use partially because of its similarity to C and to C++

• Perhaps more important is its implementation using a virtual machine that is intended to run code unchanged on many different platforms.

• This last feature has made it very attractive to larger development shops with heterogeneous environments.

Page 7: Object Orientated Programming

Key Benefits of Java

• Java is “write once, run anywhere”– architecture neutral– portable across different platforms– Due to Java Virtual Machine (JVM)

• Security features– highly configurable security levels prevent any piece of Java code doing

harm to the host system

• Network-centric platform– easy to work with resources across a network and to create network based

applications

• Object Oriented– an interacting collection of independent software components– dynamic extensible programs

Page 8: Object Orientated Programming

Key Benefits of Java

• Internationalisation– uses 16 bit Unicode characters that represents the phonetic

and ideographic character sets of the entire world

• Performance– although an interpreted language Java programs run almost

as fast as native C, C++ programs

• Simple and easy to develop– powerful & well designed set of APIs

Page 9: Object Orientated Programming

Java Class Libraries

• Classes– Include methods that perform tasks

• Return information after task completion– Used to build Java programs

• Java contains class libraries– Known as Java APIs (Application Programming Interfaces)

Page 10: Object Orientated Programming

Basics of a Typical Java Environment

• Java programs normally undergo five phases– Edit

• Programmer writes program (and stores program on disk)– Compile

• Compiler creates bytecodes from program– Load

• Class loader stores bytecodes in memory– Verify

• Verifier ensures bytecodes do not violate security requirements

– Execute• Interpreter translates bytecodes into machine language

Page 11: Object Orientated Programming

Typical Java environment.

PrimaryMemory

.

.

.

.

.

.

Disk

Disk

Disk

Editor

Compiler

Class Loader

Program is created in an editor and stored on disk in a file ending with .java.

Compiler creates bytecodes and stores them on disk in a file ending with .class.

Class loader reads .class files containing bytecodes from disk and puts those bytecodes in memory.

Phase 1

Phase 2

Phase 3

PrimaryMemory

.

.

.

.

.

.

Bytecode Verifier Bytecode verifier confirms that all bytecodes are valid and do not violate Java’s security restrictions.

Phase 4

PrimaryMemory

.

.

.

.

.

.

InterpreterInterpreter reads bytecodes and translates them into a language that the computer can understand, possibly storing data values as the program executes.

Phase 5

Page 12: Object Orientated Programming

Java

In discussing java it is important to distinguish between :

1. The Java Programming Language

2. The Java Virtual Machine

3. The Java Platform

Page 13: Object Orientated Programming

Java

1. The Java Programming Language is the language in which Java applications, applets , servlets and components are written. It is an object orientated language like C++.

2. The Java Virtual Machine, (or java interpreter) is the crucial piece of every Java installation. Java applications are portable, but they are only portable to platforms to which a java virtual machine ( or java interpreter ) has been ported.

3. The Java Platform . All programs written in Java rely on the set of predefined classes that comprise the java platform. These classes are organised into related groups known as packages. The java platform defines packages for functionality such as input/output, networking, graphics, user interface creation.

Page 14: Object Orientated Programming

JVM

class myCode {…………

}

myCode.java

Compiled by Java compiler

Application runs

Application runs

Interpreted by JVM

Source Code

1001100101001……

myCode.class

Bytecode

Page 15: Object Orientated Programming

JVM

• JVM provides the run time environment for the bytecode (Java Runtime Environment JRE)– executes the bytecode and causes native machine

code instructions to execute on the CPU that the JVM is on

each target platform needs an implementation of the JVM

Page 16: Object Orientated Programming

Java Compiler

class myCode {…………

}

myCode.java

Source Code

Errors

Compiler Byte Code

Interpreter for Computer A (JVM)

Interpreter for Computer B (JVM)

Interpreter for Computer C (JVM)

Page 17: Object Orientated Programming

Operation of the JRE (Java Runtime Environment)

______________

myCode.java

Compile Class Loader

Bytecode verifier

Interpreter

______________

myCode.class

javac

RunTime

Hardware

Load from hard disk, network or other source

Runtime

java

Page 18: Object Orientated Programming

JRE

• At runtime the java bytecode that makes up a java software program are– Loaded, checked and run in an interpreter

– In the case of applets you can download the bytecode and they are then interpreted by the JVM built into the browser.

• Bytecode verifier checks– Attributes correctly typed

– Body of the methods check

– Type checking

– References to other types checked

Page 19: Object Orientated Programming

Java Applications vs Java Applets• There are two categories of Java Programs

– A Java Application – • These are stand alone programs in the traditional sense• Run independently• Run under the java interpreter

– Java applets• These are programs designed to run from browsers such as

Netscape or Sun’s HotJava.• They don’t run independently• Programs that can be included in web documents.

Page 20: Object Orientated Programming

First Java Program (Application)

• A class definition is the basic block of a Java program• Each program must have a main method to tell it

where to start executing

// First Program HelloWorld

public class HelloWorld {

public static void main(String args []){

System.out.println(“Hello world...”):}

}

Page 21: Object Orientated Programming

Notes

• // First Program HelloWorld

• Comments begin with //

• Can also use C style comments

• /* First Program HelloWorld */

Page 22: Object Orientated Programming

Notes 2

• public class HelloWorld• This begins a class definition for class

HelloWorld• These classes are known as Programmer

defined classes or User defined classes • The public and class terms are java

keywords which are always lowercase• HelloWorld is the class identifier which

always start with an uppercase letter

Page 23: Object Orientated Programming

Notes 3

• When you save your class definition in a file the filename must be the same as the class identifier with a .java extension

• So the HelloWorld class would be stored in a HelloWorld.java file

Page 24: Object Orientated Programming

Notes 4

• public static void main(String args [])• Java applications automatically begin executing at

main• Parentheses () after main indicate a program piece

called a method which define some operational component of the class.

• Void indicates that the method will perform a task without returning any information when it completes

• String args [] are used as input arguments to the method. In this case we don’t use input but java requires it anyway

Page 25: Object Orientated Programming

Notes 5

• System.out.println(“Hello world...”):• System.out is called the standard output object• This will display a line of text in the command

window• In Java , any source and destination for I/O is

considered a stream of bytes or characters.• To perform output we insert bytes or characters

into a stream. To perform input we extract bytes or characters from a stream.

• java.lang.System class contains three predefined streams

• System.out• System.err for errors• System.in

Page 26: Object Orientated Programming

A First Java Program

• This is a Java application not a Java applet – indicated by presence of main

• Source code exists in a file with same name as the class and an extension of .java. So for the previous example it is HelloWorld.java

• Java is case sensitive (like C.)

Page 27: Object Orientated Programming

Using the JDK

• Create source files for each class in your program

• The name of source file should be the same as the name of class

public class myCode {…………

}

myCode.java

Source File

Page 28: Object Orientated Programming

Compiling your source code

• Compile each class source file into bytecode (class files)

• To compile a java source file javac myCode.java

• This creates a classfile called myCode.class

1001101001110101011…………

myCode.class

Class File

Page 29: Object Orientated Programming

To run your program

• To start your program running you run the bytecode of the program control class

• The program control class has the main method • To run bytecode – pass it to the JVM

java classFileName

e.g. java myProg

note no .class included