COMP2021: Object-Oriented Programming
Introduction to OOP and Java
Learning Objectives
❖ After the lecture, students should be able to
➢ understand the value of software to computer systems;
➢ understand different types of software systems;
➢ understand software engineering goals and principles;
➢ appreciate object-oriented design and programming; and ➢ master the basic steps in Java programming.
❖ Computer and Software
❖ Software Engineering Principles ❖ Object-Oriented Programming
❖ The Java Programming Language
Computer Organization
❖ Every computer is roughly organized into 4 parts
➢ Input devices ➢ Output devices
Input Devices
Output Devices
Control Unit
❖ The fetch/execute cycle is the steps the CPU takes to execute instructions
❖ Performing the action specified by an instruction is known as “executing the instruction”
❖ The program counter (PC) holds the memory address of the next instruction
Fetch the instruction to which the PC points
Increment the PC
Execute the fetched instruction
❖ System software
➢ Programs that support the execution and development of
other programs
➢ Two major types
▪ Operating systems ▪ Translation systems
❖ Application software
➢ Programs designed to perform specific tasks that are transparent to the user
Operating System
❖ Controls and manages the computing resources
❖ Important services that an operating system provides
➢ File system: Folders, Files
➢ Commands that allow for manipulation of the file system: delete, copy
➢ Ability to perform input and output on a variety of devices
➢ Management of the running systems
❖ Examples
Translation System
❖ Set of programs used to develop software
➢ A key component of a translation system is a translator
❖ Types of translators
➢ Compiler: Converts code from one language to another ➢ Interpreter: Reads and executes code
➢ Linker: Combines resources
❖ Examples
Application Software
❖ Application software is the software that has made using computers indispensable and popular
❖ Common application software
➢ Word processors
➢ Desktop publishing programs ➢ Spreadsheets
➢ Presentation managers ➢ Drawing programs ➢…
❖ Learning how to develop application software is our focus
Software Development
❖ Major activities.
➢ Compiling/Linking
➢ Loading and executing
➢ Viewing the behavior of the program
Compile/Link
Software Development Cycle
Integrated Development Environments (IDEs)
❖ Integrated Development Environments or IDEs
➢ Translation systems that support the entire software
development cycle
❖ Combine all the capabilities that a programmer would want handy while developing software
➢ Editor, Compiler, Linker, Loader, Debugger, Viewer, …
Engineering Software
❖ Software engineering
➢ Area of computer science concerned with building large software
➢ Challenge: Tremendous advances in hardware have not been accompanied by comparable advances in software
13% of projects are on time, 64% canceled!
18% of time spent on coding, 35% debugging!
100% 90% 80% 70% 60% 50% 40% 30% 20% 10% 0%
100% 90% 80% 70% 60% 50% 40% 30% 20% 10% 0%
10K 100K 1M 10M
1K 10K 100K 1M
Lines of Code
Support & Management Finding & Removing Defects
Lines of Code
Documentation Coding
, Estimating Software Costs, pg. 140
, Patterns of Software Systems Failure & Success
% of Projects
% of Effort by Task
The Four Constraints of Software Projects
❖ Every software project is constrained by its
➢ Scope goals: What is the project trying to accomplish? ➢ Time goals: How long should it take to complete?
➢ Cost goals: What should it cost?
➢ Quality goals: At what defect level?
Software Engineering Goals
❖ Reliability: An unreliable life-critical system can be fatal
❖ Understandability: Future development becomes very difficult if software is hard to understand
❖ Adaptability: System that is adaptive is easier to alter and expand
❖ Reusability: Improves reliability and maintainability, and reduces development costs
❖ Cost Effectiveness: Cost to develop and maintain should not exceed profit
Software Engineering Principles
❖ Abstraction: Extract the relevant properties of an object while
ignoring inessential details
❖ Encapsulation: Breaking down an object into parts, hiding and protecting its essential information, and supplying an interface to modify the information in a controlled and useful manner
❖ Modularity: Dividing an object into smaller pieces or modules such that the object is easier to understand and manipulate
❖ Hierarchy: Ranking or ordering of objects based on some relationship between them
Abstraction
❖ Process of extracting only the relevant properties of an object ➢ Extracted properties define a view of the object
➢ Car dealer views a car from selling features standpoint
▪ E.g., price, length of warranty, color, optional equipment
➢ Mechanic views a car from systems maintenance standpoint
▪ E.g., type of oil, size of the oil filter, type of spark plugs
Encapsulation
❖ Breaking down an object into parts, hiding and protecting its essential information, and supplying an interface to modify the information in a controlled and useful manner
➢ By hiding the information its representation and content can be changed without affecting other parts of the system
❖ Example – car radio
➢ Interface consists of the controls and types of connectors for
connecting the radio to the car
➢ The details of how it works is hidden
➢ To install and use a radio, we do not need to know anything about the radio’s electrical system
Modularity
❖ Dividing an object into smaller pieces or modules such that the modules hold useful information and the object is easier to understand and manipulate
➢ Most complex systems are modular
❖ Example – Automobile can be decomposed into subsystems
➢ Cooling system ▪ Radiator
▪ Thermostat ▪ Water pump
➢ Ignition system ▪ Battery
▪ Spark plugs
❖ Ranking or ordering of objects based on some relationship between them
➢ Hierarchies help us understand complex systems
➢ For complex systems, a useful way of ordering similar abstractions is from most general to least general
▪ Scientists use this technique to identify and classify species
▪ Hierarchical ordering based on natural relationships is called a
❖ Example – a company hierarchy helps employees understand the company and their positions within it
Object-Oriented Design and Programming
❖ OO design and programming is a methodology that supports good software engineering
➢ It promotes thinking about software in a way that models the way we think and interact with the real world
❖ Example – watching television ➢ The remote is a physical object
with properties
▪ Has weight and size
▪ Can send messages to the television
➢ The television is also a physical object with various properties
❖ An object is almost anything that can be attributed with the following characteristics
➢ Properties
➢ The ability to act when receiving a message ▪ Directive to perform an action
▪ Request to change one of its properties
What Are Objects?
Objects are like people. They’re living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction, like we’re doing right here.
Here’s an example: If I’m your laundry object, you can give me your dirty clothes and send me a message that says, “Can you get my clothes laundered, please.” I happen to know where the best laundry place in San Francisco is. And I speak English, and I have dollars in my pockets. So I go out and hail a taxicab and tell the driver to take me to this place in San Francisco. I go get your clothes laundered, I jump back in the cab, I get back here. I give you your clean clothes and say, “Here are your clean clothes.”
You have no idea how I did that. You have no knowledge of the laundry place. Maybe you speak French, and you can’t even hail a taxi. You can’t pay for one, you don’t have dollars in your pocket. Yet I knew how to do all of that. And you didn’t have to know any of it. All that complexity was hidden inside of me, and we were able to interact at a very high level of abstraction. That’s what objects are. They encapsulate complexity, and the interfaces to that complexity are high level.
— , A 1994 Rolling Stone interview
Procedure Oriented vs. Object Oriented
Procedure Oriented
❖ List of instructions to tell the computer what to do step by step
➢ Procedure is less stable
➢ C, Fortran, COBOL, Pascal, …
Object Oriented
❖ Group of objects who interact with each other to carry out all computations
➢ Structure is more stable ➢ Java, C#, Smalltalk, …
Local data
Global data
Function-1
Local data
Function-2
Local data
Function-3
Local data
Function-4
Local data
Procedure Oriented (C) – Diablo
typedef struct Hero{ char* name;
int strength;
int attack;
int defense; …
void initHero(Hero* hero); void increaseHeroStrength(…); …
Hero heroA, heroB;
initHero(&heroA);
increaseHeroStrength(&heroA, 1);
Procedure Oriented (C) – Diablo (Cont’d)
❖ Two problems (among others) ➢ Using a hero
➢ Adding barbarians
heroA.strength = -1000;
increaseHeroStrength(&heroA, -1000);
typedef struct Barbarian{ char* name;
int strength;
int attack;
int defense;
bool isAngry; // new field …
void initBarbarian(Barbarian* barbarian); void increaseBarbarianStrength(…);
Object Oriented (Java) – Diablo
class Hero{
private String name; private int strength; …
public Hero(…){…}
public void increaseStrength(…){…}; }
class Barbarian extends Hero{ private boolean isAngry;
Hero h = new Hero(…); h.increaseStrength(…); Barbarian b = new Barbarian(…); b.increaseStrength(…);
Characteristics of Java
❖ Simple and Familiar ❖ Object-Oriented
❖ Distributed
❖ Interpreted
❖ Robust ❖ Secure
❖ Architecture-Neutral ❖ Portable
❖ Performance
❖ Multithreaded
❖ Dynamic and Extensible
Java Editions
❖ Java Standard Edition (Java SE) can be used to develop
client-side standalone applications or applets.
❖ Java Enterprise Edition (Java EE) can be used to develop server-side applications such as Java Servlets, Java Server Pages, and Java Server Faces.
❖ Java Micro Edition (Java ME) can be used to develop applications for mobile devices such as smart watches.
❖ We uses Java SE to introduce Java programming.
A Simple Java Program
// This program prints “Welcome to Java!”
public class Welcome {
public static void main(String[] args) {
System.out.println(“Welcome to Java!”); }
➢ A line of comment;
➢ A class named “Welcome”;
➢ A method called “main”;
➢ A statement that prints a line;
➢ Save the program to, e.g., d:\comp2021\Welcome.java
How to Develop A Java Program
Create/Modify Source Code
Saved on the disk
Source Code
public class Welcome {
public static void main(String[] args) {
System.out.println(“Welcome to Java!”);
Compile Source Code
javac Welcome.java
Method void main(java.lang.String[])
0 getstatic #2 …
3 ldc #3
Stored on the disk
If compile errors occur
Execute Bytecode
java Welcome
If runtime errors or incorrect result
Welcome to Java!
Write Once, Run Anywhere
Create a simple text file with .java extension then compile it using
javac Welcome.java
Source File (.java file)
Now the code is understandable by the JVM. Execute it using
java Welcome
Byte Code (.class file)
❖ Interpreter vs. Compiler
➢ https://youtu.be/_C5AHaS1mOA ➢ Just-in-time (JIT) compilation
❖ Software Engineering Goals and Principles ❖ Procedure Oriented vs. Object-Oriented
❖ Java and Write Once, Run Anywhere ❖ Compilation vs. Interpretation