Oop Lecture 1
Oop Lecture 1
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
6
Object Oriented Programming (OOP)
Allows constructing a
program using a set
of objects and their interactions.
7
Data-Critical element of OOP
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.
10
Representation of an Object
Person_1 Object
Name
Data
BasicPay
Salary()
Methods
Tax()
11
Objects and Classes
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.
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.
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.
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.
19
Encapsulation
hierarchical classification.
21
22
Inheritance
● Subclass and Superclass
● If a given class
encapsulates some
attributes, then
any subclass
specialization
23
Inheritance
Provides the idea of reusability.
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.
27
Message Communication
1. Creating classes
2. Creating objects
3. Establishing communication among objects
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
34
JDK
etc).
35
Process of building and running
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
Output:
This is a simple Java program.
38
A Second Short Program
39
A Second Short Program
Output:
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
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.
48
Lexical Issues
Identifiers - Valid
49
Lexical Issues
Literals- A constant value in Java.
50
Lexical Issues
comments
51
Lexical Issues
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.
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
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
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:
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.
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