Object Oriented Programming (OOP) in Java
Lect. #1
By: Dr. Mohammad Hashim
2 Agenda
First Lect. Second Lect.
• What is Java? • From C++ to Java
• History of Java • Paradigm-related differences
• Features of Java • Syntax of C++ vs syntax of Java
• Go Deeper in Java • Fast Transition from C++ to Java
• C++ vs Java • Input/output statements
• First Java Program • Control statements
• Variables, arrays, pointers, …
• Types of Java Applications
• Class Type
• Templates vs Generics
• Examples
WHAT IS JAVA? 3
Features
History
of Java
4 HISTORY
• James Gosling and Green Team
• In June 1991 they initiate this project at Sun Microsystems.
• Originally designed for small, embedded systems in electronic appliances like set-top
boxes.
• but it was too advanced technology for the digital cable television industry at that time.
• Oak
• The language was initially called “Greentalk”
• Then it is renamed to be “Oak”
• Java
• May 20, 1995
• HotJava
• The first Java-enabled Web browser, March 1997
• JDK Evolutions
• J2SE, J2ME, and J2EE
5 JAVA VERSION EVALUATION
2010
• JDK Alpha and Beta (1995) • Java SE 7 (28th July 2011)
• JDK 1.0 (23rd Jan 1996) • Java SE 8 (18th March 2014)
• JDK 1.1 (19th Feb 1997)
• Java SE 9 (21st Sep 2017)
• J2SE 1.2 (8th Dec 1998)
• Java SE 10 (20th March 2018)
• J2SE 1.3 (8th May 2000)
• Java SE 11()
• J2SE 1.4 (6th Feb 2002)
• J2SE 5.0 (30th Sep 2004)
• Java SE 6 (11th Dec 2006)
6 JDK EDITIONS
• Java Standard Edition (Java SE)
• J2SE can be used to develop client-side standalone applications or
applets (small web-based application).
• Java Enterprise Edition (Java EE)
• J2EE can be used to develop server-side applications such as Java
servlets and Java ServerPages.
• Java Micro Edition (Java ME).
• J2ME can be used to develop applications for mobile devices such
as cell phones.
7
8 FEATURES OF JAVA
1. Java is Pure object-oriented 7. Java is architecture-neutral
2. Java is simple 8. Java is dynamic
3. Java is secured 9. Java is interpreted
4. Java is platform independent
10. Java high performance
5. Java is robust
11. Java is multithreaded
6. Java is portable
12. Java is distributed
9 FEATURES OF JAVA - Java is Pure Object-Oriented
• Object-oriented programming (OOPs) is a methodology that simplifies
software development and maintenance by providing some rules.
• Java is a pure object-oriented programming language.
• Everything in Java is an object.
• Object-oriented means we organize our software as a combination of
different types of objects that incorporates both data and behavior.
10 FEATURES OF JAVA – Java is Simple
• Java syntax is based on C++ (so easier for programmers to learn it after C++).
• Java has removed many complicated and rarely-used features, for example, explicit
pointers, operator overloading, destructors, etc.
• There is no need to remove unreferenced objects because there is an Automatic
Garbage Collection in Java.
• …
11 FEATURES OF JAVA – Java is Platform Independent
• A platform is the hardware or software environment in which a program runs.
• Java is Write Once and Run Anywhere language (WORA).
• It is different from other languages like C, C++, etc. which are compiled into platform
specific machines.
• Java code is compiled by the compiler and converted into bytecode (.class file).
This bytecode is a platform-independent code because it can be run on multiple platforms.
• Java bytecode can be run on multiple platforms, for example, Windows, Linux, Sun Solaris,
Mac/OS, etc. using the Java Virtual Machine (JVM) or Java Runtime Environment (JRE).
12 INTERNAL DETAILS
• What happens at compile time?
Platform independent
13 FEATURES OF JAVA – Java is Secure
• With Java, we can develop virus-free systems.
• Java is secured because the following securities are provided by default:
• No explicit pointer
• Java Programs run inside a virtual machine sandbox
• Security Manager: It determines what resources a class can access such as reading and writing
to the local disk
• Some security can also be provided by an application developer
explicitly through SSL, JAAS, Cryptography, etc.
14 FEATURES OF JAVA – Java is Portable
• Java is portable because it facilitates you to carry the Java bytecode to any platform.
• It doesn't require any further implementation.
• The same as platform-independent.
15 FEATURES OF JAVA – Java is Architecture-neutral
• Java is architecture neutral because there are no implementation-dependent features.
• for example, the size of primitive types is fixed.
• In C programming, int data type occupies 2 bytes of memory for 32-bit architecture
and 4 bytes of memory for 64-bit architecture.
• However, in Java it occupies 4 bytes of memory for both 32 and 64-bit architectures.
16 FEATURES OF JAVA – Java is Multi-Threaded
• A thread is like a separate program, executing concurrently.
• We can write Java programs that deal with many tasks at once by defining multiple
threads.
• The main advantage of multi-threading is that it doesn't occupy memory for each
thread. It shares a common memory area.
• Threads are important for multi-media, Web applications, etc.
17 HOW JAVA CHANGED THE INTERNET
• The Internet helped Java to go fast to the forefront of programming, and
Java, in turn, had a profound effect on the Internet.
• In addition to simplify web programming in general, Java innovated a new
type of networked program called the applet that changed the way the
online world thought about content.
• Java also addressed some of the thorniest issues associated with the
Internet: portability and security.
GO DEEPER IN JAVA 18
19 C++ vs JAVA
• There are many differences and similarities between the C++ programming
language and Java.
• A list of top differences between C++ and Java are given below:
• Java is a pure OOP Language while C++ only supports OOP
• Java does not support header files like C++
• Java does not support the pre-processor statements. (#define, #if, …)
• Java uses the import keyword to include different classes and methods instead of #include
20 FIRST JAVA PROGRAM
• C++ Example • Java Example
#include <iostream> class Simple{
using namespace std; public static void main(String args[]){
int main() {
cout << "Hello C++ Programming"; System.out.println("Hello Java");
return 0; }
} Simple.java
main.cpp
21 REQUIREMENTS FOR RUNNING JAVA PROGRAMS
Write a Java program Compile a Java program Run a Java program
Simple editor (Notepad) Install the suitable JDK from Install the suitable JVM or JRE on
Special editors (JCreator, java.sun.com your machine
NetBeans, …)
Save the file with the same name
as the class with extension
“.java”
22 INTERNAL DETAILS
• What happens at runtime?
Platform dependent
23 INTERNAL DETAILS
• What happens if we have multiple classes in the same Java Source file?
24 TYPES OF JAVA APPLICATIONS
According to Sun, 3 billion devices run Java. . Some of them are as follows:
• Desktop Applications such as acrobat reader, media player, antivirus, etc.
• Web Applications such as irctc.co.in, javatpoint.com, etc.
• Enterprise Applications such as banking applications.
• Mobile
• Embedded System
• Smart Card
• Robotics
• Games, etc.
25 TYPES OF JAVA APPLICATIONS
• There are mainly 4 types of applications that can be created using Java
programming:
1) Standalone Application
2) Web Application
3) Enterprise Application
4) Mobile Application
26 TYPES OF JAVA APPLICATIONS
1) Standalone Application
• also known as desktop applications or window-based applications.
• are traditional software that we need to install on every machine.
• Examples of standalone application are Media player, antivirus, etc.
• AWT and Swing are used in Java for creating standalone applications.
27 TYPES OF JAVA APPLICATIONS
2) Web Application
• An application that runs on the server side and creates a dynamic page
• Currently, Servlet, JSP, Struts, Spring, JSF, etc. technologies are used for
creating web applications in Java.
• Part of the J2EE
28 TYPES OF JAVA APPLICATIONS
3) Enterprise Application
• An application that is distributed in nature, such as banking applications,
etc.
• It has advantages of the high-level security, load balancing, and clustering.
• In Java, EJB is used for creating enterprise applications.
• Part of the J2EE
29 TYPES OF JAVA APPLICATIONS
4) Mobile Application
• An application which is created for mobile devices.
• Currently, Android and Java ME are used for creating mobile applications
• Part of the J2ME.
30 OBJECT ORIENTED PROGRAMMING
31 TWO PARADIGMS
• All computer programs consist of two elements: code and data.
• A program can be conceptually organized around its code or around its
data. That is, some programs are written around “what is happening” and
others are written around “who is being affected.”
• These are the two paradigms that govern how a program is constructed.
• The first way is called the process-oriented model
• the second approach is called object-oriented programming,
32 PARADIGM OF PROCESS-ORIENTED
• The process-oriented model can be thought of as code acting on data.
• Procedural languages such as C employ this model to considerable
success.
• Problems with this approach appear as programs grow larger and more
complex.
33 PARADIGM OF OOP
• Object-oriented programming (OOP) is a programming paradigm based
on the concept of "objects", which may contain data (attributes) and code, in
the form of procedures (methods)
• In OOP, computer programs are designed by making them out of objects that
interact with one another.
• The main target of the OOP is the reusability of code
• Significant object-oriented languages include Java, C++, C#, Python, PHP
34 PARADIGM OF OOP
• Object-oriented programming
organizes a program around its
data (that is objects) and a set of
well-defined interfaces to that
data.
• By switching the controlling
entity to data, you can achieve
several organizational benefits.
35
THE A-PIE CONCEPTS