You are on page 1of 24

1

Lecture 1
Introduction to OOP
2

Outline
 A Computer Program

 Main aspects of Java

 Object-Oriented Programming

 Compiling Programs

 Differences between Java and C++

 Java Development Environments


3

What is a Computer Program?


• For a computer to be able to do anything
(multiply, play a song, run a word processor), it
must be given the instructions to do so.

• A program is a set of instructions written by


humans for computers to perform tasks.

• The instructions are written in programming


languages such as C, C++, Java, etc.
4

Recipe Analogy
Comparing a computer program to a food recipe
Food Recipe/Food preparation Computer Program
• a chef writes a set of • a programmer writes a set of
instructions called a recipe
instructions called a program
• the recipe requires specific
• the program requires specific
ingredients
inputs
• the cook follows the
• the computer follows the
instruction step-by-step
instructions step-by-step
• the food will vary
depending on the amount of • the output will vary
ingredients and the cook depending on the values of the
inputs and the computer
5

Main aspects of Java


 Today Java is by far the most popular
programming language in the industry and has
reached version 8.

 Java has the distinction of being the first and only


programming language that had a ten-minute
story on the National Public Radio.
6

Main aspects of Java …


• Simple • Portable
• Object Oriented • Interpreted
• Network –savvy • High Performance
• Robust • Multithreaded
• Secure • Dynamic
• Architecture Neutral
7

Explanations for the above aspects


• Simple:
▫ A system that can be programmed easily without a lot of
training.
▫ It omits rarely used, poorly understood, confusing
features
• Object-Oriented:
▫ Analogy: OO Carpenter Vs Non-OO Carpenter
▫ Focus : the chair being built Vs the tools used to make it
• Network – Savvy:
▫ Library routines for TCP/IP protocols like HTTP & FTP
▫ Java applications can open & access objects across the
Net via URLs.
▫ Uses RMI to communicate between distributed objects.
8


• Robust:
▫ Java Compiler performs early error checking
▫ Dynamic run-time checking
▫ Eliminates situation that are error prone.
• Secure:
▫ Allow the construction of virus-free, tamper-free
systems.
• Architecture Neutral:
▫ The compiled code is executable on many processors
given the presence of JVM
• Portable:
▫ An int is always a 32-bit integer
▫ Binary data is stored and transmitted in a fixed format
▫ Java code can be virtually run anywhere.
9

Object-Oriented Programming
 Java is an object-oriented programming
language

 For the rest of this lecture, we’ll be introduced


to the basic principles of object-oriented
programming.

 We won’t be using these principles


immediately, but they will become important
over the next few weeks.
10

OOP Concepts

• In object-oriented programming (OOP),


programs are organized into objects

• The properties of objects are determined by


their class
• Class blue print to create objects

• Objects act on each other by passing messages


11

Object
• Definition: An object is a software bundle
that has State and Behavior.
• State: what an object has?
• Behavior: what an object can do?

• Software Objects are often used to model


real-world objects.
• Example: dogs have states (name, color,
hungry, breed) and behaviors (bark, fetch,
and wag tail).
12

Object Examples
• Example 1: Dogs
▫ States: name, color, breed, and “is hungry?”
▫ Behaviors: bark, run, and wag tail

• Example 2: Cars
▫ States: color, model, speed, direction
▫ Behaviors: accelerate, turn, change gears
13

Class
• Definition: A class is a blueprint that defines the
states and the behaviors common to all objects of a
certain kind.
• In the real world, you often have many objects of
the same kind. For example, a guard dog, herding
dog, snoop dog . . .
• Even though all dogs have four legs, and bark, each
dog’s behavior is independent of other dogs.
• For example: Dog #1 is a black Poodle, Dog #2 is a
red Irish Setter
14

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

• Example: when you want your dog to gather a


herd of goats, you whistle and send him out.
15

Summary of OOP
• When writing an object-oriented program, we
define classes, which describe categories of
objects, and the states and behaviors that they
have in common.
• We then create objects which belong to classes,
and share the common features of their class.
• Objects interact with each other by passing
messages.
• You will be creating your own classes and objects
soon!
16

Compiling Programs
• Computers do not understand the languages (C+
+, Java, etc) that programs are written in.

• Programs must first be compiled (converted)


into machine code that the computer can run.

• A compiler is a program that translates a


programming language into machine code.
17

Multiple Compilers
• Because different operating systems (Windows, Macs,
Unix) require different machine code, you must compile
most programming languages separately for each platform.

program

compiler compiler

compiler

Unix
Win
MAC
18

Java Interpreter
• Java is a little different.
• Java compiler produces bytecode not
machine code.
• Bytecode can be run on any computer
with the Java interpreter installed. Win

r
rete
te rp
Java Program Java Bytecode In
MAC

compiler Interpreter

Inte
rpre
ter Unix
19

Advantages and Disadvantages of Java


Advantages:
• Java is platform independent. Once it's compiled, you can run the
bytecode on any machine with a Java interpreter. You do not have
to recompile for each platform.
• Java is safe. Certain common programming bugs and dangerous
operations are prevented by the language and compiler.
• Java standardizes many useful operations like managing network
connections and providing graphical user interfaces.

Disadvantages:
• Running bytecode through the interpreter is not as fast as running
machine code, which is specific to that platform.
• Because it is platform independent, it is difficult to use platform
specific features (e.g., Windows taskbar, quick launch) in Java.
• Java interpreter must be installed on the computer in order to run
Java programs.
20

Differences between Java and C++


• Java has no
▫ Pointers, references, structs, unions, typdefs, and
#define
▫ No need to manage/free(delete) memory
▫ No size of operator – primitive data types are fixed in
size
▫ No goto allowed even though the keyword is present
▫ In Java everything is signed while in C++ you have
signed and unsigned data types for short, int , long
21

Cont…
▫ No standalone methods allowed
▫ No operator overloading
▫ Primitive data types must be passed by value
▫ Boolean data type
▫ No multiple inheritance replaced with interface
▫ No scope operator(::)
▫ No global functions or global data
▫ Extensive use of the static keyword
▫ No destructors as in C++
22

Java Development Environments

The most popular Java IDEs


 NetBeans
 Eclipse

 You should be able to use both


 It is possible to develop Java apps without an IDE
(using text editors and command windows such as
Ant, Maven, Gradle)
23

Installing a Java Environment


To develop Java Programs, you need to install:
 Some thing called the Java Development Kit (JDK)
 And your preferred IDE (Eclipse, NetBeans, …)
 To run a software developed using Java, you need:
 Some thing called the Java Runtime Environment (JRE)
 And the actual software
24

What We Do Using Java?


• Stand Alone/Desktop Application
• Networking
• Web Development
• Mobile Application
• ATM Machine Program
• …

You might also like