SlideShare a Scribd company logo
Object Oriented Programming (PCC-
CS503)
Bikash Choudhury
Assistant Professor
Department of Computer Science & Engineering
Ramkrishna Mahato Government Engineering College
Purulia
September 28, 2021
Bikash Choudhury (RKMGECP) OOPS September 28,2021 1/68
Content
s
1 Why Object Oriented Programming
Why Object-Oriented
programming? Why Java?
Literature of Java
2 Entire Execution Process in Java
How to Write and Execute Java
Program Input and Output in Java
Strings
Introduction to
OOPS Methods
Bikash Choudhury (RKMGECP) OOPS September 28,2021 2/68
Why Object-Oriented
Programming?
Object
1. Philosophically an object is anything that really exists in the world
and can be distinguished from others. Everything that we see
physically like, every human being, a book, a dog, a tree etc. are
the example of object.
2. In computer science, an object can be a variable, a data
structure, a function, or a method, and as such, is a value in
memory referenced by an identifier.
3. In the class-based and object-oriented programming paradigms,
object refers to a particular instance of a class, where the object
can be a combination of variables, functions, and data
structures.
4. In the relational model of database management, an object can be
a table or column, or an association between data and a database
entity (such as relating a person’s age to a specific person) [...
wiki]
Bikash Choudhury (RKMGECP) OOPS September 28,2021 3/68
What is Object Oriented
Programming?
Object Oriented Programming
1.Object Oriented programming (OOP) is a programming paradigm
that relies on the concept of classes and objects. It is used to
structure a software program into simple, reusable pieces of code
blueprints (usually called classes) which are used to create
individual instances of objects.
Bikash Choudhury (RKMGECP) OOPS September 28,2021 4/68
Why Object-Oriented
Programming?
1.OOP makes code organized, reusable and easy to maintain. It
follows the DRY (Dont Repeat Yourself) principle used by many
programmers to make efficient programs.
2.OOP also prevents unwanted access to data, or exposing
proprietary code through encapsulation and abstraction.
3.So how do programmers create Object Oriented programs? Well
the short answer is by making classes, and creating objects from
the classes. Classes form the blueprint for how data & behaviors
are structured.
4.Most programming languages are a mix of different ways of
writing computer programs. For example, Python allows for
computer programs to be written both in object-oriented
programming and in procedural programming. There are many
programming languages that allow you to write computer
programs in object-oriented programming. Some of these
programming languages are: C++, Java, Ruby, Perl, Emarald,
Sapphire, PHP, Python, etc. [...wiki]
Bikash Choudhury (RKMGECP) OOPS September 28,2021 5/68
Why
Java?
P e n t i u m P r o c e s s o r
W i n d o w s O / S
C e l e r o n P r o c e s s o r
U b u n t u O / S
S p a r k P r o c e s s o r
M a c O / S
1 . I n s t r u c t i o n ( a d d a , b )
2 . E x t e n s i o n ( T e s t . e x e )
1 . I n s t r u c t i o n ( a a d d b )
2 . E x t e n s i o n ( T es t . o u t )
Figure:Difficulties in M/C Dependent
Program
1 CC + + is not suitable for developing software forInternet.
2 What to do?
Bikash Choudhury (RKMGECP) OOPS September 28,2021 6/68
Why
Java?
S a m p l e . o u t /
S a m p l e . e x e
( M a c h i n e C o d e )
O u t p u t
S a m p l e . c l a s s
( B y t e C o d e )
M a c h i n e C o d e
( J V M )
S a m p l e . j a v a
( S o u r c e C o d e )
C o m p i l e r
S a m p l e . c
C o m p i l e r
E x e c u t e
O u t p u t
C o m p i l e r o f J V M
E x e c u t e
Figure:Difficulties in M/C Dependent
Program
Bikash Choudhury (RKMGECP) OOPS September 28,2021 7/68
Why
Java?
S a m p l e . o u t /
S a m p l e . e x e
( M a c h i n e C o d e )
O u t p u t
S a m p l e . c l a s s
( B y t e C o d e )
S a m p l e . j a v a
( S o u r c e C o d e )
C o m p i l e r
S a m p l e . c
C o m p i l e r
E x e c u t e
O u t p u t
C o m p i l e r o f J V M
M a c h i n e C o d e
( J V M )
E x e c u t e
M / C I n d e p e n d e n t
M / C D e p e n d e n t
Figure:Difficulties in M/C Dependent
Program
Bikash Choudhury (RKMGECP) OOPS September 28,2021 8/68
Literature of
Java
In 1990, Sun Microsystems Inc.(US) has conceived a project to
develop s/w consumer electronic devices that could be controlled
bya remote. This project was called Stealth Project but later its
name was changed toGreen Project.
In 1991, Bill Joy, James Gosling and their team has several
discussions regarding the project. Gosling thought CC + + could
be used to develop the project.
As CC + + is m/c dependent, Gosling starts to develop new
language for the aforesaid project. This language initially
calledOak. Later it was changed toJAVA.
Why the name JAVA?
In 1994,WebRunnera Java based web browser was developed
later renamed it asHotJava.
HotJava was the first browser, having the capabilities of
executing applets(programs designed to run dynamically in
internet).
Bikash Choudhury (RKMGECP) OOPS September 28,2021 9/68
Literature of
Java
Sun formally announcedJavaandHotJavaatSun
Worldconference in 1995. Later, Netscape, Microsoft
announced that they would support Java in their web
browser.
On January 23rd 1996,JDK 1.0version was released.
Now, more than 9 million developers use java and more than 4
billion electronics devices, including mobile phones and TVs, run
on Java.
Bikash Choudhury (RKMGECP) OOPS September 28,2021 10/68
JDK JRE
JVM
Figure:JDK & JRE
Bikash Choudhury (RKMGECP) OOPS September 28,2021 11/68
How to Write and Execute Java
Program
1 First installJREandJDK. Check whether JRE and JDK
installed properly or not!
Next, open any text editor, write the code and save the file
with ‘.java’extension.
Next, to compile the code, open the terminal and enter to the
folder where recently created ‘.java’ file reside. Next, writejavac
filename.java.
After compilation, compiler generates‘.class’file.
To run the ‘.class’ file write in the terminaljava ‘filename.class’.
2
3
4
5
Bikash Choudhury (RKMGECP) OOPS September 28,2021 12/68
JRE and JDK Installation
Procedure
1.open terminal and enter the below
command
Figure:Check JRE and JDK are installed or
not!
Bikash Choudhury (RKMGECP) OOPS September 28,2021 13/68
JRE and JDK Installation
Procedure
2.open terminal and enter the below
command
Figure:Install
JRE
Bikash Choudhury (RKMGECP) OOPS September 28,2021 14/68
JRE and JDK Installation
Procedure
3.open terminal and enter the below
command
Figure:Install
JRE
Bikash Choudhury (RKMGECP) OOPS September 28,2021 15/68
JRE and JDK Installation
Procedure
4.open terminal and enter the below
command
Figure:Install
JRE
Bikash Choudhury (RKMGECP) OOPS September 28,2021 16/68
JRE and JDK Installation
Procedure
5.open terminal and enter the below command to install the
JDK
Figure:Install
JDK
Bikash Choudhury (RKMGECP) OOPS September 28,2021 17/68
JRE and JDK Installation
Procedure
6.open terminal and enter the below
command
Figure:Install
JDK
Bikash Choudhury (RKMGECP) OOPS September 28,2021 18/68
How to Write and Execute Java
Program
1 First installJREandJDK. Check whether JRE and JDK
installed properly or not!
Next, open any text editor, write the code and save the file
with ‘.java’extension.
Next, to compile the code, open the terminal and enter to the
folder where recently created ‘.java’ file reside. Next, writejavac
filename.java.
After compilation, compiler generates‘.class’file.
To run the ‘.class’ file write in the terminaljava ‘filename.class’.
2
3
4
5
Bikash Choudhury (RKMGECP) OOPS September 28,2021 19/68
How to Write Java
Program
1 There are three types of comments available in Java.‘Single
line’, ‘multi-line’and‘Java-documentation’.
Single line //This is the single line
comment Multi-line /∗ This is multi-line
comments ∗/
Java-documentation / ∗∗ it provides description of every feature
in Java Program ∗/
This (Java-documentation) description is helpful in the creation
of a ‘.html’ file called API (Application Program Interface)
document. write
Please go through the first program (Hellop.java)in the
Assignment and execute it.
2
3
4
5
6
Bikash Choudhury (RKMGECP) OOPS September 28,2021 20/68
How to Write Java
Program
Impor
t 1
2
3
4
5
In CC + + we use‘# include < stdio.h >’.
What happens when we include the header
file?
Similar but more efficient mechanism is available in Java
called ‘import’. import java.lang.System;
Java Library → Packages → Classes → Methods
Please go through the first program (Hello.java)in the
Assignment1.0 and and execute it.
Bikash Choudhury (RKMGECP) OOPS September 28,2021 21/68
Assignme
nt
Assignme
nt1
2
3
4
5
6
7
8
9
10
What is the difference between an executable file and a ‘.class’
file? Why ‘.class’ file is called Byte code?
Why Java is suitable for Internet?
Why pointers are eliminated from Java?
What is the difference between a function and a
method? Is Java a purely object oriented language or
not? Justify.
Which part of the JVM will allocate the memory for a Java
program? Explain JDK,JRE,JVM and JIT.
Difference between include and
import. Difference between C++ and
Java.
Bikash Choudhury (RKMGECP) OOPS September 28,2021 22/68
JDK JRE
JVM
Figure:JDK JRE
JVM
Bikash Choudhury (RKMGECP) OOPS September 28,2021 23/68
JDK JRE
JVM
Figure:JDK JRE
JVM
Bikash Choudhury (RKMGECP) OOPS September 28,2021 24/68
JVM
JVM
1
2
3
4
5
First,‘.Java’program is compiled and converted
to‘.class’(Byte Code) by the Compiler of JDK/Java.
Second,‘.class’file is given to the JVM/JRE for the execution. In
JVM there is a module called‘Class Loader Sub System’which
performsa)loads ‘.class’ in to memoryb)Verifies all the byte code
instructions are proper or not? if suspicious instructions found
then rejects the execution. Otherwise allocates the necessary
memory to execute the program.
Method Area:stores class code, code of the variables and code of
the methods in the Java program.
Heap Area:here objects are created.
Java Stacks:Here java methods are executed. Especially
stores all the data and results used by the method.
Bikash Choudhury (RKMGECP) OOPS September 28,2021 25/68
JVM
JVM
1 PC Register:contain memory address of the instructions of
the methods.
2 Native method stacks:native methods ( CC + +) are executed
on
3
4
5
6
native method stacks.
Native method interface:To execute native method, native method
libraries are required. These header files are located and
connected to JVM by a program called Native Method Interface.
Execution Engine:containsinterpreterandJIT(Just in Time)
compiler. Both are responsible for converting Byte code to
machine code so that processor can understand and execute
accordingly.
Both interpreter and JIT compiler works simultaneously to
convert Byte code to m/c code.
Why both are needed and how both works simultaneously?
Bikash Choudhury (RKMGECP) OOPS September 28,2021 26/68
JVM
1 Consider a sample byte
code
1. print a = 1;
2. print b = 2;
3. for(i = 0;i < 10;i ++) {print a};
Interpreter starts execution from line1 it converts print a = 1;
into machine code and gives it to microprocessor. Assume
interpreter takes2 nanoseconds to do the aforesaid. Next,
processor takes it execute it and displayed the value of a = 1.
Again interpreter comes back to the memory and read the second
instruction print b = 2;. Interpreter takes another2 nanoseconds to
do the aforesaid. Next, processor takes it execute it and displayed
the value of b = 2.
2
3
Bikash Choudhury (RKMGECP) OOPS September 28,2021 27/68
JVM
1 Next, interpreter comes back to the memory and read the
third
instruction which is a looping statement {print a};. This should
be
done10times and this is known to the interpreter. So, first time it
converts the instruction to m/c code and gives it to the processor
and comes back to the memory for that it takes another2
nanoseconds.
Similarly10times the interpreter read the instruction and gives to
the
2
processor. So, total it takes10 ∗ 2 = 20 nanoseconds to read the
third instruction and gives it to the processor. Moreover, It is not
efficient with respect to time. This is the only reason JVM does
not allocate 3rd instruction to the interpreter instead it allocates
to the JIT compiler.
3 JIT compiler reads the print a; instruction and converts that into
m/c code. For this it takes2 nanoseconds. Next, JIT compiler
allots a block of memory and pushes this m/c code instruction into
that memory. For this it takes another2 nanoseconds. So, JIT
takes total of4 nanoseconds.
Bikash Choudhury (RKMGECP) OOPS September 28,2021 28/68
JVM
1 Now processor will fetch the instructions from the memory
and execute it 10times.
2 Observe that JIT compiler takes only4 nanoseconds to execute
the
3rd instruction, where as interpreter takes10 ∗ 2 = 20
nanoseconds for the same instruction. Hence, the JIT compiler
increases the speed of the execution.
3 Remember, first two instructions will not be allotted to the JIT
compiler by the JVM. (Interpreter takes2 nanoseconds to
convert byte code to m/c code whereas JIT takes4
nanoseconds).
After loading the‘.class’code into the class loader JVM identifies
which code is to be left to the interpreter and which one to the
JIT compiler so that the performance is better. The blocks of
code allocated fro the JIT compiler also called‘ hotspotsj.
4
Bikash Choudhury (RKMGECP) OOPS September 28,2021 29/68
Class and
Object
Properties:Variable
Actions: methods ()
Class: Dog
1. name
2. height
3. colour
4. age
1.running
2.barking
3.eating
1. Tom
2. 1 foot
3. brown
4. 5 years
Properties:Variable
Actions: methods ()
1. run ()
2. bark ()
3. eat ()
Object: Tom
Figure:Class and
Object
Bikash Choudhury (RKMGECP) OOPS September 28,2021 30/68
My First
Program
First Program
1.import java.lang.System; // java library—Package—Class–
Methods 2.import java.lang.String;
3.//import java.lang.*;
4.class Hello
5.{
6. public static void main(String
args[]) 7. {
8. System.out.print(”Hello
PGEC”); 9. }
10.}
Bikash Choudhury (RKMGECP) OOPS September 28,2021 31/68
My First
Program
1.public:Access specifier. If we do not made main() as public
then JVM does not allow to execute it.
2.static:methods which can be called and executed without
creating the objects.
3.void:return type of the main method().
4.main():is a method also starting point of the program. it accepts
a group of strings.
5.String args[]:string type array. args[] is the name of array and it is
of String type.
6.System: class name.print() belongs to PrintStream class. We
should call the print() by creating an object to PrintStream class.
But, as it is not possible to create an object of the PrintStream
class i.e., we use alternative approach.
7.out:static variable in System class (When we call ‘out’ internally
an object of PrintStream class is created).
8.print():used to display messages on the monitor.
Bikash Choudhury (RKMGECP) OOPS September 28,2021 32/68
Create a
Class
Class
1.class Person {
2.statements; // any no. of variables and methods we can
create 3.}
Object
1.class-name new-object-name = new class-name();
2. Hello Suman =new Person(); // Suman object is created
Bikash Choudhury (RKMGECP) OOPS September 28,2021 33/68
Variables and Data
types
Variables in Java
Data types in
Java
Bikash Choudhury (RKMGECP) OOPS September 28,2021 34/68
Operator and
Array
Operators in Java
Array in
Java
Bikash Choudhury (RKMGECP) OOPS September 28,2021 35/68
Input and Output in
Java
1 A stream is required to accept input from the keyboard.
Stream represents flow of data from one place to another. Like a
water pipe carries water from one place to another, similarly
Stream carries data from one place to another (from keyboard to
memory or from memory to file or printer).
Different types of Stream:a) Input Stream:which receives or
read data coming from some another place.b) Output
Stream:which send or write data to some other place.
All streams (input or output) are represented in’java.io’.
System.in: represents InputStream object, which by default
represents standard input device, i.e., keyboard.
System.out: represents InputStream object, which by
default represents standard output device, i.e., monitor.
System.err: represents InputStream object, which by
default represents monitor.
Difference betweenSystem.outandSystem.err?
2
3
4
5
6
7
8
Bikash Choudhury (RKMGECP) OOPS September 28,2021 36/68
Input in
Java
1 java.io.Console(Please open program Cmd.java from
Assignment1.5.) java.io.BufferedReader
java.util.Scanner
2
3
Bikash Choudhury (RKMGECP) OOPS September 28,2021 37/68
Input through
System.in
1 Keyboard (System.in) → input stream (InputStreamReader) →
input
stream (BufferedReader).
To accept data from keyboard (i.e., System.in) we need to connect
it to an input stream (InputStreamReader) as some input stream
is needed to read data. Connect the keyboard to an input stream
object and converts byte-oriented stream to character-oriented
stream.
InputStreamReader obj = new InputStreamReader(System.in);
To read data properly coming from InputStreamReader, we need
another stream called BufferedReader or BufferedReader class
can be used to read data line by line by readLine() method.
BufferedReader br = new BufferedReader(obj);
BufferedReader br= new BufferedReader(new
InputStreamReader(System.in)); // alternate
approach
2
3
4
5
6
7
Bikash Choudhury (RKMGECP) OOPS September 28,2021 38/68
Java BufferedReader class
methods
1 read(): used for reading a single character.
markSupported(): used to test the input stream support for the
mark and reset method.
readLine(): used for reading a line of text.
ready(): used to test whether the input stream is ready to be
read. skip(): used for skipping the characters.
reset(): reposition the stream at a position the mark method was
last called on this input stream.
mark(): is used for marking the present position in astream.
close(): closes the input stream and releases any of the
system resources associated with the stream.
2
3
4
5
6
7
8
Bikash Choudhury (RKMGECP) OOPS September 28,2021 39/68
Input through java.util.Scanner
Class
We can use Scanner class of java.util package to read input from the
keyboard or a text file. When the Scanner class receives input, it
breaks it into several pieces, called tokens. These token can be
retrieve from the Scanner object using the following methods:
1 nextLine(): used to read Strings from user.
nextBoolean():Reads a boolean value from the
user. nextByte(): Reads a byte value from the
user. nextDouble(): Reads a double value from
the user. nextFloat(): Reads a float value from
the user. nextInt(): Reads a int value from the
user. nextLong(): Reads a long value from the
user. nextShort(): Reads a short value from the
user.
Please open program Scan.java from
Assignment1.5.
2
3
4
5
6
7
8
9
Bikash Choudhury (RKMGECP) OOPS September 28,2021 40/68
Output through
System.out.printf()
To format and display the output printf() is available in PrintStream
class. This class works similar to printf() in c. We use
System.out.printf().
1 %s: string
%c: character
%d : decimalinteger
%f : float number
%o: octal number
%b, %B: boolean value
%x, %X: hexadecimal number
%e,%E : number in scientific notation
%n: new line character
Please open program Out.java from
Assignment1.5.
2
3
4
5
6
7
8
9
10
Bikash Choudhury (RKMGECP) OOPS September 28,2021 41/68
String
s
1 String represents a group of characters. Most of the data transmits
in internet is in the form of groups of characters. Such group of
characters are called Strings.
In CC + + languages, a String represents an array of characters,
2
where the last character 0 (called null character) represents the
end
of the Strings.
But this is not valid in java.In java String is an object of String
class. It is not an character array. In java we have character array
also, but Strings are treated differently because of their extensive
use in internet.
Is String is a datatype or class?String is a class in ‘java.lang’
package. But in java all classes are considered as data types.
So we can consider String as a data type also.
Can we call a class a datatype?Yes, a class also called a user
defined datatype.
3
4
5
Bikash Choudhury (RKMGECP) OOPS September 28,2021 42/68
Creating
Strings
There arethreedifferent ways to create string in
java.
1 We can create a String just by assigning a group of charters
to a String type variable.
String s; // Declare String type variable .
s=“RKMGEC”; // Assign a group of characters to it.
String s = ”RKMGEC” // Alternate
String s = new String(”RKMGEC”);
2
3 char arr[ ]={ ’R’,’K’,’M’,’G’,’E’,’C’};
String s = new String(arr);
String s = new String(arr,3,2); // Alternate: Starting from 3rd
character, a total 2 characters are copied in s.
Please open program StrCls1.java from Assignment1.6.
4
Bikash Choudhury (RKMGECP) OOPS September 28,2021 43/68
Methods in
Strings
1 hashCode():Returns the hash code of a string
charAt():Returns the character at the specified index (position)
concat():Appends a string to the end of another string
contains():Checks whether a string contains a sequence of
characters
contentEquals():Checks whether a string contains the exact same
sequence of characters of the specified CharSequence or
StringBuffer
endsWith():Checks whether a string ends with the
specified character(s)
equals():Compares two strings. Returns true if the strings are
equal, and false if not
equalsIgnoreCase():Compares two strings, ignoring
case considerations
format():Returns a formatted string using the specified
2
3
4
5
6
7
8
9
Bikash Choudhury (RKMGECP) OOPS September 28,2021 44/68
Methods in
Strings
1 getChars():Copies characters from a string to an array of chars
indexOf():Returns the position of the first found occurrence
of specified characters in a string
isEmpty():Checks whether a string is empty or
not length():Returns the length of a specified
string split():Splits a string into an array of
substrings
startsWith():Checks whether a string starts with specified
characters
substring():Extracts the characters from a string, beginning at a
specified start position, and through the specified number of
character
toLowerCase():Converts a string to lower case
letters toUpperCase():Converts a string to upper
2
3
4
5
6
7
8
9
10
Bikash Choudhury (RKMGECP) OOPS September 28,2021 45/68
String
Comparison
The relational operators like >,>=,<,<=,==,! = can not be used to
compare to strings.equals() and compareTo()methods are used to
compare the Strings.
1 When an object is created to the JVM it returns the memory
address of the object as a hexadecimal code (Object reference).
Every object will have unique object reference number.
String s1 = ”RKMGEC”
When JVM execute the above statement, it creates an object on
heap and stores “RKMGEC” in it. A reference number (‘2e45b3’)
is allotted for this object.
String s2 = new String(”RKMGEC”);
JVM creates another object and allot different reference
number (‘3b34c2’).
Please open program StrCls2.java & StrCls2.java from
Assignment1.6.
2
3
4
Bikash Choudhury (RKMGECP) OOPS September 28,2021 46/68
String
Comparison
== and equals()
String s1 = ”RKMGEC”
String s2 = new String(”RKMGEC”);
Bikash Choudhury (RKMGECP) OOPS September 28,2021 47/68
String Constant
Pool
String Constant Pool
String s1 = ”RKMGEC”
String s2 = ”RKMGEC”;
Bikash Choudhury (RKMGECP) OOPS September 28,2021 48/68
Immutability of
String
Immutable
Objects are broadly divided into -
a) Mutable (Contents can be modified) and
b)Immutable (contents can not be modified) objects. In java String
class objects are immutable.
String s1 = ”Computer”; String s2 = ”Science”;
s1=s1+s2; // ComputerScience (Please open program StrCls4.java
from
Assignment1.6.)
Bikash Choudhury (RKMGECP) OOPS September 28,2021 49/68
String Buffer &
Builder
1 To overcome the immutability issues in String we
useStringBuffer andStringBuilderclass.
Please open program StrCls5.java from Assignment1.6.
The main difference betweenStringBufferandStringBuilderis that
the StringBuffer class is by default synchronized and
StringBuilder is not.
When a programmer wants to use several threads she/he should
use StringBufferclass as it gives reliable results. If only one
thread is used,StringBuilderis preferred, as it improves the
execution.
2
3
4
Bikash Choudhury (RKMGECP) OOPS September 28,2021 50/68
Features of
OOPS
1 Class/Object
Encapsulatio
n
Abstraction
Inheritance
Polymorphis
m
2
3
4
5
Bikash Choudhury (RKMGECP) OOPS September 28,2021 51/68
Class/Objec
t
1 An object is anything thatreally exists in the world and can be
distinguished from others. E.g., a car, a table, a chair, a
person. A Class is amodel or blueprintfor creating objects.
[Please open program Cao0.java from Assignment2.0].
2
P r o p e r t i e s : V a r i a b l e
A c t i o n s : m e t h o d s ( )
1 . n a m e
2 . h e i g h t
3 . c o l o u r
4 . a g e
1 . r u n n i n g
2 . b a r k i n g
3 . e a t i n g
1 . T o m
2 . 1 f o o t
3 . b r o w n
4 . 5 y e a r s
P r o p e r t i e s : V a r i a b l e
A c t i o n s : m e t h o d s ( )
1 . r u n ( )
2 . b a r k ( )
3 . e a t ( )
C l a s s : D o g O b j e c t : T o m
Bikash Choudhury (RKMGECP) OOPS September 28,2021 52/68
Encapsulation and
Abstraction
Encapsulatio
n 1
2
Encapsulation is a mechanism where the data (variables) and
the code (methods) that act on the data will bind together.
Class consist of variables and methods tightly. Class is an
example for Encapsulation.
Abstractio
n 1
2
There may be a lot of data, a class contains and the user does
not need the entire data. The user requires only some part of
the available data. In this case, we can hide the unnecessary
data from the user and expose only that data that is of interest
to the user. This is called abstraction.
A car driver should know how to drive the car and does not require
to know how the engine is designed. [Please open program
Cao0.java from Assignment2.0]
Bikash Choudhury (RKMGECP) OOPS September 28,2021 53/68
Inheritance and
Polymorphism
Inheritanc
e 1
2
It creates new classes from existing classes,so that new
classes will acquire all the features of the existing classes is
called Inheritance.
Parents producing the children and children inhering the
qualities of the parents. [Please open program Cao0.java from
Assignment2.0]
Polymorphis
m1
2
The word ’Ploymorphism’ came from two Greek
words’poly’meaning ’many’and’morphos’meaning’forms’. Thus,
polymorphism
represents the ability to assume several different forms.
In programming, we can use a single variable to refer to objects
of different types and thus, using that variable we can call the
methods of different objects. [Please open program Cao0.java
from Assignment2.0]
Bikash Choudhury (RKMGECP) OOPS September 28,2021 54/68
Hash
Code
Hash
Code
1
2
Hash Code is a unique identification number allotted to the objects
by the JVM. This hash code number also called the reference
number which is created based on the location of the object in
memory, andis unique for all objects, excepts for String objects.
Please open program ’Cao1.java’ from Assignment2.0
Bikash Choudhury (RKMGECP) OOPS September 28,2021 55/68
Hash
Code
p t r t o h e a p p t r t o s p e c i a l s t r u c t u r e
i n s t a n c e v a r i a b l e 1
i n s t a n c e v a r i a b l e 2
i n s t a n c e v a r i a b l e 3
. . .
C o d e o f M e t h o d 1 ( )
H e a p M e m o r y
A l l S t a t i c V a r i a b l e s
M e t h o d A r e a
p t r t o s t a t i c v a r i b a l e s
p t r t o m e t h o d 1 ( )
p t r t o m e t h o d 2 ( )
...
S p e c i a l S t r u c t u r e
C o d e o f M e t h o d 2 ( )
O b j e c t R e f e r e n c e
Figure:Hash
Code
Bikash Choudhury (RKMGECP) OOPS September 28,2021 56/68
Hash
Code
6 0 3 7 4 2 8 1 4
. . .
N a m e :
A g e :
...
...
v o i d t a l k ( )
H e a p M e m o r y
M e t h o d A r e a
A b h i k
Figure:Hash Code of
Abhik
Bikash Choudhury (RKMGECP) OOPS September 28,2021 57/68
Access
Specifier/Modifier
Access Specifier
Is a keyword that specifies how to access the members
(variables,methods) of a class or class itself.
1
2
3
4
Private:’private’ members of a class are not accessible from
anywhere outside the class. They are accessible only within the
class by the methods of that class.
Public:The access level of a public modifier is everywhere. It can
be accessed from within the class, outside the class, within the
package and outside the package.
Protected:The access level of a protected modifier is within the
package and outside the package through child class. If we do not
make the child class, it cannot be accessed from outside the
package.
Default:The access level of a default modifier is only within the
package. It cannot be accessed from outside the package. If we
do not specify any access level, it will be the default.
Bikash Choudhury (RKMGECP) OOPS September 28,2021 58/68
Access
Specifier/Modifier
Figure:Access Specifier/Modifier
Please open program ’Cao2.java’ from
Assignment2.0
Bikash Choudhury (RKMGECP) OOPS September 28,2021 59/68
Constructo
r
Constructor
Constructor is a mechanism to initialize instance
variables.
1
2
3
4
5
6
Constructor’s name and class name should be same.
Constructor’s name should end with a pair of simple braces.
If a constructor does not have any parameters, it is called
Default constructor.
If a constructor has one or more parameters, it is called
Parameterized constructor.
A Constructor does not return any value, not even void.
A Constructor is automatically called and executed at the time
of creating an object.
While creating an object, if no parameter is passed to the object,
the default constructor is called and executed. If some values are
passed to the object then parameterized constructor is called.
Bikash Choudhury (RKMGECP) OOPS September 28,2021 60/68
Method
s
Methods
Represents a group of statements that perform a task
(calculation, processing of data or generating a report).
returndatatype methodname(parameter1,
parameter2,...) A method can not return more than
one value.
Method without parameters and without return type.
(P1) Method without parameters but with return type.
(P2) Method with two parameters and return type. (P3)
Instance method(): Methods which act on the instance variables
of the class. To call the instance methods, we should use the
form: objectname.methodname();
Bikash Choudhury (RKMGECP) OOPS September 28,2021 61/68
Static
Methods
Static Methods
Methods which do not act upon the instance variables of a class.
Static methods are declared as ’static’.
To call the static methods, we need not require to create object.
We can call a static method as: Classname.methodname();
JVM first execute the instance methods and then only it creates
the objects. Since the objects are not available at the time of
calling the static methods, the instance variables are also not
available.
Static method can access static variables. Static variables are
also declared as static. (P4,P5,P6)
Bikash Choudhury (RKMGECP) OOPS September 28,2021 62/68
Static
Methods
Difference between Instance variable and Static variable
An instance variable is a variable whose separate copy is
available to each object. A class/static variable is a variable
whose single copy in memory is shared by all objects. (P7,P8)
Instance variables are created in the objects on heap memory.
Class variables are stored on method area.
Bikash Choudhury (RKMGECP) OOPS September 28,2021 63/68
Static
Block
Static Block
is a block of statements declared as
’static’. static {
statements;
}
1
2
3
JVM executes a static block on a highest priority basis.
JVM first execute the static block then it looks for the main () in the
program. (Prog.M9.java)
Can we write a program without main()? (Prog. M10.java)
Bikash Choudhury (RKMGECP) OOPS September 28,2021 64/68
Static
Block
Static Block
is a block of statements declared as
’static’. static {
statements;
}
1
2
3
4
5
JVM executes a static block on a highest priority basis.
JVM first execute the static block then it looks for the main () in the
program. (Prog.M9.java)
Can we write a program without main()? (Prog. M10.java)
Yes. We can cheat the JVM before execution of main() method
we can terminate the program. (Prog. M11.java)
Moreover, it works until Java 1.6 version. Java 1.7 and newer
versions dont allow this because JVM checks the presence of the
main method before initializing the class.
Bikash Choudhury (RKMGECP) OOPS September 28,2021 64/68
Local
Variables
Local Variable
Local variable is a variable that is declared as locally inside a
method or constructor and is available only within that method or
a constructor.
Local variable can not be accessed outside the method or
constructor. (Prog. M15.java)
Sometimes a local variable has the same name as that of an
instance variable. This leads to problems regarding their
accessibility. (Prog. This1.java)
How to refer to the instance variable?
Bikash Choudhury (RKMGECP) OOPS September 28,2021 65/68
thi
s
this
’this’ is a keyword that refers to the object of the class where it
is used.
’this’ refers to the object of the present class.
All members (instance variable, constructor and methods)
are referenced by ’this’.
When an object is created to a class, a default reference (’this’)
is also created internally to the object.
’this’ can refer to all the things of the present object.
(Prog. This2.java & M16.java)
Bikash Choudhury (RKMGECP) OOPS September 28,2021 66/68
Instance
Methods
Instance Methods
Instance methods are methods which act upon the instance variables.
The specialty of instance methods is that they can access not only
instance variables but also static variables directly. There are two
types of instance methods:
Accessor Methods:are the methods that simply access or read
the instance variables. They do not modify the instance
variables.
Mutator Methods:not only access the instance variables but
also modify them. (Prog. This2.java)
Bikash Choudhury (RKMGECP) OOPS September 28,2021 67/68
Passing Array and
Objects
Passing Array and Objects
Passing Array (Prog. Method17.java)
Passing Objects (Prog. Method12.java & Method13.java)
Primitive data types, objects, even object references -
everything is passed to methods using ’pass by value’ or ’call
by value’ concept. This means their bit by bit copy is passed to
the methods.
Bikash Choudhury (RKMGECP) OOPS September 28,2021 68/68

More Related Content

Similar to opps.pptx

F3-DP-2015-Milata-Tomas-java-ee-batch-editor (1)
F3-DP-2015-Milata-Tomas-java-ee-batch-editor (1)F3-DP-2015-Milata-Tomas-java-ee-batch-editor (1)
F3-DP-2015-Milata-Tomas-java-ee-batch-editor (1)
Tomáš Milata
 
Introduction to Software Development
Introduction to Software DevelopmentIntroduction to Software Development
Introduction to Software Development
Zeeshan MIrza
 
Presentation5
Presentation5Presentation5
Presentation5
Natasha Bains
 
API Doc Smackdown
API Doc SmackdownAPI Doc Smackdown
API Doc Smackdown
Ted Husted
 
Professional-core-java-training
Professional-core-java-trainingProfessional-core-java-training
Professional-core-java-training
Vibrant Technologies & Computers
 
Android NDK Overview
Android NDK OverviewAndroid NDK Overview
Android NDK Overview
Badrinath Kulkarni
 
Java - OOPS and Java Basics
Java - OOPS and Java BasicsJava - OOPS and Java Basics
Java - OOPS and Java Basics
Victer Paul
 
Introduction to Behavior Driven Development
Introduction to Behavior Driven Development Introduction to Behavior Driven Development
Introduction to Behavior Driven Development
Robin O'Brien
 
Java Is A Programming Dialect And Registering Stage Essay
Java Is A Programming Dialect And Registering Stage EssayJava Is A Programming Dialect And Registering Stage Essay
Java Is A Programming Dialect And Registering Stage Essay
Liz Sims
 
_var_www_moodledata_temp_turnitintooltwo_1014058337._Ioan_Tuns-HNDCSD-PJ-19-1...
_var_www_moodledata_temp_turnitintooltwo_1014058337._Ioan_Tuns-HNDCSD-PJ-19-1..._var_www_moodledata_temp_turnitintooltwo_1014058337._Ioan_Tuns-HNDCSD-PJ-19-1...
_var_www_moodledata_temp_turnitintooltwo_1014058337._Ioan_Tuns-HNDCSD-PJ-19-1...
Ioan Tuns
 
_var_www_moodledata_temp_turnitintooltwo_1014058337._Ioan_Tuns-HNDCSD-PJ-19-1...
_var_www_moodledata_temp_turnitintooltwo_1014058337._Ioan_Tuns-HNDCSD-PJ-19-1..._var_www_moodledata_temp_turnitintooltwo_1014058337._Ioan_Tuns-HNDCSD-PJ-19-1...
_var_www_moodledata_temp_turnitintooltwo_1014058337._Ioan_Tuns-HNDCSD-PJ-19-1...
Ioan Tuns
 
Class 1
Class 1Class 1
Class 1
Dario Pilozo
 
Class 1
Class 1Class 1
Class 1
Dario Pilozo
 
Ijetcas14 385
Ijetcas14 385Ijetcas14 385
Ijetcas14 385
Iasir Journals
 
Java Programming : introduction
Java Programming : introductionJava Programming : introduction
Java Programming : introduction
Kongu Engineering College, Perundurai, Erode
 
Professional-core-java-training
Professional-core-java-trainingProfessional-core-java-training
Professional-core-java-training
Vibrant Technologies & Computers
 
Global Earthquake Monitor (Real Time)
Global Earthquake Monitor (Real Time)Global Earthquake Monitor (Real Time)
Global Earthquake Monitor (Real Time)
HimanshiSingh71
 
DevSecCon Singapore 2018 - Remove developers’ shameful secrets or simply rem...
DevSecCon Singapore 2018 -  Remove developers’ shameful secrets or simply rem...DevSecCon Singapore 2018 -  Remove developers’ shameful secrets or simply rem...
DevSecCon Singapore 2018 - Remove developers’ shameful secrets or simply rem...
DevSecCon
 
CAR SHOWROOM SYSTEM
CAR SHOWROOM SYSTEMCAR SHOWROOM SYSTEM
CAR SHOWROOM SYSTEM
Abhishek Shakya
 
Shuzworld Analysis
Shuzworld AnalysisShuzworld Analysis
Shuzworld Analysis
Brenda Thomas
 

Similar to opps.pptx (20)

F3-DP-2015-Milata-Tomas-java-ee-batch-editor (1)
F3-DP-2015-Milata-Tomas-java-ee-batch-editor (1)F3-DP-2015-Milata-Tomas-java-ee-batch-editor (1)
F3-DP-2015-Milata-Tomas-java-ee-batch-editor (1)
 
Introduction to Software Development
Introduction to Software DevelopmentIntroduction to Software Development
Introduction to Software Development
 
Presentation5
Presentation5Presentation5
Presentation5
 
API Doc Smackdown
API Doc SmackdownAPI Doc Smackdown
API Doc Smackdown
 
Professional-core-java-training
Professional-core-java-trainingProfessional-core-java-training
Professional-core-java-training
 
Android NDK Overview
Android NDK OverviewAndroid NDK Overview
Android NDK Overview
 
Java - OOPS and Java Basics
Java - OOPS and Java BasicsJava - OOPS and Java Basics
Java - OOPS and Java Basics
 
Introduction to Behavior Driven Development
Introduction to Behavior Driven Development Introduction to Behavior Driven Development
Introduction to Behavior Driven Development
 
Java Is A Programming Dialect And Registering Stage Essay
Java Is A Programming Dialect And Registering Stage EssayJava Is A Programming Dialect And Registering Stage Essay
Java Is A Programming Dialect And Registering Stage Essay
 
_var_www_moodledata_temp_turnitintooltwo_1014058337._Ioan_Tuns-HNDCSD-PJ-19-1...
_var_www_moodledata_temp_turnitintooltwo_1014058337._Ioan_Tuns-HNDCSD-PJ-19-1..._var_www_moodledata_temp_turnitintooltwo_1014058337._Ioan_Tuns-HNDCSD-PJ-19-1...
_var_www_moodledata_temp_turnitintooltwo_1014058337._Ioan_Tuns-HNDCSD-PJ-19-1...
 
_var_www_moodledata_temp_turnitintooltwo_1014058337._Ioan_Tuns-HNDCSD-PJ-19-1...
_var_www_moodledata_temp_turnitintooltwo_1014058337._Ioan_Tuns-HNDCSD-PJ-19-1..._var_www_moodledata_temp_turnitintooltwo_1014058337._Ioan_Tuns-HNDCSD-PJ-19-1...
_var_www_moodledata_temp_turnitintooltwo_1014058337._Ioan_Tuns-HNDCSD-PJ-19-1...
 
Class 1
Class 1Class 1
Class 1
 
Class 1
Class 1Class 1
Class 1
 
Ijetcas14 385
Ijetcas14 385Ijetcas14 385
Ijetcas14 385
 
Java Programming : introduction
Java Programming : introductionJava Programming : introduction
Java Programming : introduction
 
Professional-core-java-training
Professional-core-java-trainingProfessional-core-java-training
Professional-core-java-training
 
Global Earthquake Monitor (Real Time)
Global Earthquake Monitor (Real Time)Global Earthquake Monitor (Real Time)
Global Earthquake Monitor (Real Time)
 
DevSecCon Singapore 2018 - Remove developers’ shameful secrets or simply rem...
DevSecCon Singapore 2018 -  Remove developers’ shameful secrets or simply rem...DevSecCon Singapore 2018 -  Remove developers’ shameful secrets or simply rem...
DevSecCon Singapore 2018 - Remove developers’ shameful secrets or simply rem...
 
CAR SHOWROOM SYSTEM
CAR SHOWROOM SYSTEMCAR SHOWROOM SYSTEM
CAR SHOWROOM SYSTEM
 
Shuzworld Analysis
Shuzworld AnalysisShuzworld Analysis
Shuzworld Analysis
 

Recently uploaded

Maximize Your Content with Beautiful Assets : Content & Asset for Landing Page
Maximize Your Content with Beautiful Assets : Content & Asset for Landing Page Maximize Your Content with Beautiful Assets : Content & Asset for Landing Page
Maximize Your Content with Beautiful Assets : Content & Asset for Landing Page
pmgdscunsri
 
LGBTQIA Pride Month presentation Template
LGBTQIA Pride Month presentation TemplateLGBTQIA Pride Month presentation Template
LGBTQIA Pride Month presentation Template
DakshGudwani
 
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
3vgr39kx
 
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
21uul8se
 
Introduction to User experience design for beginner
Introduction to User experience design for beginnerIntroduction to User experience design for beginner
Introduction to User experience design for beginner
ellemjani
 
Manual ISH (International Society of Hypertension)
Manual ISH (International Society of Hypertension)Manual ISH (International Society of Hypertension)
Manual ISH (International Society of Hypertension)
bagmai
 
ARENA - Young adults in the workplace (Knight Moves).pdf
ARENA - Young adults in the workplace (Knight Moves).pdfARENA - Young adults in the workplace (Knight Moves).pdf
ARENA - Young adults in the workplace (Knight Moves).pdf
Knight Moves
 
NHR Engineers Portfolio 2023 2024 NISHANT RATHI
NHR Engineers Portfolio 2023 2024 NISHANT RATHINHR Engineers Portfolio 2023 2024 NISHANT RATHI
NHR Engineers Portfolio 2023 2024 NISHANT RATHI
NishantRathi18
 
Divertidamente SLIDE.pptxufururururuhrurid8dj
Divertidamente SLIDE.pptxufururururuhrurid8djDivertidamente SLIDE.pptxufururururuhrurid8dj
Divertidamente SLIDE.pptxufururururuhrurid8dj
lunaemel03
 
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANEEASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
Febless Hernane
 
一比一原版布兰登大学毕业证(BU毕业证书)如何办理
一比一原版布兰登大学毕业证(BU毕业证书)如何办理一比一原版布兰登大学毕业证(BU毕业证书)如何办理
一比一原版布兰登大学毕业证(BU毕业证书)如何办理
wkip62b
 
Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
Designforuminternational
 
一比一原版美国哥伦比亚大学毕业证Columbia成绩单一模一样
一比一原版美国哥伦比亚大学毕业证Columbia成绩单一模一样一比一原版美国哥伦比亚大学毕业证Columbia成绩单一模一样
一比一原版美国哥伦比亚大学毕业证Columbia成绩单一模一样
881evgn0
 
Timeless Principles of Good Design
Timeless Principles of Good DesignTimeless Principles of Good Design
Timeless Principles of Good Design
Carolina de Bartolo
 
原版制作(MDIS毕业证书)新加坡管理发展学院毕业证学位证一模一样
原版制作(MDIS毕业证书)新加坡管理发展学院毕业证学位证一模一样原版制作(MDIS毕业证书)新加坡管理发展学院毕业证学位证一模一样
原版制作(MDIS毕业证书)新加坡管理发展学院毕业证学位证一模一样
hw2xf1m
 
Graphic Design Tools and Software .pptx
Graphic Design Tools and Software   .pptxGraphic Design Tools and Software   .pptx
Graphic Design Tools and Software .pptx
Virtual Real Design
 
定制美国西雅图城市大学毕业证学历证书原版一模一样
定制美国西雅图城市大学毕业证学历证书原版一模一样定制美国西雅图城市大学毕业证学历证书原版一模一样
定制美国西雅图城市大学毕业证学历证书原版一模一样
qo1as76n
 
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
k7nm6tk
 
SECURING BUILDING PERMIT CITY OF CALOOCAN.pdf
SECURING BUILDING PERMIT CITY OF CALOOCAN.pdfSECURING BUILDING PERMIT CITY OF CALOOCAN.pdf
SECURING BUILDING PERMIT CITY OF CALOOCAN.pdf
eloprejohn333
 
Virtual Tour Application Powerpoint for museum of edinburgh
Virtual Tour Application Powerpoint for museum of edinburghVirtual Tour Application Powerpoint for museum of edinburgh
Virtual Tour Application Powerpoint for museum of edinburgh
millarj46
 

Recently uploaded (20)

Maximize Your Content with Beautiful Assets : Content & Asset for Landing Page
Maximize Your Content with Beautiful Assets : Content & Asset for Landing Page Maximize Your Content with Beautiful Assets : Content & Asset for Landing Page
Maximize Your Content with Beautiful Assets : Content & Asset for Landing Page
 
LGBTQIA Pride Month presentation Template
LGBTQIA Pride Month presentation TemplateLGBTQIA Pride Month presentation Template
LGBTQIA Pride Month presentation Template
 
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
 
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
 
Introduction to User experience design for beginner
Introduction to User experience design for beginnerIntroduction to User experience design for beginner
Introduction to User experience design for beginner
 
Manual ISH (International Society of Hypertension)
Manual ISH (International Society of Hypertension)Manual ISH (International Society of Hypertension)
Manual ISH (International Society of Hypertension)
 
ARENA - Young adults in the workplace (Knight Moves).pdf
ARENA - Young adults in the workplace (Knight Moves).pdfARENA - Young adults in the workplace (Knight Moves).pdf
ARENA - Young adults in the workplace (Knight Moves).pdf
 
NHR Engineers Portfolio 2023 2024 NISHANT RATHI
NHR Engineers Portfolio 2023 2024 NISHANT RATHINHR Engineers Portfolio 2023 2024 NISHANT RATHI
NHR Engineers Portfolio 2023 2024 NISHANT RATHI
 
Divertidamente SLIDE.pptxufururururuhrurid8dj
Divertidamente SLIDE.pptxufururururuhrurid8djDivertidamente SLIDE.pptxufururururuhrurid8dj
Divertidamente SLIDE.pptxufururururuhrurid8dj
 
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANEEASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
 
一比一原版布兰登大学毕业证(BU毕业证书)如何办理
一比一原版布兰登大学毕业证(BU毕业证书)如何办理一比一原版布兰登大学毕业证(BU毕业证书)如何办理
一比一原版布兰登大学毕业证(BU毕业证书)如何办理
 
Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
 
一比一原版美国哥伦比亚大学毕业证Columbia成绩单一模一样
一比一原版美国哥伦比亚大学毕业证Columbia成绩单一模一样一比一原版美国哥伦比亚大学毕业证Columbia成绩单一模一样
一比一原版美国哥伦比亚大学毕业证Columbia成绩单一模一样
 
Timeless Principles of Good Design
Timeless Principles of Good DesignTimeless Principles of Good Design
Timeless Principles of Good Design
 
原版制作(MDIS毕业证书)新加坡管理发展学院毕业证学位证一模一样
原版制作(MDIS毕业证书)新加坡管理发展学院毕业证学位证一模一样原版制作(MDIS毕业证书)新加坡管理发展学院毕业证学位证一模一样
原版制作(MDIS毕业证书)新加坡管理发展学院毕业证学位证一模一样
 
Graphic Design Tools and Software .pptx
Graphic Design Tools and Software   .pptxGraphic Design Tools and Software   .pptx
Graphic Design Tools and Software .pptx
 
定制美国西雅图城市大学毕业证学历证书原版一模一样
定制美国西雅图城市大学毕业证学历证书原版一模一样定制美国西雅图城市大学毕业证学历证书原版一模一样
定制美国西雅图城市大学毕业证学历证书原版一模一样
 
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
 
SECURING BUILDING PERMIT CITY OF CALOOCAN.pdf
SECURING BUILDING PERMIT CITY OF CALOOCAN.pdfSECURING BUILDING PERMIT CITY OF CALOOCAN.pdf
SECURING BUILDING PERMIT CITY OF CALOOCAN.pdf
 
Virtual Tour Application Powerpoint for museum of edinburgh
Virtual Tour Application Powerpoint for museum of edinburghVirtual Tour Application Powerpoint for museum of edinburgh
Virtual Tour Application Powerpoint for museum of edinburgh
 

opps.pptx

  • 1. Object Oriented Programming (PCC- CS503) Bikash Choudhury Assistant Professor Department of Computer Science & Engineering Ramkrishna Mahato Government Engineering College Purulia September 28, 2021 Bikash Choudhury (RKMGECP) OOPS September 28,2021 1/68
  • 2. Content s 1 Why Object Oriented Programming Why Object-Oriented programming? Why Java? Literature of Java 2 Entire Execution Process in Java How to Write and Execute Java Program Input and Output in Java Strings Introduction to OOPS Methods Bikash Choudhury (RKMGECP) OOPS September 28,2021 2/68
  • 3. Why Object-Oriented Programming? Object 1. Philosophically an object is anything that really exists in the world and can be distinguished from others. Everything that we see physically like, every human being, a book, a dog, a tree etc. are the example of object. 2. In computer science, an object can be a variable, a data structure, a function, or a method, and as such, is a value in memory referenced by an identifier. 3. In the class-based and object-oriented programming paradigms, object refers to a particular instance of a class, where the object can be a combination of variables, functions, and data structures. 4. In the relational model of database management, an object can be a table or column, or an association between data and a database entity (such as relating a person’s age to a specific person) [... wiki] Bikash Choudhury (RKMGECP) OOPS September 28,2021 3/68
  • 4. What is Object Oriented Programming? Object Oriented Programming 1.Object Oriented programming (OOP) is a programming paradigm that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes) which are used to create individual instances of objects. Bikash Choudhury (RKMGECP) OOPS September 28,2021 4/68
  • 5. Why Object-Oriented Programming? 1.OOP makes code organized, reusable and easy to maintain. It follows the DRY (Dont Repeat Yourself) principle used by many programmers to make efficient programs. 2.OOP also prevents unwanted access to data, or exposing proprietary code through encapsulation and abstraction. 3.So how do programmers create Object Oriented programs? Well the short answer is by making classes, and creating objects from the classes. Classes form the blueprint for how data & behaviors are structured. 4.Most programming languages are a mix of different ways of writing computer programs. For example, Python allows for computer programs to be written both in object-oriented programming and in procedural programming. There are many programming languages that allow you to write computer programs in object-oriented programming. Some of these programming languages are: C++, Java, Ruby, Perl, Emarald, Sapphire, PHP, Python, etc. [...wiki] Bikash Choudhury (RKMGECP) OOPS September 28,2021 5/68
  • 6. Why Java? P e n t i u m P r o c e s s o r W i n d o w s O / S C e l e r o n P r o c e s s o r U b u n t u O / S S p a r k P r o c e s s o r M a c O / S 1 . I n s t r u c t i o n ( a d d a , b ) 2 . E x t e n s i o n ( T e s t . e x e ) 1 . I n s t r u c t i o n ( a a d d b ) 2 . E x t e n s i o n ( T es t . o u t ) Figure:Difficulties in M/C Dependent Program 1 CC + + is not suitable for developing software forInternet. 2 What to do? Bikash Choudhury (RKMGECP) OOPS September 28,2021 6/68
  • 7. Why Java? S a m p l e . o u t / S a m p l e . e x e ( M a c h i n e C o d e ) O u t p u t S a m p l e . c l a s s ( B y t e C o d e ) M a c h i n e C o d e ( J V M ) S a m p l e . j a v a ( S o u r c e C o d e ) C o m p i l e r S a m p l e . c C o m p i l e r E x e c u t e O u t p u t C o m p i l e r o f J V M E x e c u t e Figure:Difficulties in M/C Dependent Program Bikash Choudhury (RKMGECP) OOPS September 28,2021 7/68
  • 8. Why Java? S a m p l e . o u t / S a m p l e . e x e ( M a c h i n e C o d e ) O u t p u t S a m p l e . c l a s s ( B y t e C o d e ) S a m p l e . j a v a ( S o u r c e C o d e ) C o m p i l e r S a m p l e . c C o m p i l e r E x e c u t e O u t p u t C o m p i l e r o f J V M M a c h i n e C o d e ( J V M ) E x e c u t e M / C I n d e p e n d e n t M / C D e p e n d e n t Figure:Difficulties in M/C Dependent Program Bikash Choudhury (RKMGECP) OOPS September 28,2021 8/68
  • 9. Literature of Java In 1990, Sun Microsystems Inc.(US) has conceived a project to develop s/w consumer electronic devices that could be controlled bya remote. This project was called Stealth Project but later its name was changed toGreen Project. In 1991, Bill Joy, James Gosling and their team has several discussions regarding the project. Gosling thought CC + + could be used to develop the project. As CC + + is m/c dependent, Gosling starts to develop new language for the aforesaid project. This language initially calledOak. Later it was changed toJAVA. Why the name JAVA? In 1994,WebRunnera Java based web browser was developed later renamed it asHotJava. HotJava was the first browser, having the capabilities of executing applets(programs designed to run dynamically in internet). Bikash Choudhury (RKMGECP) OOPS September 28,2021 9/68
  • 10. Literature of Java Sun formally announcedJavaandHotJavaatSun Worldconference in 1995. Later, Netscape, Microsoft announced that they would support Java in their web browser. On January 23rd 1996,JDK 1.0version was released. Now, more than 9 million developers use java and more than 4 billion electronics devices, including mobile phones and TVs, run on Java. Bikash Choudhury (RKMGECP) OOPS September 28,2021 10/68
  • 11. JDK JRE JVM Figure:JDK & JRE Bikash Choudhury (RKMGECP) OOPS September 28,2021 11/68
  • 12. How to Write and Execute Java Program 1 First installJREandJDK. Check whether JRE and JDK installed properly or not! Next, open any text editor, write the code and save the file with ‘.java’extension. Next, to compile the code, open the terminal and enter to the folder where recently created ‘.java’ file reside. Next, writejavac filename.java. After compilation, compiler generates‘.class’file. To run the ‘.class’ file write in the terminaljava ‘filename.class’. 2 3 4 5 Bikash Choudhury (RKMGECP) OOPS September 28,2021 12/68
  • 13. JRE and JDK Installation Procedure 1.open terminal and enter the below command Figure:Check JRE and JDK are installed or not! Bikash Choudhury (RKMGECP) OOPS September 28,2021 13/68
  • 14. JRE and JDK Installation Procedure 2.open terminal and enter the below command Figure:Install JRE Bikash Choudhury (RKMGECP) OOPS September 28,2021 14/68
  • 15. JRE and JDK Installation Procedure 3.open terminal and enter the below command Figure:Install JRE Bikash Choudhury (RKMGECP) OOPS September 28,2021 15/68
  • 16. JRE and JDK Installation Procedure 4.open terminal and enter the below command Figure:Install JRE Bikash Choudhury (RKMGECP) OOPS September 28,2021 16/68
  • 17. JRE and JDK Installation Procedure 5.open terminal and enter the below command to install the JDK Figure:Install JDK Bikash Choudhury (RKMGECP) OOPS September 28,2021 17/68
  • 18. JRE and JDK Installation Procedure 6.open terminal and enter the below command Figure:Install JDK Bikash Choudhury (RKMGECP) OOPS September 28,2021 18/68
  • 19. How to Write and Execute Java Program 1 First installJREandJDK. Check whether JRE and JDK installed properly or not! Next, open any text editor, write the code and save the file with ‘.java’extension. Next, to compile the code, open the terminal and enter to the folder where recently created ‘.java’ file reside. Next, writejavac filename.java. After compilation, compiler generates‘.class’file. To run the ‘.class’ file write in the terminaljava ‘filename.class’. 2 3 4 5 Bikash Choudhury (RKMGECP) OOPS September 28,2021 19/68
  • 20. How to Write Java Program 1 There are three types of comments available in Java.‘Single line’, ‘multi-line’and‘Java-documentation’. Single line //This is the single line comment Multi-line /∗ This is multi-line comments ∗/ Java-documentation / ∗∗ it provides description of every feature in Java Program ∗/ This (Java-documentation) description is helpful in the creation of a ‘.html’ file called API (Application Program Interface) document. write Please go through the first program (Hellop.java)in the Assignment and execute it. 2 3 4 5 6 Bikash Choudhury (RKMGECP) OOPS September 28,2021 20/68
  • 21. How to Write Java Program Impor t 1 2 3 4 5 In CC + + we use‘# include < stdio.h >’. What happens when we include the header file? Similar but more efficient mechanism is available in Java called ‘import’. import java.lang.System; Java Library → Packages → Classes → Methods Please go through the first program (Hello.java)in the Assignment1.0 and and execute it. Bikash Choudhury (RKMGECP) OOPS September 28,2021 21/68
  • 22. Assignme nt Assignme nt1 2 3 4 5 6 7 8 9 10 What is the difference between an executable file and a ‘.class’ file? Why ‘.class’ file is called Byte code? Why Java is suitable for Internet? Why pointers are eliminated from Java? What is the difference between a function and a method? Is Java a purely object oriented language or not? Justify. Which part of the JVM will allocate the memory for a Java program? Explain JDK,JRE,JVM and JIT. Difference between include and import. Difference between C++ and Java. Bikash Choudhury (RKMGECP) OOPS September 28,2021 22/68
  • 23. JDK JRE JVM Figure:JDK JRE JVM Bikash Choudhury (RKMGECP) OOPS September 28,2021 23/68
  • 24. JDK JRE JVM Figure:JDK JRE JVM Bikash Choudhury (RKMGECP) OOPS September 28,2021 24/68
  • 25. JVM JVM 1 2 3 4 5 First,‘.Java’program is compiled and converted to‘.class’(Byte Code) by the Compiler of JDK/Java. Second,‘.class’file is given to the JVM/JRE for the execution. In JVM there is a module called‘Class Loader Sub System’which performsa)loads ‘.class’ in to memoryb)Verifies all the byte code instructions are proper or not? if suspicious instructions found then rejects the execution. Otherwise allocates the necessary memory to execute the program. Method Area:stores class code, code of the variables and code of the methods in the Java program. Heap Area:here objects are created. Java Stacks:Here java methods are executed. Especially stores all the data and results used by the method. Bikash Choudhury (RKMGECP) OOPS September 28,2021 25/68
  • 26. JVM JVM 1 PC Register:contain memory address of the instructions of the methods. 2 Native method stacks:native methods ( CC + +) are executed on 3 4 5 6 native method stacks. Native method interface:To execute native method, native method libraries are required. These header files are located and connected to JVM by a program called Native Method Interface. Execution Engine:containsinterpreterandJIT(Just in Time) compiler. Both are responsible for converting Byte code to machine code so that processor can understand and execute accordingly. Both interpreter and JIT compiler works simultaneously to convert Byte code to m/c code. Why both are needed and how both works simultaneously? Bikash Choudhury (RKMGECP) OOPS September 28,2021 26/68
  • 27. JVM 1 Consider a sample byte code 1. print a = 1; 2. print b = 2; 3. for(i = 0;i < 10;i ++) {print a}; Interpreter starts execution from line1 it converts print a = 1; into machine code and gives it to microprocessor. Assume interpreter takes2 nanoseconds to do the aforesaid. Next, processor takes it execute it and displayed the value of a = 1. Again interpreter comes back to the memory and read the second instruction print b = 2;. Interpreter takes another2 nanoseconds to do the aforesaid. Next, processor takes it execute it and displayed the value of b = 2. 2 3 Bikash Choudhury (RKMGECP) OOPS September 28,2021 27/68
  • 28. JVM 1 Next, interpreter comes back to the memory and read the third instruction which is a looping statement {print a};. This should be done10times and this is known to the interpreter. So, first time it converts the instruction to m/c code and gives it to the processor and comes back to the memory for that it takes another2 nanoseconds. Similarly10times the interpreter read the instruction and gives to the 2 processor. So, total it takes10 ∗ 2 = 20 nanoseconds to read the third instruction and gives it to the processor. Moreover, It is not efficient with respect to time. This is the only reason JVM does not allocate 3rd instruction to the interpreter instead it allocates to the JIT compiler. 3 JIT compiler reads the print a; instruction and converts that into m/c code. For this it takes2 nanoseconds. Next, JIT compiler allots a block of memory and pushes this m/c code instruction into that memory. For this it takes another2 nanoseconds. So, JIT takes total of4 nanoseconds. Bikash Choudhury (RKMGECP) OOPS September 28,2021 28/68
  • 29. JVM 1 Now processor will fetch the instructions from the memory and execute it 10times. 2 Observe that JIT compiler takes only4 nanoseconds to execute the 3rd instruction, where as interpreter takes10 ∗ 2 = 20 nanoseconds for the same instruction. Hence, the JIT compiler increases the speed of the execution. 3 Remember, first two instructions will not be allotted to the JIT compiler by the JVM. (Interpreter takes2 nanoseconds to convert byte code to m/c code whereas JIT takes4 nanoseconds). After loading the‘.class’code into the class loader JVM identifies which code is to be left to the interpreter and which one to the JIT compiler so that the performance is better. The blocks of code allocated fro the JIT compiler also called‘ hotspotsj. 4 Bikash Choudhury (RKMGECP) OOPS September 28,2021 29/68
  • 30. Class and Object Properties:Variable Actions: methods () Class: Dog 1. name 2. height 3. colour 4. age 1.running 2.barking 3.eating 1. Tom 2. 1 foot 3. brown 4. 5 years Properties:Variable Actions: methods () 1. run () 2. bark () 3. eat () Object: Tom Figure:Class and Object Bikash Choudhury (RKMGECP) OOPS September 28,2021 30/68
  • 31. My First Program First Program 1.import java.lang.System; // java library—Package—Class– Methods 2.import java.lang.String; 3.//import java.lang.*; 4.class Hello 5.{ 6. public static void main(String args[]) 7. { 8. System.out.print(”Hello PGEC”); 9. } 10.} Bikash Choudhury (RKMGECP) OOPS September 28,2021 31/68
  • 32. My First Program 1.public:Access specifier. If we do not made main() as public then JVM does not allow to execute it. 2.static:methods which can be called and executed without creating the objects. 3.void:return type of the main method(). 4.main():is a method also starting point of the program. it accepts a group of strings. 5.String args[]:string type array. args[] is the name of array and it is of String type. 6.System: class name.print() belongs to PrintStream class. We should call the print() by creating an object to PrintStream class. But, as it is not possible to create an object of the PrintStream class i.e., we use alternative approach. 7.out:static variable in System class (When we call ‘out’ internally an object of PrintStream class is created). 8.print():used to display messages on the monitor. Bikash Choudhury (RKMGECP) OOPS September 28,2021 32/68
  • 33. Create a Class Class 1.class Person { 2.statements; // any no. of variables and methods we can create 3.} Object 1.class-name new-object-name = new class-name(); 2. Hello Suman =new Person(); // Suman object is created Bikash Choudhury (RKMGECP) OOPS September 28,2021 33/68
  • 34. Variables and Data types Variables in Java Data types in Java Bikash Choudhury (RKMGECP) OOPS September 28,2021 34/68
  • 35. Operator and Array Operators in Java Array in Java Bikash Choudhury (RKMGECP) OOPS September 28,2021 35/68
  • 36. Input and Output in Java 1 A stream is required to accept input from the keyboard. Stream represents flow of data from one place to another. Like a water pipe carries water from one place to another, similarly Stream carries data from one place to another (from keyboard to memory or from memory to file or printer). Different types of Stream:a) Input Stream:which receives or read data coming from some another place.b) Output Stream:which send or write data to some other place. All streams (input or output) are represented in’java.io’. System.in: represents InputStream object, which by default represents standard input device, i.e., keyboard. System.out: represents InputStream object, which by default represents standard output device, i.e., monitor. System.err: represents InputStream object, which by default represents monitor. Difference betweenSystem.outandSystem.err? 2 3 4 5 6 7 8 Bikash Choudhury (RKMGECP) OOPS September 28,2021 36/68
  • 37. Input in Java 1 java.io.Console(Please open program Cmd.java from Assignment1.5.) java.io.BufferedReader java.util.Scanner 2 3 Bikash Choudhury (RKMGECP) OOPS September 28,2021 37/68
  • 38. Input through System.in 1 Keyboard (System.in) → input stream (InputStreamReader) → input stream (BufferedReader). To accept data from keyboard (i.e., System.in) we need to connect it to an input stream (InputStreamReader) as some input stream is needed to read data. Connect the keyboard to an input stream object and converts byte-oriented stream to character-oriented stream. InputStreamReader obj = new InputStreamReader(System.in); To read data properly coming from InputStreamReader, we need another stream called BufferedReader or BufferedReader class can be used to read data line by line by readLine() method. BufferedReader br = new BufferedReader(obj); BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); // alternate approach 2 3 4 5 6 7 Bikash Choudhury (RKMGECP) OOPS September 28,2021 38/68
  • 39. Java BufferedReader class methods 1 read(): used for reading a single character. markSupported(): used to test the input stream support for the mark and reset method. readLine(): used for reading a line of text. ready(): used to test whether the input stream is ready to be read. skip(): used for skipping the characters. reset(): reposition the stream at a position the mark method was last called on this input stream. mark(): is used for marking the present position in astream. close(): closes the input stream and releases any of the system resources associated with the stream. 2 3 4 5 6 7 8 Bikash Choudhury (RKMGECP) OOPS September 28,2021 39/68
  • 40. Input through java.util.Scanner Class We can use Scanner class of java.util package to read input from the keyboard or a text file. When the Scanner class receives input, it breaks it into several pieces, called tokens. These token can be retrieve from the Scanner object using the following methods: 1 nextLine(): used to read Strings from user. nextBoolean():Reads a boolean value from the user. nextByte(): Reads a byte value from the user. nextDouble(): Reads a double value from the user. nextFloat(): Reads a float value from the user. nextInt(): Reads a int value from the user. nextLong(): Reads a long value from the user. nextShort(): Reads a short value from the user. Please open program Scan.java from Assignment1.5. 2 3 4 5 6 7 8 9 Bikash Choudhury (RKMGECP) OOPS September 28,2021 40/68
  • 41. Output through System.out.printf() To format and display the output printf() is available in PrintStream class. This class works similar to printf() in c. We use System.out.printf(). 1 %s: string %c: character %d : decimalinteger %f : float number %o: octal number %b, %B: boolean value %x, %X: hexadecimal number %e,%E : number in scientific notation %n: new line character Please open program Out.java from Assignment1.5. 2 3 4 5 6 7 8 9 10 Bikash Choudhury (RKMGECP) OOPS September 28,2021 41/68
  • 42. String s 1 String represents a group of characters. Most of the data transmits in internet is in the form of groups of characters. Such group of characters are called Strings. In CC + + languages, a String represents an array of characters, 2 where the last character 0 (called null character) represents the end of the Strings. But this is not valid in java.In java String is an object of String class. It is not an character array. In java we have character array also, but Strings are treated differently because of their extensive use in internet. Is String is a datatype or class?String is a class in ‘java.lang’ package. But in java all classes are considered as data types. So we can consider String as a data type also. Can we call a class a datatype?Yes, a class also called a user defined datatype. 3 4 5 Bikash Choudhury (RKMGECP) OOPS September 28,2021 42/68
  • 43. Creating Strings There arethreedifferent ways to create string in java. 1 We can create a String just by assigning a group of charters to a String type variable. String s; // Declare String type variable . s=“RKMGEC”; // Assign a group of characters to it. String s = ”RKMGEC” // Alternate String s = new String(”RKMGEC”); 2 3 char arr[ ]={ ’R’,’K’,’M’,’G’,’E’,’C’}; String s = new String(arr); String s = new String(arr,3,2); // Alternate: Starting from 3rd character, a total 2 characters are copied in s. Please open program StrCls1.java from Assignment1.6. 4 Bikash Choudhury (RKMGECP) OOPS September 28,2021 43/68
  • 44. Methods in Strings 1 hashCode():Returns the hash code of a string charAt():Returns the character at the specified index (position) concat():Appends a string to the end of another string contains():Checks whether a string contains a sequence of characters contentEquals():Checks whether a string contains the exact same sequence of characters of the specified CharSequence or StringBuffer endsWith():Checks whether a string ends with the specified character(s) equals():Compares two strings. Returns true if the strings are equal, and false if not equalsIgnoreCase():Compares two strings, ignoring case considerations format():Returns a formatted string using the specified 2 3 4 5 6 7 8 9 Bikash Choudhury (RKMGECP) OOPS September 28,2021 44/68
  • 45. Methods in Strings 1 getChars():Copies characters from a string to an array of chars indexOf():Returns the position of the first found occurrence of specified characters in a string isEmpty():Checks whether a string is empty or not length():Returns the length of a specified string split():Splits a string into an array of substrings startsWith():Checks whether a string starts with specified characters substring():Extracts the characters from a string, beginning at a specified start position, and through the specified number of character toLowerCase():Converts a string to lower case letters toUpperCase():Converts a string to upper 2 3 4 5 6 7 8 9 10 Bikash Choudhury (RKMGECP) OOPS September 28,2021 45/68
  • 46. String Comparison The relational operators like >,>=,<,<=,==,! = can not be used to compare to strings.equals() and compareTo()methods are used to compare the Strings. 1 When an object is created to the JVM it returns the memory address of the object as a hexadecimal code (Object reference). Every object will have unique object reference number. String s1 = ”RKMGEC” When JVM execute the above statement, it creates an object on heap and stores “RKMGEC” in it. A reference number (‘2e45b3’) is allotted for this object. String s2 = new String(”RKMGEC”); JVM creates another object and allot different reference number (‘3b34c2’). Please open program StrCls2.java & StrCls2.java from Assignment1.6. 2 3 4 Bikash Choudhury (RKMGECP) OOPS September 28,2021 46/68
  • 47. String Comparison == and equals() String s1 = ”RKMGEC” String s2 = new String(”RKMGEC”); Bikash Choudhury (RKMGECP) OOPS September 28,2021 47/68
  • 48. String Constant Pool String Constant Pool String s1 = ”RKMGEC” String s2 = ”RKMGEC”; Bikash Choudhury (RKMGECP) OOPS September 28,2021 48/68
  • 49. Immutability of String Immutable Objects are broadly divided into - a) Mutable (Contents can be modified) and b)Immutable (contents can not be modified) objects. In java String class objects are immutable. String s1 = ”Computer”; String s2 = ”Science”; s1=s1+s2; // ComputerScience (Please open program StrCls4.java from Assignment1.6.) Bikash Choudhury (RKMGECP) OOPS September 28,2021 49/68
  • 50. String Buffer & Builder 1 To overcome the immutability issues in String we useStringBuffer andStringBuilderclass. Please open program StrCls5.java from Assignment1.6. The main difference betweenStringBufferandStringBuilderis that the StringBuffer class is by default synchronized and StringBuilder is not. When a programmer wants to use several threads she/he should use StringBufferclass as it gives reliable results. If only one thread is used,StringBuilderis preferred, as it improves the execution. 2 3 4 Bikash Choudhury (RKMGECP) OOPS September 28,2021 50/68
  • 52. Class/Objec t 1 An object is anything thatreally exists in the world and can be distinguished from others. E.g., a car, a table, a chair, a person. A Class is amodel or blueprintfor creating objects. [Please open program Cao0.java from Assignment2.0]. 2 P r o p e r t i e s : V a r i a b l e A c t i o n s : m e t h o d s ( ) 1 . n a m e 2 . h e i g h t 3 . c o l o u r 4 . a g e 1 . r u n n i n g 2 . b a r k i n g 3 . e a t i n g 1 . T o m 2 . 1 f o o t 3 . b r o w n 4 . 5 y e a r s P r o p e r t i e s : V a r i a b l e A c t i o n s : m e t h o d s ( ) 1 . r u n ( ) 2 . b a r k ( ) 3 . e a t ( ) C l a s s : D o g O b j e c t : T o m Bikash Choudhury (RKMGECP) OOPS September 28,2021 52/68
  • 53. Encapsulation and Abstraction Encapsulatio n 1 2 Encapsulation is a mechanism where the data (variables) and the code (methods) that act on the data will bind together. Class consist of variables and methods tightly. Class is an example for Encapsulation. Abstractio n 1 2 There may be a lot of data, a class contains and the user does not need the entire data. The user requires only some part of the available data. In this case, we can hide the unnecessary data from the user and expose only that data that is of interest to the user. This is called abstraction. A car driver should know how to drive the car and does not require to know how the engine is designed. [Please open program Cao0.java from Assignment2.0] Bikash Choudhury (RKMGECP) OOPS September 28,2021 53/68
  • 54. Inheritance and Polymorphism Inheritanc e 1 2 It creates new classes from existing classes,so that new classes will acquire all the features of the existing classes is called Inheritance. Parents producing the children and children inhering the qualities of the parents. [Please open program Cao0.java from Assignment2.0] Polymorphis m1 2 The word ’Ploymorphism’ came from two Greek words’poly’meaning ’many’and’morphos’meaning’forms’. Thus, polymorphism represents the ability to assume several different forms. In programming, we can use a single variable to refer to objects of different types and thus, using that variable we can call the methods of different objects. [Please open program Cao0.java from Assignment2.0] Bikash Choudhury (RKMGECP) OOPS September 28,2021 54/68
  • 55. Hash Code Hash Code 1 2 Hash Code is a unique identification number allotted to the objects by the JVM. This hash code number also called the reference number which is created based on the location of the object in memory, andis unique for all objects, excepts for String objects. Please open program ’Cao1.java’ from Assignment2.0 Bikash Choudhury (RKMGECP) OOPS September 28,2021 55/68
  • 56. Hash Code p t r t o h e a p p t r t o s p e c i a l s t r u c t u r e i n s t a n c e v a r i a b l e 1 i n s t a n c e v a r i a b l e 2 i n s t a n c e v a r i a b l e 3 . . . C o d e o f M e t h o d 1 ( ) H e a p M e m o r y A l l S t a t i c V a r i a b l e s M e t h o d A r e a p t r t o s t a t i c v a r i b a l e s p t r t o m e t h o d 1 ( ) p t r t o m e t h o d 2 ( ) ... S p e c i a l S t r u c t u r e C o d e o f M e t h o d 2 ( ) O b j e c t R e f e r e n c e Figure:Hash Code Bikash Choudhury (RKMGECP) OOPS September 28,2021 56/68
  • 57. Hash Code 6 0 3 7 4 2 8 1 4 . . . N a m e : A g e : ... ... v o i d t a l k ( ) H e a p M e m o r y M e t h o d A r e a A b h i k Figure:Hash Code of Abhik Bikash Choudhury (RKMGECP) OOPS September 28,2021 57/68
  • 58. Access Specifier/Modifier Access Specifier Is a keyword that specifies how to access the members (variables,methods) of a class or class itself. 1 2 3 4 Private:’private’ members of a class are not accessible from anywhere outside the class. They are accessible only within the class by the methods of that class. Public:The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package. Protected:The access level of a protected modifier is within the package and outside the package through child class. If we do not make the child class, it cannot be accessed from outside the package. Default:The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If we do not specify any access level, it will be the default. Bikash Choudhury (RKMGECP) OOPS September 28,2021 58/68
  • 59. Access Specifier/Modifier Figure:Access Specifier/Modifier Please open program ’Cao2.java’ from Assignment2.0 Bikash Choudhury (RKMGECP) OOPS September 28,2021 59/68
  • 60. Constructo r Constructor Constructor is a mechanism to initialize instance variables. 1 2 3 4 5 6 Constructor’s name and class name should be same. Constructor’s name should end with a pair of simple braces. If a constructor does not have any parameters, it is called Default constructor. If a constructor has one or more parameters, it is called Parameterized constructor. A Constructor does not return any value, not even void. A Constructor is automatically called and executed at the time of creating an object. While creating an object, if no parameter is passed to the object, the default constructor is called and executed. If some values are passed to the object then parameterized constructor is called. Bikash Choudhury (RKMGECP) OOPS September 28,2021 60/68
  • 61. Method s Methods Represents a group of statements that perform a task (calculation, processing of data or generating a report). returndatatype methodname(parameter1, parameter2,...) A method can not return more than one value. Method without parameters and without return type. (P1) Method without parameters but with return type. (P2) Method with two parameters and return type. (P3) Instance method(): Methods which act on the instance variables of the class. To call the instance methods, we should use the form: objectname.methodname(); Bikash Choudhury (RKMGECP) OOPS September 28,2021 61/68
  • 62. Static Methods Static Methods Methods which do not act upon the instance variables of a class. Static methods are declared as ’static’. To call the static methods, we need not require to create object. We can call a static method as: Classname.methodname(); JVM first execute the instance methods and then only it creates the objects. Since the objects are not available at the time of calling the static methods, the instance variables are also not available. Static method can access static variables. Static variables are also declared as static. (P4,P5,P6) Bikash Choudhury (RKMGECP) OOPS September 28,2021 62/68
  • 63. Static Methods Difference between Instance variable and Static variable An instance variable is a variable whose separate copy is available to each object. A class/static variable is a variable whose single copy in memory is shared by all objects. (P7,P8) Instance variables are created in the objects on heap memory. Class variables are stored on method area. Bikash Choudhury (RKMGECP) OOPS September 28,2021 63/68
  • 64. Static Block Static Block is a block of statements declared as ’static’. static { statements; } 1 2 3 JVM executes a static block on a highest priority basis. JVM first execute the static block then it looks for the main () in the program. (Prog.M9.java) Can we write a program without main()? (Prog. M10.java) Bikash Choudhury (RKMGECP) OOPS September 28,2021 64/68
  • 65. Static Block Static Block is a block of statements declared as ’static’. static { statements; } 1 2 3 4 5 JVM executes a static block on a highest priority basis. JVM first execute the static block then it looks for the main () in the program. (Prog.M9.java) Can we write a program without main()? (Prog. M10.java) Yes. We can cheat the JVM before execution of main() method we can terminate the program. (Prog. M11.java) Moreover, it works until Java 1.6 version. Java 1.7 and newer versions dont allow this because JVM checks the presence of the main method before initializing the class. Bikash Choudhury (RKMGECP) OOPS September 28,2021 64/68
  • 66. Local Variables Local Variable Local variable is a variable that is declared as locally inside a method or constructor and is available only within that method or a constructor. Local variable can not be accessed outside the method or constructor. (Prog. M15.java) Sometimes a local variable has the same name as that of an instance variable. This leads to problems regarding their accessibility. (Prog. This1.java) How to refer to the instance variable? Bikash Choudhury (RKMGECP) OOPS September 28,2021 65/68
  • 67. thi s this ’this’ is a keyword that refers to the object of the class where it is used. ’this’ refers to the object of the present class. All members (instance variable, constructor and methods) are referenced by ’this’. When an object is created to a class, a default reference (’this’) is also created internally to the object. ’this’ can refer to all the things of the present object. (Prog. This2.java & M16.java) Bikash Choudhury (RKMGECP) OOPS September 28,2021 66/68
  • 68. Instance Methods Instance Methods Instance methods are methods which act upon the instance variables. The specialty of instance methods is that they can access not only instance variables but also static variables directly. There are two types of instance methods: Accessor Methods:are the methods that simply access or read the instance variables. They do not modify the instance variables. Mutator Methods:not only access the instance variables but also modify them. (Prog. This2.java) Bikash Choudhury (RKMGECP) OOPS September 28,2021 67/68
  • 69. Passing Array and Objects Passing Array and Objects Passing Array (Prog. Method17.java) Passing Objects (Prog. Method12.java & Method13.java) Primitive data types, objects, even object references - everything is passed to methods using ’pass by value’ or ’call by value’ concept. This means their bit by bit copy is passed to the methods. Bikash Choudhury (RKMGECP) OOPS September 28,2021 68/68