0% found this document useful (0 votes)
69 views29 pages

OOP Chapter 1

This document provides an introduction to object-oriented programming (OOP) and the Java programming language. It discusses the structured and OOP paradigms, key concepts of OOP like objects, classes, encapsulation, inheritance and polymorphism. It also describes the Java environment, features of Java like portability and security, and important Java API packages. The document serves as a high-level overview of moving from structured to OOP and the basics of the Java language.

Uploaded by

Abdex Aliyi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views29 pages

OOP Chapter 1

This document provides an introduction to object-oriented programming (OOP) and the Java programming language. It discusses the structured and OOP paradigms, key concepts of OOP like objects, classes, encapsulation, inheritance and polymorphism. It also describes the Java environment, features of Java like portability and security, and important Java API packages. The document serves as a high-level overview of moving from structured to OOP and the basics of the Java language.

Uploaded by

Abdex Aliyi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Chapter One – Introduction to OOP

By Abdu A.
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.

2 DaDU CoET Dept of Computer Science 10/7/22


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.

3 DaDU CoET Dept of Computer Science 10/7/22


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
4 DaDU CoET Dept of Computer Science 10/7/22
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.

5 DaDU CoET Dept of Computer Science 10/7/22


Object-Oriented Programming
. Object
Attributes (data)
Programming typically private to this object
Interface

Other
objects

Methods
(behaviors / procedures)

6 DaDU CoET Dept of Computer Science 10/7/22


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

7 DaDU CoET Dept of Computer Science 10/7/22


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.

8 DaDU CoET Dept of Computer Science 10/7/22


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.

9 DaDU CoET Dept of Computer Science 10/7/22


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.

10 DaDU CoET Dept of Computer Science 10/7/22


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.

11 DaDU CoET Dept of Computer Science 10/7/22


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.
12 DaDU CoET Dept of Computer Science 10/7/22
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.
13 DaDU CoET Dept of Computer Science 10/7/22
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).
14 DaDU CoET Dept of Computer Science 10/7/22
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 )

15 DaDU CoET Dept of Computer Science 10/7/22


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.

16 DaDU CoET Dept of Computer Science 10/7/22


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.

17 DaDU CoET Dept of Computer Science 10/7/22


Types of Java Program…

18 DaDU CoET Dept of Computer Science 10/7/22


Java program structure
 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

19 DaDU CoET Dept of Computer Science 10/7/22


Java Program Structure …
// comments about the class
public class MyProgram
{

// comments about the method


public static void main (String[] args)
{
method header
method body
}

20 DaDU CoET Dept of Computer Science 10/7/22


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
20 DaDU CoET Dept of Computer Science 10/7/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.
1-22
DaDU CoET Dept of Computer Science 10/7/22
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.
DaDU CoET Dept of Computer Science 10/7/22
Program Development Process
Saves Java statements
Text editor Source code
(.java)

a d b y
Is re

Produces Byte code


Java compiler (.class)

e d by
nt er p re t
Is i
Java Results in Program
Virtual Execution
Machine

24 10/7/22
DaDU CoET Dept of Computer Science
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 DaDU CoET Dept of Computer Science 10/7/22


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.

26 DaDU CoET Dept of Computer Science 10/7/22


Creating, Compiling, and Running
Programs

Create/Modify Source Code

Source code (developed by the programmer)


Saved on the disk
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!"); Source Code
}
}

Compile Source Code


Byte code (generated by the compiler for JVM i.e., javac Welcome.java
to read and interpret, not for you to understand)

Method Welcome() If compilation errors
0 aload_0 stored on the disk

Bytecode
Method void main(java.lang.String[])
0 getstatic #2 …
3 ldc #3 <String "Welcome to
Java!">
5 invokevirtual #4 …
8 return Run Byteode
i.e., java Welcome

Result

If runtime errors or incorrect result

27 DaDU CoET Dept of Computer Science 10/7/22


Some questions
 Java compiles into an intermediate language called byte-
code. What is byte-code?
 What is a syntax error?

28 DaDU CoET Dept of Computer Science 10/7/22


n e
r O
t e
a p
f Ch
d o
E n

Next: Basics in Java Programming

You might also like