SlideShare a Scribd company logo
Introduction
Elizabeth MacDonald
The State University of New York at Albany
What we will cover
tonight
• History of Java
• Benefits of Java
• Java Environment
• Types of Java programs
• The Java Platforms
• UML
• Structured vs. Object Oriented Programming
• Basics of a Java application
The History of Java
Code-Name Green
• The name for Sun’s internal corporate
research project aimed at providing
intelligent consumer-electronic devices.
• Result was Oak
– C++ based language
– Created by James Gosling
– Named after an Oak tree outside his office
– Name of new language changed to Java
Green in the red?
• Green project didn’t go far
– Marketplace for intelligent consumer-
electronic devices was slow to rise
– Sun didn’t win certain contracts
– Was on the verge of being cancelled
Saved by the Web!
• World Wide Web popularity explosion in
1993 saved project Green
• Sun saw the potential for Java providing
dynamic content to web pages
• Java was formally announced in 1995
• Java is now used for large-scale
enterprise applications, applications for
consumer devices, and many other
purposes.
Benefits of Java
“Write once run
anywhere”
• Platform independent and architecturally
neutral
• As opposed to C++, you do not need to
recompile
• This is due to the Java Virtual Machine
(we will discuss that in a moment)
• Bytecode can be ported across platforms
with compatible versions of the JVM
Rich libraries
• The term Java refers to both the language
you can use to develop your own libraries,
as well as the numerous existing classes
that have already been developed.
– Simplifies programming
– Includes classes for building Graphical User
Interfaces, file I/O, networking, web
applications, etc.
Internationalization
• Unlike most other languages, Java uses
16 bit Unicode characters
– Can represent the phonetic and ideographic
character sets of the entire world
– C++ uses 8 bits ~ 256 different characters
– 16 bit Java char ~ 65,535 different characters
Distributed
• Java classes can execute remotely
• Has full support for serialization into
bytecode
• Has networking capabilities built in
– ADTs for distributed message passing
Security
• Java has customizable Security settings
• Java applets
– Users would be afraid to run applets that
could damage their machines
– Prevent applets from doing any local file I/O
or killing other running processes…
Threading
• Java simplifies multithreading
• A thread is a lightweight process
• Multiple processes run through the same
set of instructions in memory, as opposed
to loading the application code into
different memory locations
• Java has encapsulated threads as an
Abstract Data Type
Object Oriented
• Java is a truly Object Oriented
programming language
• Everything has to reside in a class
– Main method and all other methods have to
be in a class
• Has support for inheritance,
polymorphism, message passing, …
The Java Environment
To run a Java
Program
– Java instructions need to be translated
into an intermediate language called
bytecode
– Then the bytecode is interpreted into a
particular machine language
What is needed…
• Compiler: A program that translates a
program written in a high-level language into
the equivalent machine language.
– In the case of Java, this machine language is the
bytecode.
• Java Virtual Machine (JVM) - hypothetical
computer developed to make Java programs
machine independent
Java Virtual Machine
• Java programs execute on the JVM.
• The JVM is a virtual rather than a physical
machine (although the JVM has been
implemented in silicon.)
• The JVM is typically implemented as a
run-time interpreter
• Translates Java bytecode instructions 
native instruction codes
Processing a Java
Program
• Source program: Written in a high-level
language
• Linker: Combines bytecode with other programs
provided by the SDK and creates executable
code
• Loader: transfers executable code into main
memory
• Interpreter: reads and translates each bytecode
instruction into machine language and then
executes it
Processing a Java
Program
Java Runtime
Environment
• The java utility in the SDK provides the JVM as a
run-time interpreter.
• The JVM provides a run-time environment (JRE)
• Enables programs to execute on a host platform.
• The Java runtime can work with a security
manager to determine which operations a
program can perform on the host platform.
Types of Java Programs
Program types
• Java has four program types, although the
differences among them are shallow.
• The two most commonly used types are:
– Application
– Applet
Java Application
• Standalone program in the sense of
requiring only the JVM to execute.
• Does not require a host program such as a
browser.
• It’s main method is used as its entry point
• Has to have a method with the following
signature…
public static void main(String args[ ])
Java Applet
• Small program typically downloaded from a
server to a client machine.
• JVM is built into the web browser
• A Web browser acts as the host program for an
applet.
• An applet is typically launched through an HTML
or equivalent document.
• They are imbedded into HTML with the <applet>
tag
Java Applet (cont’d)
• An applet typically operates under a strict
security manager, which imposes sandbox
security.
• This prevents an applet from performing
potentially dangerous operations such as
reading from or writing to the local disk.
• An applet’s class descends from the standard
Applet or JApplet class.
Unified Modeling Language
UML (part 1)
• Unified Modeling Language
– Meant to facilitate the design, development,
deployment and maintenance of software
systems.
– Standardized in 1997 by the Object
Management Group.
– Combines methods from Object Modeling
Technique (OMT) and Object-Oriented Software
Engineering (OOSE).
• UML diagrams can serve as high-level design
specifications for software systems and, in this role,
can guide code development.
UML (part 2)
• Basic UML vocabulary consists of
– Things: Entities to be modeled, whether
concrete or abstract.
– Relationships: Connections among things.
– Diagrams: graphical depictions of things and
their relationships
• We will discuss the types of UML
diagrams as they come up.
Object Oriented vs.
Structured Programming
Structured Approach
• Subset of procedural programming
• Enforces a logical structure on the program being written
to make it more efficient and easier to understand and
modify.
• Frequently employs a top-down design model
– Developers map out the overall program structure into separate
subsections.
– A defined function or set of similar functions is coded in a
separate module or submodule, which means that code can be
loaded into memory more efficiently and that modules can be
reused in other programs.
– After a module has been tested individually, it is then integrated
with other modules into the overall program structure.
Object Oriented
Approach
• Provides a more natural and intuitive way
to view the design process
• Modeling software components just as we
describe real-world objects
– By their attributes, behaviors, and the
messages they pass between each other
• Objects
– Reusable software components
– Building blocks
Benefits of OO
• Quality
– Small, small-contained manageable objects reduces
the complexity of the system development
– Less error prone
• Productivity
– More reuse
• Flexibility
– Can add or modify objects while minimizing the
impact on the rest of the system
– This is achieved via encapsulation
Java and OO
• Just because it’s written in Java, does not
mean it is object-oriented
• The standard Java libraries are not always
good examples of OO programming.
• Compromise between practical and
efficiency considerations and truly good
OO design
What is an object-
oriented system?
• Has the following 6 properties:
– Abstraction with objects
– Encapsulated classes
– Communication via messages
– Object lifetime
– Class hierarchies
– Polymorphism
Abstraction
• A model of a real-world object or concept
• Usually a simplified representation of a
complex situation
• Designing objects can be difficult
• Have to decide on what attributes and
behaviors are necessary
• Address book example
Encapsulated Classes
• Remember abstract data types?
• The process of hiding all the internal
details of an object from the outside
world.
• In Java, this is having definitions for
attributes and methods inside a class
definition.
• State is changed using methods in the
class.
Communication via
Messages
• Messages are how objects communicate with
each other.
• Follows client/server model.
• Objects can send and receive messages.
• Send = invoke a class method
• Receive = have your method called by another
object
• Can also be invoked by the language runtime
system or the operating system.
Class Hierarchies
• An ordering of classes.
• This includes:
– Association
– Aggregation
– Composition
– Inheritance
Association
• A relationship between two classes.
• Indicates how the objects of the classes relate
to each other.
• The relationship has some sort of name (verb).
• Multiplicity specifies the number of objects that
can participate in the association.
Teacher Student
teaches
learns from
*
*
Aggregation
• has-a relationship
• The aggregate object includes other
objects
• The included objects are considered to be
a part-of the aggregate
Car Dealership Car
*
Composition
• Special case of aggregation
• The whole cannot exist without the
aggregate object(s)
Car Engine
1
Inheritance
• is-a relationship
• A mechanism that allows one class (the
subclass) to share the attributes and
behaviors of another class (the superclass).
• A subclass is a specialization of the
generalized superclass.
• The derived class may override or provide
an alternate definition for an attribute or
method of the superclass, or use the default
behaviors of the superclass.
Inheritance Example
Animal
Warm Blooded Cold Blooded
Mammal
Bird
Flying Non-Flying
Parrot Penguin
More on Inheritance
• In Java, all classes are implicitly derived from
the Object class, making Object the root
class.
• Single inheritance is when a class is directly
derived from only 1 parent class.
• Multiple inheritance is when a class is derived
from more than one parent classes.
• Unlike other languages, Java does not support
multiple inheritance.
Multiple Inheritance is
BAD
• Repeated Inheritance – a base can
occur more than once in the parents list.
• Ambiguity – a feature or method occurs
more than once among the parents…
which one is called?
• This brings us to interfaces, the
alternative to multiple inheritance.
Interfaces
• In psychology, a “mask” that allows a person to
behave in a manner appropriate to a particular
role.
• In Java, the specification of methods that a
class using the interface must implement, or
provide code for.
• Defines the is-a relationship between classes.
• You will NOT find code in an interface.
Polymorphism
• Dynamic binding of an object to the
appropriate method. (The actual binding of a
call to a specific method is delayed until
runtime.)
GoodStudent BadStudent
Student
+takeExam() : int
+takeExam() : int
+takeExam() : int
Basics of a Java Application
Jumping into Java
Programming
• To create and run a Java program
– Create a text file with a .java extension for
the source code. For instance, source code
for a first program might reside in the file
Hi.java.
– Write the source code.
– Compile the source code into binary code.
– Execute the binary code.
Compiling
• Suppose that the Java source code
resides in the plain text file Hi.java. Using
the $ as the system prompt, the source
code is compiled with the command
$ javac Hi.java
The javac utility comes with the Java SDK.
Compiling
• Compiling a source file such as Hi.java
generates one or more binary files with a
.class extension, e.g., Hi.class.
– If fatal errors occur during compilation, the
process ends with appropriate error
messages.
– If nonfatal errors occur during compilation,
the process continues with appropriate
warnings.
Executing
• Once the program has been compiled, it
can executed with the java utility. For
instance, if the file Hi.class contains a
compiled standalone program, the
command
$ java Hi
executes the program.
Compiling versus
executing
• Note that the .java extension is included
in the compilation but that the .class
extension is not included in the execution.
Note the contrast:
$ javac Hi.java
$ java Hi
A first program (part 1)
// This program prints
// Hello, world!
// to the standard output.
class Hi {
public static void main( String[ ] a ){
System.out.println( “Hello, world!” );
}
}
A first program (part 2)
• The source code for the Hi program resides in a
text file with a .java extension, Hi.java.
• Every Java program requires at least one
source code class, as the Hi example
illustrates.
• A public class must reside in a .java file of the
same name.
• There can be at most one public class definition
in a file.
A first program (part 3)
• The Hi program is a Java application rather
than, for instance, an applet, or a servlet,
which are other program types.
• A Java application is the most general and
basic program type.
• An application must have a public static method
called main, that takes in an array of Strings:
public static void main(String args[]) {}
A first program (part 4)
• The syntax
String[] a
indicates that a (for “array”) refers to an
array. In general, the square brackets
indicate an array. In this case, the array
can hold references to Strings, which
are any command-line arguments passed
to the program.
A first program (part 5)
• In the print statement
System.out.println( “Hello, world!” );
– System is a standard Java class that
represents the system on which the program
executes.
– out is a static field in the System class
and its type is PrintStream, another
standard Java class.
– println is a PrintStream method.
A first program (part 6)
• In the Hi program’s print statement, the
parts System, out, and println are
separated by the member operator, the
period.
• The print statement is terminated by a
semicolon.
– In Java, the semicolon terminates
statements.

More Related Content

PPT
Object oriented programming_Unit1_Introduction.ppt
AnujaS24
 
PDF
Unit 1 Core Java for Compter Science 3rd
prat0ham
 
PPTX
Java Introduction
sunmitraeducation
 
PPTX
object oriented programming examples
Abdii Rashid
 
PPT
Comp102 lec 3
Fraz Bakhsh
 
PPT
Java ppt-class_Introduction_class_Objects.ppt
VGaneshKarthikeyan
 
PPT
Java ppt-class_basic data types methods definitions
ganeshkarthy
 
PPTX
Introduction to Java
Soumya Suman
 
Object oriented programming_Unit1_Introduction.ppt
AnujaS24
 
Unit 1 Core Java for Compter Science 3rd
prat0ham
 
Java Introduction
sunmitraeducation
 
object oriented programming examples
Abdii Rashid
 
Comp102 lec 3
Fraz Bakhsh
 
Java ppt-class_Introduction_class_Objects.ppt
VGaneshKarthikeyan
 
Java ppt-class_basic data types methods definitions
ganeshkarthy
 
Introduction to Java
Soumya Suman
 

Similar to JAVA object oriented programming (oop).ppt (20)

PPTX
unit1.pptx
PrasadKalal4
 
PPTX
Untitled presentation about object oriented.pptx
janetvidyaanancys
 
PPTX
1 java introduction
abdullah al mahamud rosi
 
PPTX
1 java intro
abdullah al mahamud rosi
 
PPTX
CS8392 OOP
DhanalakshmiVelusamy1
 
PDF
1. JAVA_Module_1-edited - AJIN ABRAHAM.pptx.pdf
10322210023
 
PPTX
oop unit1.pptx
sureshkumara29
 
PPTX
JAVA INTRODUCTION - 1
Infoviaan Technologies
 
PPT
2-Lec - History of OOP and Java (1) .ppt
AqeelAbbas94
 
PPTX
2 22CA026_Advance Java Programming_Data types and Operators.pptx
dolphiverma80
 
PPTX
Vb.net basics 1(vb,net--3 year)
Ankit Gupta
 
PPTX
Java fundamentals
Om Ganesh
 
PPT
Unit 1- Basic concept of object-oriented-programming.ppt
hannahroseline2
 
PPTX
Java training in bangalore
zasi besant
 
PPTX
java slides
RizwanTariq18
 
PDF
Introduction to Java Programming
Ravi Kant Sahu
 
PPT
Fundamentals of JAVA
KUNAL GADHIA
 
PPTX
MWLUG - Universal Java
Philippe Riand
 
DOCX
java completed units.docx
SATHYAKALAKSKPRCASBS
 
DOCX
java full 1.docx
SATHYAKALAKSKPRCASBS
 
unit1.pptx
PrasadKalal4
 
Untitled presentation about object oriented.pptx
janetvidyaanancys
 
1 java introduction
abdullah al mahamud rosi
 
1. JAVA_Module_1-edited - AJIN ABRAHAM.pptx.pdf
10322210023
 
oop unit1.pptx
sureshkumara29
 
JAVA INTRODUCTION - 1
Infoviaan Technologies
 
2-Lec - History of OOP and Java (1) .ppt
AqeelAbbas94
 
2 22CA026_Advance Java Programming_Data types and Operators.pptx
dolphiverma80
 
Vb.net basics 1(vb,net--3 year)
Ankit Gupta
 
Java fundamentals
Om Ganesh
 
Unit 1- Basic concept of object-oriented-programming.ppt
hannahroseline2
 
Java training in bangalore
zasi besant
 
java slides
RizwanTariq18
 
Introduction to Java Programming
Ravi Kant Sahu
 
Fundamentals of JAVA
KUNAL GADHIA
 
MWLUG - Universal Java
Philippe Riand
 
java completed units.docx
SATHYAKALAKSKPRCASBS
 
java full 1.docx
SATHYAKALAKSKPRCASBS
 
Ad

Recently uploaded (20)

PDF
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
DOCX
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
PDF
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PDF
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
PDF
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pdf
Certivo Inc
 
PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
PDF
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
PPTX
oapresentation.pptx
mehatdhavalrajubhai
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
PPTX
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
PDF
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
Presentation about variables and constant.pptx
kr2589474
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pdf
Certivo Inc
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
oapresentation.pptx
mehatdhavalrajubhai
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Ad

JAVA object oriented programming (oop).ppt

  • 1. Introduction Elizabeth MacDonald The State University of New York at Albany
  • 2. What we will cover tonight • History of Java • Benefits of Java • Java Environment • Types of Java programs • The Java Platforms • UML • Structured vs. Object Oriented Programming • Basics of a Java application
  • 4. Code-Name Green • The name for Sun’s internal corporate research project aimed at providing intelligent consumer-electronic devices. • Result was Oak – C++ based language – Created by James Gosling – Named after an Oak tree outside his office – Name of new language changed to Java
  • 5. Green in the red? • Green project didn’t go far – Marketplace for intelligent consumer- electronic devices was slow to rise – Sun didn’t win certain contracts – Was on the verge of being cancelled
  • 6. Saved by the Web! • World Wide Web popularity explosion in 1993 saved project Green • Sun saw the potential for Java providing dynamic content to web pages • Java was formally announced in 1995 • Java is now used for large-scale enterprise applications, applications for consumer devices, and many other purposes.
  • 8. “Write once run anywhere” • Platform independent and architecturally neutral • As opposed to C++, you do not need to recompile • This is due to the Java Virtual Machine (we will discuss that in a moment) • Bytecode can be ported across platforms with compatible versions of the JVM
  • 9. Rich libraries • The term Java refers to both the language you can use to develop your own libraries, as well as the numerous existing classes that have already been developed. – Simplifies programming – Includes classes for building Graphical User Interfaces, file I/O, networking, web applications, etc.
  • 10. Internationalization • Unlike most other languages, Java uses 16 bit Unicode characters – Can represent the phonetic and ideographic character sets of the entire world – C++ uses 8 bits ~ 256 different characters – 16 bit Java char ~ 65,535 different characters
  • 11. Distributed • Java classes can execute remotely • Has full support for serialization into bytecode • Has networking capabilities built in – ADTs for distributed message passing
  • 12. Security • Java has customizable Security settings • Java applets – Users would be afraid to run applets that could damage their machines – Prevent applets from doing any local file I/O or killing other running processes…
  • 13. Threading • Java simplifies multithreading • A thread is a lightweight process • Multiple processes run through the same set of instructions in memory, as opposed to loading the application code into different memory locations • Java has encapsulated threads as an Abstract Data Type
  • 14. Object Oriented • Java is a truly Object Oriented programming language • Everything has to reside in a class – Main method and all other methods have to be in a class • Has support for inheritance, polymorphism, message passing, …
  • 16. To run a Java Program – Java instructions need to be translated into an intermediate language called bytecode – Then the bytecode is interpreted into a particular machine language
  • 17. What is needed… • Compiler: A program that translates a program written in a high-level language into the equivalent machine language. – In the case of Java, this machine language is the bytecode. • Java Virtual Machine (JVM) - hypothetical computer developed to make Java programs machine independent
  • 18. Java Virtual Machine • Java programs execute on the JVM. • The JVM is a virtual rather than a physical machine (although the JVM has been implemented in silicon.) • The JVM is typically implemented as a run-time interpreter • Translates Java bytecode instructions  native instruction codes
  • 19. Processing a Java Program • Source program: Written in a high-level language • Linker: Combines bytecode with other programs provided by the SDK and creates executable code • Loader: transfers executable code into main memory • Interpreter: reads and translates each bytecode instruction into machine language and then executes it
  • 21. Java Runtime Environment • The java utility in the SDK provides the JVM as a run-time interpreter. • The JVM provides a run-time environment (JRE) • Enables programs to execute on a host platform. • The Java runtime can work with a security manager to determine which operations a program can perform on the host platform.
  • 22. Types of Java Programs
  • 23. Program types • Java has four program types, although the differences among them are shallow. • The two most commonly used types are: – Application – Applet
  • 24. Java Application • Standalone program in the sense of requiring only the JVM to execute. • Does not require a host program such as a browser. • It’s main method is used as its entry point • Has to have a method with the following signature… public static void main(String args[ ])
  • 25. Java Applet • Small program typically downloaded from a server to a client machine. • JVM is built into the web browser • A Web browser acts as the host program for an applet. • An applet is typically launched through an HTML or equivalent document. • They are imbedded into HTML with the <applet> tag
  • 26. Java Applet (cont’d) • An applet typically operates under a strict security manager, which imposes sandbox security. • This prevents an applet from performing potentially dangerous operations such as reading from or writing to the local disk. • An applet’s class descends from the standard Applet or JApplet class.
  • 28. UML (part 1) • Unified Modeling Language – Meant to facilitate the design, development, deployment and maintenance of software systems. – Standardized in 1997 by the Object Management Group. – Combines methods from Object Modeling Technique (OMT) and Object-Oriented Software Engineering (OOSE). • UML diagrams can serve as high-level design specifications for software systems and, in this role, can guide code development.
  • 29. UML (part 2) • Basic UML vocabulary consists of – Things: Entities to be modeled, whether concrete or abstract. – Relationships: Connections among things. – Diagrams: graphical depictions of things and their relationships • We will discuss the types of UML diagrams as they come up.
  • 31. Structured Approach • Subset of procedural programming • Enforces a logical structure on the program being written to make it more efficient and easier to understand and modify. • Frequently employs a top-down design model – Developers map out the overall program structure into separate subsections. – A defined function or set of similar functions is coded in a separate module or submodule, which means that code can be loaded into memory more efficiently and that modules can be reused in other programs. – After a module has been tested individually, it is then integrated with other modules into the overall program structure.
  • 32. Object Oriented Approach • Provides a more natural and intuitive way to view the design process • Modeling software components just as we describe real-world objects – By their attributes, behaviors, and the messages they pass between each other • Objects – Reusable software components – Building blocks
  • 33. Benefits of OO • Quality – Small, small-contained manageable objects reduces the complexity of the system development – Less error prone • Productivity – More reuse • Flexibility – Can add or modify objects while minimizing the impact on the rest of the system – This is achieved via encapsulation
  • 34. Java and OO • Just because it’s written in Java, does not mean it is object-oriented • The standard Java libraries are not always good examples of OO programming. • Compromise between practical and efficiency considerations and truly good OO design
  • 35. What is an object- oriented system? • Has the following 6 properties: – Abstraction with objects – Encapsulated classes – Communication via messages – Object lifetime – Class hierarchies – Polymorphism
  • 36. Abstraction • A model of a real-world object or concept • Usually a simplified representation of a complex situation • Designing objects can be difficult • Have to decide on what attributes and behaviors are necessary • Address book example
  • 37. Encapsulated Classes • Remember abstract data types? • The process of hiding all the internal details of an object from the outside world. • In Java, this is having definitions for attributes and methods inside a class definition. • State is changed using methods in the class.
  • 38. Communication via Messages • Messages are how objects communicate with each other. • Follows client/server model. • Objects can send and receive messages. • Send = invoke a class method • Receive = have your method called by another object • Can also be invoked by the language runtime system or the operating system.
  • 39. Class Hierarchies • An ordering of classes. • This includes: – Association – Aggregation – Composition – Inheritance
  • 40. Association • A relationship between two classes. • Indicates how the objects of the classes relate to each other. • The relationship has some sort of name (verb). • Multiplicity specifies the number of objects that can participate in the association. Teacher Student teaches learns from * *
  • 41. Aggregation • has-a relationship • The aggregate object includes other objects • The included objects are considered to be a part-of the aggregate Car Dealership Car *
  • 42. Composition • Special case of aggregation • The whole cannot exist without the aggregate object(s) Car Engine 1
  • 43. Inheritance • is-a relationship • A mechanism that allows one class (the subclass) to share the attributes and behaviors of another class (the superclass). • A subclass is a specialization of the generalized superclass. • The derived class may override or provide an alternate definition for an attribute or method of the superclass, or use the default behaviors of the superclass.
  • 44. Inheritance Example Animal Warm Blooded Cold Blooded Mammal Bird Flying Non-Flying Parrot Penguin
  • 45. More on Inheritance • In Java, all classes are implicitly derived from the Object class, making Object the root class. • Single inheritance is when a class is directly derived from only 1 parent class. • Multiple inheritance is when a class is derived from more than one parent classes. • Unlike other languages, Java does not support multiple inheritance.
  • 46. Multiple Inheritance is BAD • Repeated Inheritance – a base can occur more than once in the parents list. • Ambiguity – a feature or method occurs more than once among the parents… which one is called? • This brings us to interfaces, the alternative to multiple inheritance.
  • 47. Interfaces • In psychology, a “mask” that allows a person to behave in a manner appropriate to a particular role. • In Java, the specification of methods that a class using the interface must implement, or provide code for. • Defines the is-a relationship between classes. • You will NOT find code in an interface.
  • 48. Polymorphism • Dynamic binding of an object to the appropriate method. (The actual binding of a call to a specific method is delayed until runtime.) GoodStudent BadStudent Student +takeExam() : int +takeExam() : int +takeExam() : int
  • 49. Basics of a Java Application
  • 50. Jumping into Java Programming • To create and run a Java program – Create a text file with a .java extension for the source code. For instance, source code for a first program might reside in the file Hi.java. – Write the source code. – Compile the source code into binary code. – Execute the binary code.
  • 51. Compiling • Suppose that the Java source code resides in the plain text file Hi.java. Using the $ as the system prompt, the source code is compiled with the command $ javac Hi.java The javac utility comes with the Java SDK.
  • 52. Compiling • Compiling a source file such as Hi.java generates one or more binary files with a .class extension, e.g., Hi.class. – If fatal errors occur during compilation, the process ends with appropriate error messages. – If nonfatal errors occur during compilation, the process continues with appropriate warnings.
  • 53. Executing • Once the program has been compiled, it can executed with the java utility. For instance, if the file Hi.class contains a compiled standalone program, the command $ java Hi executes the program.
  • 54. Compiling versus executing • Note that the .java extension is included in the compilation but that the .class extension is not included in the execution. Note the contrast: $ javac Hi.java $ java Hi
  • 55. A first program (part 1) // This program prints // Hello, world! // to the standard output. class Hi { public static void main( String[ ] a ){ System.out.println( “Hello, world!” ); } }
  • 56. A first program (part 2) • The source code for the Hi program resides in a text file with a .java extension, Hi.java. • Every Java program requires at least one source code class, as the Hi example illustrates. • A public class must reside in a .java file of the same name. • There can be at most one public class definition in a file.
  • 57. A first program (part 3) • The Hi program is a Java application rather than, for instance, an applet, or a servlet, which are other program types. • A Java application is the most general and basic program type. • An application must have a public static method called main, that takes in an array of Strings: public static void main(String args[]) {}
  • 58. A first program (part 4) • The syntax String[] a indicates that a (for “array”) refers to an array. In general, the square brackets indicate an array. In this case, the array can hold references to Strings, which are any command-line arguments passed to the program.
  • 59. A first program (part 5) • In the print statement System.out.println( “Hello, world!” ); – System is a standard Java class that represents the system on which the program executes. – out is a static field in the System class and its type is PrintStream, another standard Java class. – println is a PrintStream method.
  • 60. A first program (part 6) • In the Hi program’s print statement, the parts System, out, and println are separated by the member operator, the period. • The print statement is terminated by a semicolon. – In Java, the semicolon terminates statements.