You are on page 1of 32

CSE 213

Review of OOP Terminology


Introduction to Java

Object-oriented programming (OOP)

Programming methodology that views a program


consisting of objects that can act alone or interact
with one another.

Similar to the real world!

Each object has the ability to perform certain


actions
Each action can affect some other objects

Class

Defines a type or kind of object. A class is a


blueprint for defining and creating objects.

Object

A program construction, representing a real-world


entity or an abstraction, that has attributes and
behaviors. An object is an instance of a class, and
all objects of the same class have the same data
type. Each object can act alone or interact with
other objects to accomplish the programs purpose.

Attribute (characteristic)

Behavior (responsibility)

State

Data associated with an object. All objects of the same


class have the same attributes. The collective values of
an objects attributes define the objects state.
An action that can be performed an object. All objects of
the same class have the same behaviors. The behaviors
of an object are defined by methods (i.e., functions and
procedures).
The current status of an object given the values of its
attributes. Object state can be different for each object
of the same class.

Encapsulation

Inheritance

Information hiding of implementation details by


combining data and actions into objects
Details are not necessary for understanding how objects
of the class are used
Ability to define a basic class and then later pass its
general attributes and behaviors to more specialized
classes

New classes can add some new details to the existing more
general class definition

Organizes a collection of classes by forming a


relationship among them
Is a relationship

Polymorphism

Enables substitution of one object for another in a


method invocation

Objects must have identical interfaces (the description/


specification of a classs public methods and named
constants)

One method name can cause different actions


depending on the context in which it is
invoked
The same program instruction can mean different
things in different contexts

Composition

We can create new objects by putting together other


objects
Since composition items do not inherit from one
another, the type of relationship is a has-a
relationship

Designed to be relatively easy for people to


understand and use
Example: Java, C, C++, Perl, Python, etc
Must be translated into a language the computer
understands

Low-level languages: machine language and


assembly language
Assembly language still needs some minor
additional translation

Compiler

Interpreter

Program that translates high-level languages to lowlevel languages


Compile once, execute often
Source -> Compiler -> Executable
Program that translates high-level languages to low
level languages
However: executes a portion of code right after
translating it
Translation alternates with execution
Translation occurs with every execution

Disadvantages of compilers/interpreters

Need a different compiler/interpreter for each type


of language or computer system

One program would need three different compilations


to run on windows, mac os x, linux systems

Expensive and time-consuming to develop

Java compiler

Does not translate program into machine language


for a particular computer system
Translates Java program into (generic) bytecode

Machine language for a virtual machine


(hypothetical computer system)
Similar to all typical computers

Java Virtual Machine (JVM)

Interpreter that translates and executes the Java


bytecode on an actual computer
User needs to install Java Runtime Environment (JRE)
on machine

Portability

Bytecode can be run on any computer without being


recompiled
You can send your bytecode over a network

Java compiler does not need to be redesigned every time a


new type of computer system is introduced
Every type of computer system needs its own bytecode
interpreter (JVM)
Simple programs compared to compiler
Easy to add to a new computer system

Transparent

Two commands: one to compile (javac), one to run (java)


Java command tells JVM to execute bytecode

User never notices translation of bytecode to machine


language

Application

Applet

Servlets

A standalone program meant to be run on your computer system


Java Web Start mechanism to deliever program and correct JRE
An applet is a program written in the Java programming language
that can be included in an HTML page, much in the same way an
image is included in a page. from java.com
A servlet is a Java programming language class used to extend
the capabilities of servers that host applications accessed via a
request-response programming model.
Typically extend HTTP-servers (CGI programming)

Performance

In general, bytecode is slower than natively


compiled code
Has gotten better with Just-In time compilation and
Hot-spot

Look & Feel

Swing GUI doesnt look quite the same as native OS


X, Windows, or Linux applications.

Java program is composed of

comments,
import statements, and
class declarations.

The McGraw-Hill Companies, Inc.


Permission required for
reproduction or display.

Ch
ap
ter
216

/*
Chapter 2 Sample Program: Displaying a Window
File: Ch2Sample2.java

*/

import javax.swing.*;

class Ch2Sample1 {
public static void main(String[ ] args) {

JFrame

myWindow = new JFrame( );

myWindow.setSize(300, 200);

myWindow.setTitle(My First Java Program);

myWindow.setVisible(true);
}

myWindow;

/*
Chapter 2 Sample Program: Displaying a Window
File: Ch2Sample2.java
*/
import javax.swing.*;
class Ch2Sample1 {
public static void main(String[ ] args) {
JFrame

myWindow;

Comment

myWindow = new JFrame( );


myWindow.setSize(300, 200);
myWindow.setTitle(My First Java Program);
myWindow.setVisible(true);
}
}
The McGraw-Hill Companies, Inc.
Permission required for
reproduction or display.

Ch
ap
ter
218

/*
This is a comment with
three lines of

Multiline Comment

text.
*/

// This is a comment
// This is another comment

Single line Comments

// This is a third comment

/**
* This class provides basic clock functions. In addition
* to reading the current time and todays date, you can
* use this class for stopwatch functions.
*/
The McGraw-Hill Companies, Inc.
Permission required for
reproduction or display.

javadoc Comments
Ch
ap
ter
219

/*
Whenever you use a
java.lang package,
Otherwise you have
only shorthand for
avoid typing fully
into program */

class that is not defined in the basic


you need to use an import directive.
to type the full name out. Importing is
referencing classes; it allows user to
qualified names. Classes are not copied

Import
Statement

import javax.swing.*;
class Ch2Sample1 {
public static void main(String[ ] args) {
JFrame

myWindow;

myWindow = new JFrame( );


myWindow.setSize(300, 200);
myWindow.setTitle(My First Java Program);
myWindow.setVisible(true);
}
}

The McGraw-Hill Companies, Inc.


Permission required for
reproduction or display.

Ch
ap
ter
220

Package Name
Name of the package that
contains the classes we
want to use.

Class Name
The name of the class we
want to import. Use asterisks
to import all classes.

<package name>
e.g.

More
Examples

dorm

import
import
import

<classname> ;
Resident;

javax.swing.JFrame;
java.util.*;
com.drcaffeine.simplegui.*;

Chapter 2

- 21

The McGraw-Hill
Companies, Inc. Permission
required for reproduction
or display.

/*
Chapter 2 Sample Program: Displaying a Window

Class
Declaration

File: Ch2Sample2.java
*/
import javax.swing.*;
class Ch2Sample1 {
public static void main(String[ ] args) {
JFrame

myWindow;

myWindow = new JFrame( );


myWindow.setSize(300, 200);
myWindow.setTitle(My First Java Program);
myWindow.setVisible(true);
}
}
The McGraw-Hill Companies, Inc.
Permission required for
reproduction or display.

Ch
ap
ter
222

/*
Chapter 2 Sample Program: Displaying a Window

Method
Declaration

File: Ch2Sample2.java
*/
import javax.swing.*;
class Ch2Sample1 {
public static void main(String[ ] args) {
JFrame

myWindow;

myWindow = new JFrame( );


myWindow.setSize(300, 200);
myWindow.setTitle(My First Java Program);
myWindow.setVisible(true);
}
}
The McGraw-Hill Companies, Inc.
Permission required for
reproduction or display.

Ch
ap
ter
223

Modifier

public

Modifier

static

Return Type

Method Name

void

main(

Parameter

String[ ] args

){

Method Body

JFrame myWindow;
myWindow = new JFrame( );
myWindow.setSize(300, 200);

myWindow.setTitle(My First Java Program);


myWindow.setVisible(true);
}

The McGraw-Hill Companies, Inc.


Permission required for
reproduction or display.

Ch
ap
ter
224

/*
Chapter 2 Sample Program: Displaying a Window

Comment

File: Ch2Sample2.java
*/

Import
Statements

import javax.swing.*;
class Ch2Sample1

public static void main(String[ ] args) {


JFrame myWindow;

Class Name

myWindow = new JFrame( );


myWindow.setSize(300, 200);

Method Body

myWindow.setTitle(
My First Java Program);
myWindow.setVisible(true);
}
}

The McGraw-Hill Companies, Inc.


Permission required for
reproduction or display.

Ch
ap
ter
225

Class

A piece of software that we can use in a program to


instantiate objects.

Package

Library of classes that have already been defined for


you.

Object

An instance of a class used to perform actions that


are defined by methods.

Method

Method call (invocation)

Argument

A collection of statements for accomplishing a specific task.


Every Java application needs a main method. Actions are
performed when a method of an object is invoked or called.
A message to an object consisting of the name of the object
followed by a dot and the methods name with
parentheses that may or may not include arguments to the
method.

Provides information to methods that they need to carry out


their defined actions. Arguments are also known as
parameters and can be input to a method or output from a
method.

Statement

An instruction within a method that helps to define a


task. Each statement is terminated by a semicolon.

Variable

A named place in memory where a piece of data can be


stored.

Data type

Specifies a set of possible values for a variable,


argument, or object and the operations defined for those
values.

Java programs are divided into smaller parts called classes


Each program can consist of any number of class definitions
Our example (which we will demo in class) includes the following
classes:
Calculate: written by us
System and Scanner: provided by Java

Normally, each class definition is in a separate file

Name of the file must be the name of the class with .java
extension
Example: Calculate.java is a java source code file.

You cannot expect the users of your program to know


what you want them to do
Give the user understandable instructions

Dont reinvent the wheel. When there are existing


objects that satisfy our needs, use them.
Saves time and money
Better tested and more reliable

Learning how to use standard Java classes is the


first step toward mastering OOP. Before we can
learn how to define our own classes, we need to
learn how to use existing classes
Java SE 6 API contains 203 packages and 3793
classes
Chapter 2 - 30

The McGraw-Hill
Companies, Inc. Permission
required for reproduction
or display.

Compile using the command javac followed


by the name of the file containing the class
Example: javac Calculate.java

javac produces bytecode and creates a file


whose name is the name of the class with
a .class extension
Example: Calculate.class

Run (execute) using the command java


followed by the name of the class you think
of as the program

Use the name of the class, not the name of the file
containing the class or its bytecode
Example: java Calculate (not java Calculate.class)

The command java <Class name> runs the


Java bytecode interpreter on the compiled
version of the program

You might also like