object-oriented programming chapter two. java buzz words simple architecture neutral object oriented...

28
Object-Oriented Programming Chapter Two

Post on 20-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Object-Oriented Programming

Chapter Two

Page 2: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Java Buzz Words

• Simple• Architecture neutral• Object oriented• Portable• Distributed• High performance• Interpreted• Multithreaded• Robust• Dynamic• Secure

Page 3: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Design Goals

• Simplicity –Java’s overriding design goals.

• Removal of many features from C++.

• To illustrate the simplicity – use classic

HelloWord app.

Page 4: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

HelloWorld

class HelloWorld {static public void main(String

args[]) {System.out.println("Hello

world!");}

}//This example declares a class

name /**HelloWorld.*//*more comments*/

Page 5: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

What is a Class?• A class--the basic building block of an

object-oriented language such as Java.• A template that describes the data and

behavior associated with instances of that class.

• When you instantiate a class you create an object that looks and feels like other instances of the same class.

• The data associated with a class or object is stored in variables

• the behavior associated with a class or object is implemented with methods.

Page 6: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Definitions

• Methods are similar to the functions or procedures in procedural languages such as C.

• Class – a collection of data and methods to operate on the data.

• Instantiation - producing a particular object from its class template. This involves allocation of a structure with the types specified by the template, and initialization of instance variables with either default values or those provided by the class's constructor function.

Page 7: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Definitions Continued

• Object – an instance of a class – a class models a group of things, an object models a particular member of that group.

• Instance – an object – when a class is instantiated to produce an object, we say the object is an instance of the class.

• Variable – data items in a class – fields.• Encapsulate – technique that makes an

object’s data private or protected – allow programmers to access and manipulate – only thru method calls – data hiding.

Page 8: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Back to Code

class HelloWorld {static public void main(String

args[]) {System.out.println("Hello

world!");}

}//This example declares a class

name /**HelloWorld.*//*more comments*/

Page 9: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

HelloWorld

• Class defined as HelloWorld• Method is Main( )• Pre-defined method – println() – for

text output – this code is CLP• GUI – by adding other objects into

our code

Page 10: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

GUI

New code: JFrame frame = new Jframe( “Hello

World” ); frame.setSize( 300, 300); frame.setVisible( true );Use this to replace println() New object – JFrame title Hello WorldConfigure size – methodMake visible - method

Page 11: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

More GUI

• Now put a message in the window –import javax.swing.*;class HelloWorld {

static public void main( String args[]) {JFrame frame = new Jframe(“Hello

World”); JLabel label = new JLabel (“Hello World”,

JLabel.CENTER);frame.getContentPanel( ).add( label );frame.setSize( 300, 300 );frame.setVisible ( true );

}}

Page 12: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

A Class RW Example

• Julia Child's recipe for rack of lamb is a real-world example of a class. Her rendition of the rack of lamb is one instance of the recipe, and mine is quite another. (While both racks of lamb may "look and feel" the same, I imagine that they "smell and taste" different.)

Page 13: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Or Another Class Exp.

• A more traditional example from the world of programming is a class that represents a rectangle. The class would contain variables for the origin of the rectangle, its width, and its height. The class might also contain a method that calculates the area of the rectangle. An instance of the rectangle class would contain the information for a specific rectangle, such as the dimensions of the floor of an office, or the dimensions of a page.

Page 14: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Arguments to Main

• public static void main(String[ ] args)

-the main method accepts a single argument: an array of elements of type String

-mechanism through which the runtime system passes information to your application.

-Each String in the array is called a command-line argument

Page 15: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Command Line Arguments

• CLA’s let users affect the operation of the application without recompiling it.

• An example, a sorting program might allow the user to specify that the data be sorted in descending order with this command-line argument: -descending

• Our program ignores CLA’s at this point.

Page 16: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Back to our GUI:

import javax.swing.*;class HelloWorld {

static public void main( String args[]) {JFrame frame = new Jframe(“Hello

World”); JLabel label = new JLabel (“Hello World”,

JLabel.CENTER);frame.getContentPanel( ).add( label );frame.setSize( 300, 300 );frame.setVisible ( true );

}}

Page 17: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

What is an Object

• A pen, a desk, a TV, a computer.• Real world objects have state &

behavior.• Pen -> red, thin -> lay on desk, roll• Software object maintains its state

in one or more variables – and has behavior with methods.

• Object is a software bundle of variables and related methods.

Page 18: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

More Objects

• Everything that the software object knows (state) and can do (behavior) is expressed by the variables and the methods within that object.

• A software object that modeled a real-world bicycle would have variables that indicated the bicycle's current state: its speed is 10 mph, its pedal cadence is 90 rpm, and its current gear is the 5th gear.

• These variables are formally known as instance variables because they contain the state for a particular bicycle object, and in object-oriented terminology, a particular object is called an instance.

Page 19: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Objects cont.

• In addition to its variables, the software bicycle would also have methods to brake, change the pedal cadence, and change gears.

• These are instance methods because they inspect and change the state of a bicycle instance.

Page 20: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Class Loaders

• Second layer of security.

• Responsible for bringing the bytecode for one or more classes into the interpreter.

Page 21: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Security Managers

• Responsible for application-level security.

• An object – can be installed by an app to restrict access to system resources.

• Consulted every time the app tries to access items – filesystem, network ports – etc.

• Integrity is based on the protection afforded by the lower-levels.

Page 22: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Application & User-level Security

• Web provides a insecure environment.

• Java needs to be able to run some components even if may cause problems.

• Therefore creates the ability to ask user for okay requests.

• Security manager offers another option – to flag windows created by untrusted app – with special borders. Stop impersonations.

Page 23: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Java & the Web

• Web browser that uses the Java runtime system -> java applets as executable content inside docs.

• Enormous possibilities for Java use – with the app security manager.

• Spreadsheet apps can become fully-functional after its embedded into a web doc.

Page 24: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Applets

• Means small, subordinate or embedded app.

• To web browser an applet is just another object to display – its embedded into an HTML page with a special tag.

• Java applet is a compiled Java program, composed of classes.

Page 25: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

A Message

• A single object alone is generally not very useful.

• an object usually appears as a component of a larger program or application that contains many other objects.

• Software objects interact and communicate with each other by sending messages to each other.

Page 26: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

More about Messages

• When object A wants object B to perform one of B's methods, object A sends a message to object B.

• Sometimes, the receiving object needs more information so that it knows exactly what to do.

• Our example, when you want to change gears on your bicycle, you have to indicate which gear you want. This information is passed along with the message as parameters.

Page 27: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Visualize

The next figure shows the three components that comprise a message:

1. The object to which the message is addressed (YourBicycle) 2. The name of the method to perform (changeGears)

3. Any parameters needed by the method (lowerGear)

Page 28: Object-Oriented Programming Chapter Two. Java Buzz Words Simple Architecture neutral Object oriented Portable Distributed High performance Interpreted

Message Benefits

• Messages provide two important benefits. – An object's behavior is expressed

through its methods, so (aside from direct variable access) message passing supports all possible interactions between objects.

– Objects don't need to be in the same process or even on the same machine to send and receive messages back and forth to each other.