OOP Chapter One

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Chapter One

Introduction to Object Oriented programming and Java

Contents;
 Types of programming paradigm
 Overview of OOP
 classes
 objects
 Overview of Java
 The JVM and Byte Code

Ch-1 Introduction to OOP and Java


Chapter one
Introduction to Object Oriented programming and Java

Types of programming paradigm

Programming paradigm refers to approaches or styles of programming.

Each paradigm has its own set of concepts, principles, and techniques for
designing and implementing programs.

The common programming paradigms:

Imperative Programming:
focuses on describing a sequence of steps or instructions to solve a problem.

It emphasizes changing the program's state through assignment statements


and control structures like loops and conditionals.

Examples of imperative programming languages include C, Pascal, and


Fortran.

Ch-1 Introduction to OOP and Java


Chapter one
Introduction to Object Oriented programming and Java

Object-Oriented Programming (OOP):


OOP organizes software design around objects that encapsulate data and
behavior.

It emphasizes concepts like classes, objects, inheritance, and polymorphism.

Popular OOP languages include Java, C++, and Python.

Functional Programming:
Treats computation as the evaluation of mathematical functions.

It emphasizes immutability and avoids changing program state or mutable data.

Languages like Haskell, Lisp, and Erlang are often associated with functional
programming.

Procedural Programming:
focuses on writing procedures or routines that operate on data.
It involves breaking down a program into smaller, reusable functions.

Languages like C, Pascal, andCh-1


Fortran are
Introduction procedural
to OOP and Java in nature.
Chapter one
Introduction to Object Oriented programming and Java

Declarative Programming:
focuses on describing the desired outcome or result, rather than specifying the
steps to achieve it.

In this paradigm, the programmer merely declares properties of the desired


result, but not how to compute it.

Prolog, which involves defining facts and rules using logical predicates, is an
example of programming language that uses declarative paradigm.

Declarative SQL queries for database management or declarative UI


frameworks like React are another examples.

Event-Driven Programming:
Focuses on responding to events or user actions.
The program typically waits for events to occur and triggers associated event
handlers or callbacks.

commonly used in graphical user interfaces (GUIs) and game development.

Languages like JavaScript andCh-1


C# have toevent-driven
Introduction OOP and Java frameworks.
Chapter one
Introduction to Object Oriented programming and Java

Concurrent Programming:
deals with managing and coordinating multiple tasks or processes that can run
simultaneously.

It involves concepts like threads, locks, semaphores, and message passing.


Languages like Java, Go, and Erlang provide built-in support for concurrent
programming.

These paradigms are not mutually exclusive.

The choice of a programming paradigm depends on the problem domain,


language capabilities, and personal preferences.

Ch-1 Introduction to OOP and Java


Chapter one
Introduction to Object Oriented programming and Java

Overview of OOP
 Object-oriented programming (OOP) is a programming paradigm that
organizes code into objects.

 It focuses on modeling real-world entities as objects, allowing for modular,


reusable, and maintainable code.

The key concepts in OOP include:

 Encapsulation:
Encapsulation refers to bundling data and related behaviors (methods) together
into an object.
It hides the internal implementation details and provides a clean interface for
interacting with the object. Ch-1 Introduction to OOP and Java
Chapter one
Introduction to Object Oriented programming and Java

Overview of OOP

 Abstraction:
Abstraction focuses on defining essential characteristics and behaviors of an
object while hiding unnecessary details. It simplifies complex systems by
providing a high-level view.

 Inheritance:
Inheritance allows objects to inherit properties and behaviors from parent
classes.

It promotes code reuse and supports the creation of hierarchical relationships


between classes.

Ch-1 Introduction to OOP and Java


Chapter one
Introduction to Object Oriented programming and Java

Overview of OOP

 Polymorphism:
Polymorphism enables objects to take on different forms or behaviors based on
the context. It allows different objects to respond differently to the same method
call, promoting flexibility and extensibility.

 Class:
A class is a blueprint or template for creating objects. It defines the properties
(attributes) and behaviors (methods) that objects of that class will have.

 Object:
An object is an instance of a class. It represents a specific entity in the
program, with its own state (values of attributes) and behavior (methods).

Ch-1 Introduction to OOP and Java


Chapter one
Introduction to Object Oriented programming and Java

Overview of Java
Java is a full-fledged and powerful language that can be used in many ways.
It comes in three editions:

 Java Standard Edition (Java SE) to develop client-side applications.


The applications can run standalone or as applets running from a Web browser.

 Java Enterprise Edition (Java EE) to develop server-side applications, such


as Java servlets, JavaServer Pages (JSP), and JavaServer Faces (JSF).

 Java Micro Edition (Java ME) to develop applications for mobile devices,
such as cell phones.

Oracle releases each version with a Java Development Toolkit (JDK).

The JDK consists of a set of separate programs, each invoked from a


command line, for developing and testing Java programs.

Ch-1 Introduction to OOP and Java


Chapter one
Introduction to Object Oriented programming and Java

Instead of using the JDK, you can use a Java development tool (e.g.,
NetBeans, Eclipse, and TextPad)—software that provides an integrated
development environment (IDE) for developing Java programs quickly.

Bytecode and JVM


A Java compiler translates the Java source program into what is known as
bytecode

The Java compiler stores the generated bytecode in a file with the
extension .class.
The bytecode consists of the instructions for a “pseudo CPU.” (a CPU that
does not exist in reality)

The JVM provides the runtime environment for a Java executable (bytecode).

Ch-1 Introduction to OOP and Java


Chapter one
Introduction to Object Oriented programming and Java

It also provides a bytecode interpreter and a verifier that confirms the


bytecode’s validity before translating and running it on a real CPU.

A JVM is essentially a machine (as its name suggests) that is capable of


running a Java executable.

The JVM subjects the loaded classes to verification to ensure that they do not
contain any undefined instructions for the pseudo CPU.

If the bytecode of your application program contains an invalid instruction, the


JVM rejects its execution and unloads it from memory.

Ch-1 Introduction to OOP and Java


Chapter one
Introduction to Object Oriented programming and Java

Creating, Compiling, and Executing a Java Program


You save a Java program in a .java file and compile it into a .class file.
The .class file is executed by the Java Virtual Machine.

You have to create your program and compile it before it can be executed.

If your program has compile errors, you have to modify the program to fix
them, and then recompile it.

If your program has runtime errors or does not produce the correct result,
you have to modify the program, recompile it, and execute it again.

Note
The source file must end with the extension .java and must have the same
exact name as the public class name.

For example, the file for the source code in the following should be named
Welcome.java, since the public class name is Welcome.
Ch-1 Introduction to OOP and Java
Chapter one
Introduction to Object Oriented programming and Java

File name:
public class Welcome { Welcome.java
public static void main(String[] args) {
// Display message Welcome to Java! on the console
System.out.println("Welcome to Java!");
}
}

A Java compiler translates a Java source file into a Java bytecode file.
The following command compiles Welcome.java:

javac Welcome.java

Note
You must first install and configure the JDK before you can compile and run
programs.

If there aren’t any syntax errors, the compiler generates a bytecode file with
a .class extension.

Thus, the preceding command generates a file named Welcome.class


Ch-1 Introduction to OOP and Java
Chapter one
Introduction to Object Oriented programming and Java

The following command runs the bytecode for the above java code

java Welcome

Caution
 Do not use the extension .class in the command line when executing the
program. Use java ClassName to run the program.

 If you use java ClassName.class in the command line, the system will
attempt to fetch ClassName.class.class.

 If you execute a class file that does not exist, a NoClassDefFoundError will
occur.

 If you execute a class file that does not have a main method or you mistype
the main method (e.g., by typing Main instead of main), a
NoSuchMethodError will occur.

Ch-1 Introduction to OOP and Java


Chapter one
Introduction to Object Oriented programming and Java

The Java program-development process consists of repeatedly creating/modifying


Ch-1 Introduction to OOP and Java
source code, compiling, and executing programs.
Chapter one
Introduction to Object Oriented programming and Java

(a) Java source code is translated into bytecode. (b) Java bytecode can be executed
on any computer with a Java Virtual Machine.

Ch-1 Introduction to OOP and Java


Chapter one
Introduction to Object Oriented programming and Java

A Sample Java Program

Ch-1 Introduction to OOP and Java


Chapter one
Introduction to Object Oriented programming and Java

Sample Screen Output


Hello out there.
I will add two numbers for you.
Enter One whole numbers on a line:
12 30
The sum of those two numbers is
42

Exercise
 Define the terms class and object
 What is the Java source filename extension, and what is the Java bytecode
filename extension?
 What are the input and output of a Java compiler?
 What is the command to compile a Java program?
 What is the command to run a Java program?
 What is the JVM?
 Can Java run on any machine? What is needed to run Java on a computer?
 If a NoClassDefFoundError occurs when you run a program, what is the
cause of the error?
 If a NoSuchMethodError occurs when you run a program, what is the
cause of the error? Ch-1 Introduction to OOP and Java
Chapter One
Introduction to Object Oriented programming and Java

Java Keywords
The keywords of a programming language are the words that define the language,
have special meaning to the compiler, and cannot be used as identifiers.

The following displays all the Java keywords

Ch-1 Introduction to OOP and Java

You might also like