SlideShare ist ein Scribd-Unternehmen logo
1 von 31
Lecture 5-Java Training
Object-Oriented Programming
Two Paradigms
• all computer programs consist of two elements: code and data.
• a program can be conceptually organized around its code or around
its data.
• programs are written around “what is happening” and others are
written around “who is being affected.”
• The first way is called the process-oriented model. This approach
characterizes a program as a series of linear steps (that is, code).
The process-oriented model can be thought of as code acting on
data.
• Object-oriented programming organizes a program around its data
(that is, objects) and a set of well-defined interfaces to that data
OOP Principles
Encapsulation
1. Encapsulation is the mechanism that binds together code
and the data it manipulates, and keeps both safe from
outside interference and misuse.
2. One way to think about encapsulation is as a protective
wrapper that prevents the code and data from being
arbitrarily accessed by other code defined outside the
wrapper.
3. In Java the basis of encapsulation is the class.
Polymorphism
• Polymorphism (from the Greek, meaning “many
forms”) is a feature that allows one interface to
be used for a general class of actions.
• The specific action is determined by the exact
nature of the situation.
• More generally, the concept of polymorphism is
often expressed by the phrase “one interface,
multiple methods.”
Abstraction
• An essential element of object-oriented programming
is abstraction. Humans manage complexity through
abstraction.
• For example, people do not think of a car as a set of
tens of thousands of individual parts. They think of it as
a well-defined object with its own unique behaviour.
• This abstraction allows people to use a car to drive to
the grocery store without being overwhelmed by the
complexity of the parts that form the car. They can
ignore the details of how the engine, transmission, and
braking systems work. Instead they are free to utilize
the object as a whole.
Inheritance
• Inheritance is the process by which one object acquires the
properties of another object. This is important because it
supports the concept of hierarchical classification.
• However, by use of inheritance, an object need only define
those qualities that make it unique within its class. It can
inherit its general attributes from its parent.
• Thus, it is the inheritance mechanism that makes it possible
for one object to be a specific instance of a more general case.
OOPs Concept Basics
• Object
Any entity that has state and behavior is
known as an object. For example: chair, pen,
table, keyboard, bike etc. It can be physical
and logical.
• Class
Collection of objects is called class. It is a
logical entity.
Classes in Java
• A class can be defined as a template/blueprint that describes
the behavior/state that the object of its type support.
• A class is a blueprint from which individual objects are
created.
• Following is a sample of a class.
• Example
public class Dog {
String breed;
int age;
String color;
void barking() { }
void hungry() { }
void sleeping() { }
}
Variables in Classes
• A class can contain any of the following variable types.
• Local variables − Variables defined inside methods,
constructors or blocks are called local variables. The
variable will be declared and initialized within the
method and the variable will be destroyed when the
method has completed.
• Instance variables − Instance variables are variables
within a class but outside any method. These variables
are initialized when the class is instantiated. Instance
variables can be accessed from inside any method,
constructor or blocks of that particular class.
• Class variables − Class variables are variables declared
within a class, outside any method, with the static
keyword.
Objects in Java
• A class provides the blueprints for objects. So
basically, an object is created from a class. In Java,
the new keyword is used to create new objects.
• There are three steps when creating an object
from a class −
– Declaration − A variable declaration with a variable
name with an object type.
– Instantiation − The 'new' keyword is used to create
the object.
– Initialization − The 'new' keyword is followed by a call
to a constructor. This call initializes the new object.
Example For creating an Object
Following is an example of creating an object –
public class Puppy
{
public Puppy(String name)
{ // This constructor has one parameter, name.
System.out.println("Passed Name is :" + name
);
}
public static void main(String []args) {
// Following statement would create an object myPuppy
Puppy myPuppy = new Puppy( "tommy" );
}
}
Output
Passed Name is :tommy
Advantage of OOPs over Procedure-oriented
programming language
1) OOPs makes development and maintenance easier where as in Procedure-
oriented programming language it is not easy to manage if code grows as
project size grows.
2) OOPs provides data hiding whereas in Procedure-oriented programming
language a global data can be accessed from anywhere.
3) OOPs provides ability to simulate real-world event much more effectively.
We can provide the solution of real word problem if we are using the
Object-Oriented Programming language.
Java Reference Variable
• Reference variables are used to refer to an object. They are
declared with a specific type which cannot be changed.
• A reference variable can store the reference value of an
object, and can be used to manipulate the object denoted by
the reference value.
• A variable declaration that specifies a reference type (i.e., a
class, an array, or an interface name) declares a reference
variable.
Object and Reference Variable in Memory
• Basically a reference variable just points to an actual
object. For example if we have a class-
class Person {
String name = "Eric";
int age = 25;
}
If you instantiate the class Person like this-
Person p = new Person();
• Here p is a reference variable which points to
an object of class Person.
• The actual object of class Person created in
that statement resides on the heap.
• The reference variable p only contains the
memory address of Person class object on the
heap.
• So the actual value of p can be said to be an
address on the heap.
• The String name in class
Person is itself a
reference variable. So it
will point to a String
object on the heap.
• Here the reference
variable p points to an
object of type Person.
The name reference
variable in Person
object points to String
object on the heap.
Uses of reference variable
– Reference variable can be assigned value null to show that it is
not refering to any object.
Student amit = new Student();
amit=null;
– More than one reference variables may refer to same object.
These are called object aliases.
Student amit=new Student();
Student mady=amit;
Student mihir=mady;
– Default value for refrence variable is null;
– The object can be manipulated via any one of its aliases, as
each one refers to the same object.
– A reference variable can not refer to more then one object.
Class Person{
int age;
String name;
}
Class Demo{
public static void main(String args[])
{
Person p1=new Person();
p1.age=25;
p1.name=“Harry”;
System.out.println(p1.age+ “ :” + p1.name);
Person p2=p1;
System.out.println(p2.age+ “ :” + p2.name);
p2.age=30;
p2.name=“Garry”;
System.out.println(p2.age+ “ :” + p2.name);
System.out.println(p1.age+ “ :” + p1.name);
p1=null;
System.out.println(p1.age+ “ :” + p1.name); // Wrong
}
}
• Reference variable gets memory allocated in
stack.
• Instance variable gets memory allocated in
heap.
• Those Reference variable which are instance
members of an object, also gets memory
allocated in heap.
class College{
Student s1;
String address;
int year;
public static void main(String args[]){
College c1=new College();
}
}
Stack Heap
C1
Name=null
S1=null
Year=0
Types of variables
• Instance
• Class
• Local
Static Variable Example
class test {
static int i;
public static void main(String[] args) {
System.out.println("Value before calling method1: " + i);
test t1 = new test();
t1.method1();
System.out.println("Value after calling method1: " + i);
t1.method2();
System.out.println("Value after calling method2: " + i);
}
void method1() {
i++;
}
void method2() {
i++;
Instance / Local / Method Variable Example
class MPE {
// Instance Variable
int i;
public static void main(String[] args) {
/*Here i is an Instance variable.*/
test t1 = new test();
System.out.println("Value before calling method1: " + t1.i);
}
/* Here j is a method parameter.
And k is a local variable. Note**: Local variables life is only till the end of method*/
void method1(int j) {
int k;
i = j;
/* Local Variable(k)'s life ends once execution for this method completes. As k is local
is variable it needs to be initialized before we can use it. But as it is not getting
used here, it can stay here without initializing*/
}
}
Calling a Method
• For using a method, it should be called. There are two ways in
which a method is called i.e., method returns a value or
returning nothing (no return value).
• The process of method calling is simple. When a program
invokes a method, the program control gets transferred to the
called method. This called method then returns control to the
caller in two conditions, when −
• the return statement is executed.
• it reaches the method ending closing brace.
Example FOR CALLING A METHOD
public class StudentTest {
public static void main ( String[] args ) {
Student s1 = new Student () ;
Student s2 = new Student ( "Sai", 3 );
Student s3 = new Student ( "Gautham" , 4 , 98 , 100,
96);
System.out.println("Student s1: ");
s1.printDetails();  Object s1 calling method
printDetails()
System.out.println("nStudent s2: ");
s2.printDetails();  Object s2 calling method
printDetails()
System.out.println("nStudent s3: ");
s3.printDetails();  Object s3 calling method
printDetails()
}
What are Pass-by-value and Pass-by-
reference?
• Pass-by-value:
– A copy of the passed-in variable is copied into the
argument of the method. Any changes to the
argument do not affect the original one.
• Pass-by-reference:
– The argument is an alias of the passed-in
variable. Any changes to the argument will affect
the original one.
The following example proves that Java passes
object references to methods by value:
public class Swap {
public static void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
System.out.println("x(1) = " + x);
System.out.println("y(1) = " + y);
}
public static void main(String[] args) {
int x = 10;
int y = 20;
swap(x, y);
System.out.println("x(2) = " + x);
System.out.println("y(2) = " + y);
}
}
• This result proves that the x and y are swapped to each other in the inside
the swap() method, however the passed-in variables x and y did not get changed.
the above program prints
the following output:
x(1) = 20
y(1) = 10
x(2) = 10
y(2) = 20
Modifying Reference and Changing
Reference Examples
• Given the Dog class written as below:
class Dog {
protected String name;
Dog(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
Consider the following method that
modifies a Dog reference:
public void modifyReference(Dog dog) {
dog.setName("Rex");
}
• some testing code:
Dog dog1 = new Dog("Pun");
System.out.println("Before modify: " + dog1.getName());
modifyReference(dog1);
System.out.println("After modify: " + dog1.getName());
• The method argument points to the same Dog object as the
passed-in reference variable,
The following output:
Before modify: Pun
After modify: Rex
Consider the following method that attempts to change
reference of the passed-in parameter:
public void changeReference(Dog dog) {
Dog newDog = new Dog("Poo");
dog = newDog;
}
• some testing code:
Dog dog2 = new Dog("Meek");
System.out.println("Before change: " + dog2.getName());
tester.changeReference(dog2);
System.out.println("After change: " + dog2.getName());
• Since it’s impossible to change reference of a passed-in variable within a
method, hence the following output:
Before change: Meek
After change: Meek
Java always passes object references to method by value. That means
passing the memory address of the object that the variable points
to, not passing the variable itself, not the object itself. So we cannot
change reference of a passed-in variable in a method.

Weitere ähnliche Inhalte

Ähnlich wie Lecture 5.pptx

Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Getachew Ganfur
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming CourseDennis Chang
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...Sagar Verma
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1Geophery sanga
 
Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1Synapseindiappsdevelopment
 
Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsHelen SagayaRaj
 
Introduction to OOP with java
Introduction to OOP with javaIntroduction to OOP with java
Introduction to OOP with javaSujit Kumar
 
JAVA-PPT'S.pptx
JAVA-PPT'S.pptxJAVA-PPT'S.pptx
JAVA-PPT'S.pptxRaazIndia
 
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxJAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxKunalYadav65140
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oopcolleges
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptxSAICHARANREDDYN
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
Object oriented programming 6 oop with c++
Object oriented programming 6  oop with c++Object oriented programming 6  oop with c++
Object oriented programming 6 oop with c++Vaibhav Khanna
 

Ähnlich wie Lecture 5.pptx (20)

Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
 
OOSD1-unit1_1_16_09.pptx
OOSD1-unit1_1_16_09.pptxOOSD1-unit1_1_16_09.pptx
OOSD1-unit1_1_16_09.pptx
 
JAVA-PPT'S.pdf
JAVA-PPT'S.pdfJAVA-PPT'S.pdf
JAVA-PPT'S.pdf
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
 
OOPS in Java
OOPS in JavaOOPS in Java
OOPS in Java
 
Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1
 
oopm 2.pdf
oopm 2.pdfoopm 2.pdf
oopm 2.pdf
 
Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & Applets
 
Introduction to OOP with java
Introduction to OOP with javaIntroduction to OOP with java
Introduction to OOP with java
 
JAVA-PPT'S.pptx
JAVA-PPT'S.pptxJAVA-PPT'S.pptx
JAVA-PPT'S.pptx
 
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxJAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptx
 
Cs2305 programming paradigms lecturer notes
Cs2305   programming paradigms lecturer notesCs2305   programming paradigms lecturer notes
Cs2305 programming paradigms lecturer notes
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Object oriented programming 6 oop with c++
Object oriented programming 6  oop with c++Object oriented programming 6  oop with c++
Object oriented programming 6 oop with c++
 

Kürzlich hochgeladen

Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...ranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 

Kürzlich hochgeladen (20)

Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 

Lecture 5.pptx

  • 2. Object-Oriented Programming Two Paradigms • all computer programs consist of two elements: code and data. • a program can be conceptually organized around its code or around its data. • programs are written around “what is happening” and others are written around “who is being affected.” • The first way is called the process-oriented model. This approach characterizes a program as a series of linear steps (that is, code). The process-oriented model can be thought of as code acting on data. • Object-oriented programming organizes a program around its data (that is, objects) and a set of well-defined interfaces to that data
  • 3. OOP Principles Encapsulation 1. Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. 2. One way to think about encapsulation is as a protective wrapper that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper. 3. In Java the basis of encapsulation is the class.
  • 4. Polymorphism • Polymorphism (from the Greek, meaning “many forms”) is a feature that allows one interface to be used for a general class of actions. • The specific action is determined by the exact nature of the situation. • More generally, the concept of polymorphism is often expressed by the phrase “one interface, multiple methods.”
  • 5. Abstraction • An essential element of object-oriented programming is abstraction. Humans manage complexity through abstraction. • For example, people do not think of a car as a set of tens of thousands of individual parts. They think of it as a well-defined object with its own unique behaviour. • This abstraction allows people to use a car to drive to the grocery store without being overwhelmed by the complexity of the parts that form the car. They can ignore the details of how the engine, transmission, and braking systems work. Instead they are free to utilize the object as a whole.
  • 6. Inheritance • Inheritance is the process by which one object acquires the properties of another object. This is important because it supports the concept of hierarchical classification. • However, by use of inheritance, an object need only define those qualities that make it unique within its class. It can inherit its general attributes from its parent. • Thus, it is the inheritance mechanism that makes it possible for one object to be a specific instance of a more general case.
  • 7. OOPs Concept Basics • Object Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical. • Class Collection of objects is called class. It is a logical entity.
  • 8. Classes in Java • A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support. • A class is a blueprint from which individual objects are created. • Following is a sample of a class. • Example public class Dog { String breed; int age; String color; void barking() { } void hungry() { } void sleeping() { } }
  • 9. Variables in Classes • A class can contain any of the following variable types. • Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed. • Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class. • Class variables − Class variables are variables declared within a class, outside any method, with the static keyword.
  • 10. Objects in Java • A class provides the blueprints for objects. So basically, an object is created from a class. In Java, the new keyword is used to create new objects. • There are three steps when creating an object from a class − – Declaration − A variable declaration with a variable name with an object type. – Instantiation − The 'new' keyword is used to create the object. – Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
  • 11. Example For creating an Object Following is an example of creating an object – public class Puppy { public Puppy(String name) { // This constructor has one parameter, name. System.out.println("Passed Name is :" + name ); } public static void main(String []args) { // Following statement would create an object myPuppy Puppy myPuppy = new Puppy( "tommy" ); } } Output Passed Name is :tommy
  • 12. Advantage of OOPs over Procedure-oriented programming language 1) OOPs makes development and maintenance easier where as in Procedure- oriented programming language it is not easy to manage if code grows as project size grows. 2) OOPs provides data hiding whereas in Procedure-oriented programming language a global data can be accessed from anywhere. 3) OOPs provides ability to simulate real-world event much more effectively. We can provide the solution of real word problem if we are using the Object-Oriented Programming language.
  • 13. Java Reference Variable • Reference variables are used to refer to an object. They are declared with a specific type which cannot be changed. • A reference variable can store the reference value of an object, and can be used to manipulate the object denoted by the reference value. • A variable declaration that specifies a reference type (i.e., a class, an array, or an interface name) declares a reference variable.
  • 14. Object and Reference Variable in Memory • Basically a reference variable just points to an actual object. For example if we have a class- class Person { String name = "Eric"; int age = 25; } If you instantiate the class Person like this- Person p = new Person();
  • 15. • Here p is a reference variable which points to an object of class Person. • The actual object of class Person created in that statement resides on the heap. • The reference variable p only contains the memory address of Person class object on the heap. • So the actual value of p can be said to be an address on the heap.
  • 16. • The String name in class Person is itself a reference variable. So it will point to a String object on the heap.
  • 17. • Here the reference variable p points to an object of type Person. The name reference variable in Person object points to String object on the heap.
  • 18. Uses of reference variable – Reference variable can be assigned value null to show that it is not refering to any object. Student amit = new Student(); amit=null; – More than one reference variables may refer to same object. These are called object aliases. Student amit=new Student(); Student mady=amit; Student mihir=mady; – Default value for refrence variable is null; – The object can be manipulated via any one of its aliases, as each one refers to the same object. – A reference variable can not refer to more then one object.
  • 19. Class Person{ int age; String name; } Class Demo{ public static void main(String args[]) { Person p1=new Person(); p1.age=25; p1.name=“Harry”; System.out.println(p1.age+ “ :” + p1.name); Person p2=p1; System.out.println(p2.age+ “ :” + p2.name); p2.age=30; p2.name=“Garry”; System.out.println(p2.age+ “ :” + p2.name); System.out.println(p1.age+ “ :” + p1.name); p1=null; System.out.println(p1.age+ “ :” + p1.name); // Wrong } }
  • 20. • Reference variable gets memory allocated in stack. • Instance variable gets memory allocated in heap. • Those Reference variable which are instance members of an object, also gets memory allocated in heap.
  • 21. class College{ Student s1; String address; int year; public static void main(String args[]){ College c1=new College(); } } Stack Heap C1 Name=null S1=null Year=0
  • 22. Types of variables • Instance • Class • Local
  • 23. Static Variable Example class test { static int i; public static void main(String[] args) { System.out.println("Value before calling method1: " + i); test t1 = new test(); t1.method1(); System.out.println("Value after calling method1: " + i); t1.method2(); System.out.println("Value after calling method2: " + i); } void method1() { i++; } void method2() { i++;
  • 24. Instance / Local / Method Variable Example class MPE { // Instance Variable int i; public static void main(String[] args) { /*Here i is an Instance variable.*/ test t1 = new test(); System.out.println("Value before calling method1: " + t1.i); } /* Here j is a method parameter. And k is a local variable. Note**: Local variables life is only till the end of method*/ void method1(int j) { int k; i = j; /* Local Variable(k)'s life ends once execution for this method completes. As k is local is variable it needs to be initialized before we can use it. But as it is not getting used here, it can stay here without initializing*/ } }
  • 25. Calling a Method • For using a method, it should be called. There are two ways in which a method is called i.e., method returns a value or returning nothing (no return value). • The process of method calling is simple. When a program invokes a method, the program control gets transferred to the called method. This called method then returns control to the caller in two conditions, when − • the return statement is executed. • it reaches the method ending closing brace.
  • 26. Example FOR CALLING A METHOD public class StudentTest { public static void main ( String[] args ) { Student s1 = new Student () ; Student s2 = new Student ( "Sai", 3 ); Student s3 = new Student ( "Gautham" , 4 , 98 , 100, 96); System.out.println("Student s1: "); s1.printDetails();  Object s1 calling method printDetails() System.out.println("nStudent s2: "); s2.printDetails();  Object s2 calling method printDetails() System.out.println("nStudent s3: "); s3.printDetails();  Object s3 calling method printDetails() }
  • 27. What are Pass-by-value and Pass-by- reference? • Pass-by-value: – A copy of the passed-in variable is copied into the argument of the method. Any changes to the argument do not affect the original one. • Pass-by-reference: – The argument is an alias of the passed-in variable. Any changes to the argument will affect the original one.
  • 28. The following example proves that Java passes object references to methods by value: public class Swap { public static void swap(int x, int y) { int temp = x; x = y; y = temp; System.out.println("x(1) = " + x); System.out.println("y(1) = " + y); } public static void main(String[] args) { int x = 10; int y = 20; swap(x, y); System.out.println("x(2) = " + x); System.out.println("y(2) = " + y); } } • This result proves that the x and y are swapped to each other in the inside the swap() method, however the passed-in variables x and y did not get changed. the above program prints the following output: x(1) = 20 y(1) = 10 x(2) = 10 y(2) = 20
  • 29. Modifying Reference and Changing Reference Examples • Given the Dog class written as below: class Dog { protected String name; Dog(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } }
  • 30. Consider the following method that modifies a Dog reference: public void modifyReference(Dog dog) { dog.setName("Rex"); } • some testing code: Dog dog1 = new Dog("Pun"); System.out.println("Before modify: " + dog1.getName()); modifyReference(dog1); System.out.println("After modify: " + dog1.getName()); • The method argument points to the same Dog object as the passed-in reference variable, The following output: Before modify: Pun After modify: Rex
  • 31. Consider the following method that attempts to change reference of the passed-in parameter: public void changeReference(Dog dog) { Dog newDog = new Dog("Poo"); dog = newDog; } • some testing code: Dog dog2 = new Dog("Meek"); System.out.println("Before change: " + dog2.getName()); tester.changeReference(dog2); System.out.println("After change: " + dog2.getName()); • Since it’s impossible to change reference of a passed-in variable within a method, hence the following output: Before change: Meek After change: Meek Java always passes object references to method by value. That means passing the memory address of the object that the variable points to, not passing the variable itself, not the object itself. So we cannot change reference of a passed-in variable in a method.