0% found this document useful (0 votes)
18 views79 pages

Oop Lecture 1

Object oriented programming using Java module 1

Uploaded by

kaneki1232001
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)
18 views79 pages

Oop Lecture 1

Object oriented programming using Java module 1

Uploaded by

kaneki1232001
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/ 79

CST455: OBJECT ORIENTED

CONCEPTS

Syam Sankar
Dept. of CSE

1
Lecture-1

2
Course Outcomes

3
Textbooks:

4
Syllabus: Module -1

5
Ex: C language
Developing a program using a set of modules

or functions

● Code acting on data

Need of OOP: When program become

larger and complex: easy-to- maintain,

bug-free, reusable programs

6
Object Oriented Programming (OOP)

Ex: C++, Java

OOP Program: Data controlling


access to code

Allows constructing a
program using a set
of objects and their interactions.

7
Data-Critical element of OOP

Methods- protecting from unintentional

modifications from other functions (or methods)

● Decompose a problem into number of

entities called objects and

then build data and functions around these

entities
8
OOP

● The data of an object can be accessed only by the methods associated with
that object.
● Methods of one object can access the methods of other objects
● Object- partitioned area of computer memory- that stores both data and
operations

9
Objects and Classes
Objects are basic runtime entities in an object oriented system.

a person, a place, a bank account etc.

Banking program: let’s take 2 objects:


Message
Customer Account

10
Representation of an Object

Person_1 Object
Name
Data
BasicPay

Salary()
Methods
Tax()

11
Objects and Classes

Class: Collection of objects of similar type


A class may be thought of as a ‘data type’ and an object as ‘variable’ of that data
type

12
13
14
Data Abstraction
● An essential element of object-oriented programming is abstraction.
● Act of representing essential features without including the background details.
● A powerful way to manage abstraction is through the use of hierarchical classifications.
● The data from a traditional process-oriented program can be transformed by abstraction
into its component objects.

● A sequence of process steps can become a collection of messages between these


objects.
● Thus, each of these objects describes its own unique behavior.
● We can treat these objects as concrete entities that respond to messages telling them to
do
something.

15
Data Abstraction

The best example of abstraction is a TV remote. The user only interacts with the
outer interface that is nothing but keys. The user only knows which key to press
for what function.

It can be implemented using abstract classes and interfaces.

16
The Three OOP Principles

1. Encapsulation
2. Inheritance
3. Polymorphism

17
Encapsulation
● Encapsulation is the mechanism that binds together code and the data it
manipulates, and keeps both safe from outside interference and misuse.

● One way to think about encapsulation is as a protective wrapper (class) that


prevents the code and data from being arbitrarily accessed by other code
defined outside the wrapper.
● It provides the mechanism which is known as data hiding.

18
Encapsulation
● In Java, the basis of encapsulation is the class.
● A class defines the structure and behavior (data and code) that will be
shared by a set of objects.
● Each object of a given class contains the structure and behavior defined by
the class.

● The data defined by the class are referred to as member variables or


instance variables.
● The code that operates on that data is referred to as member methods or just
methods.

19
Encapsulation

There are mechanisms for


hiding the complexity
of the implementation
inside the class:
Each method or variable
in a class may be
marked private
Public methods or public.
can be used to protect
private data 20
Inheritance
Inheritance is the process by which objects of one class acquires the properties of
objects of another class.

● It supports the concept of

hierarchical classification.

21
22
Inheritance
● Subclass and Superclass
● If a given class

encapsulates some

attributes, then

any subclass

will have the same

attributes plus any

that it adds as part of its

specialization

23
Inheritance
Provides the idea of reusability.

Adding additional features to an existing class without modifying it - deriving a new


class from existing class- the new class has will have the combined features of
both the classes.

24
Polymorphism
● meaning “many forms”.
● This means that it is possible to design a generic interface to a group of
related activities. This helps reduce complexity by allowing the same interface
to be used to specify a general class of action.
● Pressing "ENTER" button plays an different roles in different applications. This is
also kind of Polymorphism

25
Single function name is used in different
classes - Polymorphism
● Override

26
Dynamic Binding
It refers to the linking of a procedure call to the code to be executed in response to
the call.

Dynamic binding means that the code associated with a given procedure call is
not known until the time of the call at runtime.

It is associated with polymorphism and inheritance.

27
Message Communication
1. Creating classes
2. Creating objects
3. Establishing communication among objects

A message for an object is a request for execution of a procedure- message


triggers a method

Message passing involves: specifying the name of the object, name of the
method(message), and the information to be sent.

28
Message Communication

29
Message passing

30
Benefits of OOP
Self Study - Refer Textbook by Balaguruswamy

31
Java’s Magic: The Bytecode
● The output of a Java compiler is not executable code. Rather, it is bytecode.
Bytecode is a highly optimized set of instructions designed to be executed by
the Java run-time system, which is called the Java Virtual Machine (JVM)
● Bytecodes are not machine instructions
● JVM was designed as an interpreter for the bytecode.
● Interpreter generates the machine code that can be directly executed on the
machine that is running the java program.
● Portable programs
● Java can be considered both a compiled and an interpreted language

32
Java’s Magic: The Bytecode

● Java is a platform-independent language.


● It's because when you write Java code, it's ultimately written for JVM but not
your physical machine (computer).
● Since JVM executes the Java bytecode which is platform-independent, Java
is platform-independent.
33
JRE

JRE (Java Runtime Environment) is a software package

that provides Java class libraries,

Java Virtual Machine (JVM), and

other components that are required to run Java applications.

● JRE is the superset of JVM

34
JDK

JDK (Java Development Kit) is a software development kit required

to develop applications in Java.

When you download JDK, JRE is also downloaded with it.

In addition to JRE, JDK also contains a number of

development tools(compilers, JavaDoc, Java Debugger,

etc).
35
Process of building and running

java application program

36
A First Simple Program - Example.java

Compilations:
javac Example.java
The javac compiler creates a file called
Example.class that contains the bytecode version
of the program
37
A First Simple Program - Example.java

To run the program:


java Example

Output:
This is a simple Java program.
38
A Second Short Program

39
A Second Short Program

Output:

This is num: 100


The value of num * 2 is 200

40
The if Statement
if(condition) statement;

41
The if Statement

42
The if Statement

Output:

x is less than y

x now equal to y

x now greater than y

43
The for Loop

44
The for Loop
Output:

This is x: 0

This is x: 1

This is x: 2

………..

45
46
Demonstrate a block of code.
Output:

47
Lexical Issues
Whitespace- Free-form language- This means that you do not need to follow any
special indentation rules. Whitespace: space, tab or newline

Identifiers - They are used to name things, such as classes, variables, and
methods.

An identifier may be any descriptive sequence of uppercase and lowercase letters,


numbers, or the underscore and dollar-sign characters.

Java is case-sensitive: VALUE and Value are different.

48
Lexical Issues
Identifiers - Valid

Invalid identifier names include these:

49
Lexical Issues
Literals- A constant value in Java.

50
Lexical Issues
comments

51
Lexical Issues

Documentation comment- This type of comment is used to produce an HTML file


that documents your program. It used by javadoc tool of JDK.

52
Documentation

comment:

53
Lexical Issues
Separators:
The most commonly used
separator in Java is the semicolon.

54
Java Keywords- 50 keywords

55
The Java Class Libraries
built-in class libraries

built-in methods

56
The Java Class Libraries
built-in packages (in JAR files of JDK)

57
Data Types, Variables, and Arrays
The Primitive Types: Eight primitive types of data: byte, short, int, long, char,
float, double, and boolean.

58
Java defines four integer types: byte, short, int, and long. All of these are signed,
positive and negative values.

Java does not support unsigned, positive-only integers.

byte

The smallest integer type is byte. This is a signed 8-bit type that has a range from
–128 to 127.

byte b, c;

59
short is a signed 16-bit type. It has a range from –32,768 to 32,767.

short s;

short t;

int: The most commonly used integer type is int. It is a signed 32-bit type that has
a range

from –2,147,483,648 to 2,147,483,647.

In addition to other uses, variables of type int are commonly employed to control
loops and to index arrays.
60
long is a signed 64-bit type and is useful for those occasions where an int type is
not large enough to hold the desired value.

61
Problem -1
Problem statement: Write a Java program to calculate and display the distance
light travels in a given number of days. The speed of light is approximately
186,000 miles per second. The program should use long variables to handle large
values, and the number of days should be specified as a long variable. The
program should then convert the number of days to seconds and compute the
distance light travels in that time period. Finally, the program should print the
number of days and the computed distance in miles.

62
Program & output:

63
Floating-Point Types: double and float
Floating-point numbers, also known as real numbers, are used when evaluating
expressions that require fractional precision.

When you need to maintain accuracy over many iterative calculations, or are
manipulating large-valued numbers, double is the best choice.
64
Problem-2
Problem Statement:

Write a Java program to calculate and display the area of a circle given its radius.
Use double variables to handle the values. The program should define the radius
and the value of pi, then compute the area using the formula for the area of a
circle: area=π×r^2. Finally, the program should print the computed area to the
console

65
Program & output

Area of circle is 366.29568

66
Characters
● In C/C++, char is 8 bits wide.
● in Java char is a 16-bit type. The range of a char is 0 to 65,536.
● Java uses Unicode to represent characters. Unicode defines a fully
international character set that can represent all of the characters found in all
human languages.

67
Program that demonstrates char variables:

ch1 and ch2: X Y

68
char variables behave like integers

ch1 contains X
ch1 is now Y

69
Booleans

Java has a primitive type, called boolean, for logical values. It can have only one
of two possible values, true or false.

70
Booleans

71
Booleans
Output:

72
Variables

73
Demonstrate block scope
● a block is begun with an opening curly brace and ended by a closing curly
brace. A block defines a scope.

● Thus, each time you start a new block, you are creating a new scope.

● A scope determines what objects are visible to other parts of your program. It
also determines the lifetime of those objects.

74
Scope

75
Scope
● Within a block, variables can be declared at any point, but are valid only after
they are declared.

● Thus, if you define a variable at the start of a method, it is available to all of


the code within that method.

● Conversely, if you declare a variable at the end of a block, it is effectively


useless, because no code will have access to it.

76
Scope
● Variables are created when their scope is entered, and destroyed when their
scope is left. This means that a variable will not hold its value once it has
gone out of scope.

● Therefore, variables declared within a method will not hold their values
between calls to that method.

● Also, a variable declared within a block will lose its value when the block is
left. Thus, the lifetime of a variable is confined to its scope.

77
Scope
Although blocks can be nested, you cannot declare a variable to have
the same name as one in an outer scope.

78
Arrays

79

You might also like