Object-Oriented Programming

carenextΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 1 μήνα)

50 εμφανίσεις

Object
-
Oriented Programming

Chapter Two

Java Buzz Words


Simple


Architecture neutral


Object oriented


Portable


Distributed


High performance


Interpreted


Multithreaded


Robust


Dynamic


Secure

Design Goals


Simplicity

Java’s overriding design
goals.


Removal of many features from C++.



To illustrate the simplicity


use classic

HelloWord app.

HelloWorld

class HelloWorld {


static public void main(String args[])

{


System.out.println("Hello world!");


}

}

//This example declares a class name
/**HelloWorld.*/

/*more comments*/




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
.

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.


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.

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*/



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

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 World

Configure size


method

Make visible
-

method

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 );


}

}

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.)

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.


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


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.



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 );


}

}


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.

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
.

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.

Class Loaders


Second layer of security.



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

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.

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.

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.

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.

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.


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
.

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
)




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.