This document provides an introduction to object-oriented programming (OOP) and Java. It begins by explaining the differences between structured and object-oriented programming paradigms. Structured programming uses procedures that operate on separate data, while OOP focuses on creating objects that combine data and procedures. The document then discusses some disadvantages of structured programming and introduces key concepts of OOP like objects, classes, encapsulation, and inheritance. It provides examples of these concepts in Java and describes the Java programming environment, including the Java Development Kit and standard library. It also explains how to write, compile, and run a simple "Hello World" Java program.
2. Programming paradigm
Programming Paradigm is a way of conceptualizing what
it means to perform computation and how tasks to be
carried out are organized on a computer.
Two common approaches are Structured and Object
Oriented
Structured Programming
Problem solving would involve the analysis of processes in terms
of the procedural tasks carried out and the production of a system
whose representation is based on the procedural flow of the
processes.
Procedures typically operate on data items that are separate from
the procedures.
programmer is responsible for organizing everything in to logical
units of code/data
A procedural program is divided into functions, and (ideally, at
least) each function has a clearly defined purpose & a clearly
defined interface to the other functions in the program.
DaDU CoET Dept of Computer Science
2 October 12, 2022
3. Disadvantages of Structured Programming
1. Unrestricted Access
Functions have unrestricted access to global data.
2. Real-World Modeling
Unrelated functions and data, the basics of the
procedural paradigm, provide a poor model of the real
world.
3. Difficult of Creating New Data Types
Traditional languages are not extensible because they
will not let you create new data types.
DaDU CoET Dept of Computer Science
3 October 12, 2022
4. OOP Approach
Object-oriented programming is centered on creating
objects rather than procedures.
Objects are a melding of data and procedures that
manipulate that data.
Data in an object are known as attributes.
Procedures in an object are known as methods.
OOP allows us to decompose a problem into number
of entities called objects and then build data and
methods (functions) around these entities.
The data of an object can be accessed only by the
methods associated with the object (information
hiding)
Follows bottom-up approach in program design
DaDU CoET Dept of Computer Science
4 October 12, 2022
5. Features of OOP
Emphasis is on data rather than procedure.
Programs are divided into objects.
Data Structures are designed such that they
characterize the objects.
Methods that operate on the data of an object are tied
together in the data structure.
Data is hidden and can not be accessed directly by
external functions .
Objects may communicate with each other through
methods.
DaDU CoET Dept of Computer Science
5 October 12, 2022
6. Object-Oriented Programming
6
. Object
Attributes (data)
typically private to this object
Methods
(behaviors / procedures)
Other
objects
Programming
Interface
October 12, 2022
DaDU CoET Dept of Computer Science
7. Basic OOP Concepts
The following are the basic OOP concepts:
1. Objects
2. Classes
3. Data Abstraction
4. Data Encapsulation
5. Inheritance
6. Polymorphism
7. Dynamic Binding
8. Message Passing
DaDU CoET Dept of Computer Science
7 October 12, 2022
8. Basic OOP Concepts
1. Object
An object is an any real world entity which may represent
place, person, data item related to program.
An object is a variable/instance of class.
An object is a run-time entity.
2. Class
Is the template or blueprint that defines the
states/attributes and the behaviors/methods common to
all objects of a certain kind.
It is a collection of objects of similar type.
Classes are user-defined data types & behave like the
built-in types of programming language.
DaDU CoET Dept of Computer Science
8 October 12, 2022
9. Basic OOP Concepts
3. Data Abstraction
Abstraction means representing essential features without
including the background details or explanations
A classes can use the concept of Abstraction and are
defined as list of abstract data and functions to operate on
these data.
Since the classes use the concept of Data Abstraction,
they are known as Abstract Data Type(ADT).
4. Data Encapsulation
The wrapping up of data and methods into a single unit
(called class) is known as encapsulation.
This insulation of the data from direct access by the
program is called data hiding.
DaDU CoET Dept of Computer Science
9 October 12, 2022
10. Basic OOP Concepts
5. Inheritance
Is the process by which objects of one class acquire the
properties of objects of another class.
It provides the idea of reusability( reusing the code)
6. Polymorphism
In polymorphism, ‘Poly’ means many and ‘morph’ means forms,
i.e. many forms.
Is the ability to take more than one form. It allows a function
responding in different ways.
7. Dynamic Binding
Dynamic binding means that the code associated with a given
procedure call is not known until the time of the call at runtime.
Memory is allocated at runtime not at compile time.
DaDU CoET Dept of Computer Science
10 October 12, 2022
11. Basic OOP Concepts
8. Message Passing
The process of invoking an operation of an object is called
Message Passing.
In response to the given message, the respective method
or operation is called.
Message Passing involves specifying the name of the
object, the name of the function (message) and the
information to be sent.
employee.salary (name);
A message for an object is a request for the execution of a
procedure.
DaDU CoET Dept of Computer Science
11 October 12, 2022
12. Introduction to Java
Java is an Object Oriented Programming language developed by Sun
Microsystems in the year 1991(now owned by Oracle) led by James
Gosling
Initially it was named as “Oak” by James Gosling
In 1995, “Oak” is renamed to “Java” due to legal matters
James Gosling and his team members were consuming a lot of coffee
supplied from a place called “Java Island’.
Hence they fixed the name of the language as Java.
The symbol for Java language is cup and saucer.
Sun formally announced Java at Sun World conference in 1995.
On January 23rd 1996, JDK1.0 version was released.
Originally designed to program various devices (home
appliances).
Difficult task because appliances are controlled by a wide variety of
computer processors.
Team developed a two-step translation process.
DaDU CoET Dept of Computer Science
12 October 12, 2022
13. Features of Java (Java Buzzwords)
Simple: Learning and practicing Java is easy because of
resemblance with C and C++.
Object Oriented: Unlike C++, Java is purely OOP.
Distributed: Java is designed for use on network; it has
an extensive library which works in agreement with
TCP/IP.
Secure: Java is designed for use on Internet. Java
enables the construction of virus-free, tamper free
systems.
Robust (Strong/ Powerful): Java programs will not
crash because of its exception handling and its memory
management features.
Portable: Java does not have implementation dependent
aspects and it gives same result on any machine.
DaDU CoET Dept of Computer Science
13 October 12, 2022
14. Cont’d…
Interpreted: Java programs are compiled to generate the
byte code(.class file).
This byte code can be interpreted by the interpreter contained in
JVM.
Architectural Neutral Language: Java byte code is not
machine dependent, it can run on any machine with any
processor and with any OS.
High Performance: Along with interpreter there will be JIT
(Just In Time) compiler which enhances the speed of
execution.
Multithreaded: Executing different parts of a program
simultaneously is called multithreading.
This is an essential feature to design server side programs.
Dynamic: We can develop programs in Java which
dynamically change on Internet (e.g.: Applets).
DaDU CoET Dept of Computer Science
14 October 12, 2022
15. Java Environment
Java Environment includes
Development tools also called Java Development Kit (JDK)
and
java standard library (JSL) hundreds of classes and methods
also called Application Programming Interface (API).
JDK comes with a collection of tools that are used for
developing, debugging and running java programs:
javac (java compiler)
java (java interpreter)
javap (java disassembler)
javah (for C header files)
javadoc (for creating HTML documents)
jdb (Java debugger)
appletviewer (for viewing java applets )
DaDU CoET Dept of Computer Science
15 October 12, 2022
16. Java API (Java Standard Library)
It includes hundreds of classes and methods grouped into
several functional packages.
Most commonly used packages are:
Language Support Package: a collection of classes and
methods required for implementing basic features of java.
Utilities Package: a collection of classes to provide utility
functions such as date and time functions.
Input/output Package: a collection of classes required for
input/output manipulation.
Networking Package: a collection of classes for communicating
with other computers via Internet.
AWT Package (Abstract Window Tool kit package): contains
classes that implements platform-independent graphical user
interface.
Applet Package: includes set of classes that allows us to create
java applets.
DaDU CoET Dept of Computer Science
16 October 12, 2022
17. Types Of Java Programs
Java programs can be of two types:
Applications
Java applications are large programs that run by their own
Stand-alone programs that run without the aid of a web browser.
Has relaxed security model since the user runs the program
locally.
Applets
Small applications that require the use of a Java enabled web
browser to run.
Applets are great for creating dynamic and interactive web
applications
Has enhanced security model since the user merely goes to a
web page and the applet runs itself.
October 12, 2022
DaDU CoET Dept of Computer Science
17
18. Types of Java Program…
18 October 12, 2022
DaDU CoET Dept of Computer Science
19. Java program structure
DaDU CoET Dept of Computer Science
19
In the Java programming language:
A program is made up of one or more classes
A class contains one or more methods
A method contains program statements
These terms will be explored in detail throughout the
course
A Java application always contains a method called
main
October 12, 2022
20. Java Program Structure …
DaDU CoET Dept of Computer Science
public class MyProgram
{
}
// comments about the class
public static void main (String[] args)
{
}
// comments about the method
method header
method body
October 12, 2022
20
21. Tools needed to create java programs
There are many tools to do java programming, to the very
least you need to have
Sun Java Development Kit (JDK/ Recent version JDK 8)
Text editor such as Microsoft Notepad
Integrated Development Environment IDE for Java
NetBeans (from Sun/Oracle)
Eclipse (from IBM)
Borland JBuilder
BlueJ
JCreator
DrJ
IntelliJ
Android Studio and
Many others
DaDU CoET Dept of Computer Science
20 October 12, 2022
22. 1-22
The Compiler and the Java Virtual Machine
A programmer writes Java programming statements
for a program called source code
A text editor is used to edit and save a Java source code
file.
Source code files have a .java file extension.
A compiler is a program that translates source code
into an executable form.
A compiler is run using a source code file as input.
Syntax errors caught during compilation if exist.
Syntax errors are mistakes that violate the rules of the
programming language.
Compiler creates another file that holds the translated
instructions.
October 12,
2022
DaDU CoET Dept of Computer Science
23. The Compiler and the Java Virtual Machine
Most compilers translate source code into executable
files containing machine code.
The Java compiler translates a Java source file into a file
that contains byte code instructions.
Byte code files end with the .class file extension.
Byte code instructions are the machine language of the
Java Virtual Machine (JVM) and cannot be directly
executed directly by the CPU.
The JVM is a program that emulates a micro-processor.
The JVM executes instructions as they are read.
JVM is often called an interpreter.
Therefore, Java is both compiled and interpreted
language.
October 12, 2022
DaDU CoET Dept of Computer Science
24. Program Development Process
Text editor Source code
(.java)
Saves Java statements
Java compiler
Byte code
(.class)
Produces
Java
Virtual
Machine
Program
Execution
Results in
October 12, 2022
DaDU CoET Dept of Computer Science
24
25. Your First Java Program
Open your text-editor and type the following piece of
Java code exactly:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Save this file as HelloWorld.java (watch capitalization)
in the following directory:
c:java (you need to create the directory if
it doesn’t exist)
Class-name has to match file name
25 October 12, 2022
DaDU CoET Dept of Computer Science
26. Compiling and Running Your First Program
Open the command prompt in Windows
To run the program that you just wrote, type at the command prompt:
cd c:java
Your command prompt should now look like this:
c:java>
To compile the program that you wrote, you need to run the Java
Development Tool Kit Compiler as follows:
At the command prompt type:
c:java> javac HelloWorld.java
You have now created your first compiled Java program named
HelloWorld.class
To run your first program, type the following at the command prompt:
c:java>java HelloWorld
Although the file name includes the .class extension , this part of the name must be left
off when running the program with the Java interpreter.
DaDU CoET Dept of Computer Science
26 October 12, 2022
27. Creating, Compiling, and
Running Programs
DaDU CoET Dept of Computer Science
27
Source Code
Create/Modify Source Code
Compile Source Code
i.e., javac Welcome.java
Bytecode
Run Byteode
i.e., java Welcome
Result
If compilation errors
If runtime errors or incorrect result
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
…
Method Welcome()
0 aload_0
…
Method void main(java.lang.String[])
0 getstatic #2 …
3 ldc #3 <String "Welcome to
Java!">
5 invokevirtual #4 …
8 return
Saved on the disk
stored on the disk
Source code (developed by the programmer)
Byte code (generated by the compiler for JVM
to read and interpret, not for you to understand)
October 12, 2022
28. Some questions
DaDU CoET Dept of Computer Science
28
Java compiles into an intermediate language
called byte-code. What is byte-code?
What is a syntax error?
October 12, 2022