SlideShare a Scribd company logo
1 of 43
Object Oriented
Programming-1
What is Object Oriented Programming?
• Object-oriented programming (OOP) involves programming using
classes and objects
• OOP using two mechanisms:
• Classes
• Objects
• How OOP is different from Procedural Programming?
What is Class?
• Class can be defined in the following ways:
• A class is a template for an object
• A class is creating a user defined type
• A class is a blueprint
• A class is a template or blueprint that defines what an object’s data
fields and methods will be
• Class contain:(class members)
• Properties
• Methods
Properties:
• Properties are the attributes of the objects of a class. These are used
to differentiate one class of objects from another class.
• For example a class that represents a pen could be created with the
following attributes:
• Color of the pen
• Size of the pen
• Type of the pen
Methods:
• Methods represent behavior of the class and consist of codes that
operate on data.
• A method is a set of code which is referred to by name and can be
called (invoked) at any point in a program simply by utilizing the
method's name. Think of a method as a subprogram that acts on
data and often returns a value.
• How to define a method, lets understand with the help of simple
example.
How to define a Class in Java?
General Syntax:
class classname
{
type instance-variable1;
type instance-variable2;
type methodname1(parameter-list)
{
statements;
}
}
A Java class uses variables to
define data fields and methods
to define actions.
What is Object?
• Object is an instance of the class, you can create many instances of a
class.
A Simple Class
class Box {
double width;
double height;
double depth;
}
9
Creating Objects
There are two steps to create an object:
Method 1:
1st create an object reference variable:
Box mybox;
2nd physically create an object and assign it to the reference variable
mybox = new Box( );
Method 2:
Box mybox = new Box( );
• After executing this statement, an object of class Box will be created with
the name mybox.
10
11
Object Oriented Program Features
• Encapsulation:
• Combining data and methods into a single unit called class and the process is
known as Encapsulation.
• Data encapsulation is important feature of a class that provide security.
• The insulation of the data from direct access by the program is called data
hiding or information hiding.
• Inheritance:
• The process by which object of one class acquire the properties or features of
objects of another class.
• The concept of inheritance provide the idea of reusability means we can add
additional features to an existing class without Modifying it.
• This is possible by deriving a new class from the existing one. The new class
will have the combined features of both the classes.
• Polymorphism
• A Greek term means ability to take more than one form.
• Function name will be the same, but parameter list will be different
• Java provides two ways to implement polymorphism.
• Static Polymorphism (compile time polymorphism/ Method overloading)
• Dynamic Polymorphism (run time polymorphism/ Method Overriding)
Access Modifiers
• A Java access modifier specifies which classes can access a given class
and its fields, constructors and methods.
• Classes, fields, constructors and methods can have one of four
different Java access modifiers:
• private
• default (package)
• protected
• public
• private Access Modifier
• If a method or variable is marked as private then only code inside
the same class can access the variable, or call the method.
• Code inside subclasses cannot access the variable or method, nor can
code from any external class.
• Private access modifier is not allowed for classes
• default (package) Access Modifier
• The default Java access modifier is declared by not writing any access
modifier at all.
• The default access modifier means that code inside the class itself as
well as code inside classes in the same package as this class, can
access the class, field, constructor or method which the default access
modifier is assigned to.
• protected Access Modifier
• The protected access modifier provides the same access as the default access
modifier, with the addition that subclasses can access protected methods and
member variables (fields) of the superclass.
• This is true even if the subclass is not located in the same package as the
superclass.
• public Access Modifier
• The Java access modifier public means that all code can access the class, field,
constructor or method, regardless of where the accessing code is located.
• The accessing code can be in a different class and different package.
Access Protection
Program 1
class Box {
double width;
double height;
double depth;
}
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
mybox.width = 10;
18
Program1…
mybox.height = 20;
mybox.depth = 15;
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}
19
Example 2
class Box {
double width;
double height;
double depth;
}
class BoxDemo2 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
20
Example 2…
double vol;
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
21
Example 2…
// compute volume of first box
vol = mybox1.width * mybox1.height *mybox1.depth;
System.out.println("Volume is " + vol);
// compute volume of second box
vol = mybox2.width * mybox2.height * mybox2.depth;
System.out.println("Volume is " + vol);
}
}
22
Output of Example 2…
The output produced by this program is shown here:
Volume is 3000.0
Volume is 162.0
23
Adding A Method to The Box Class
Since the volume of a box is dependent upon the size of the box, it
makes sense to have the Box class compute it. To do this, we must add a
method to Box.
24
Adding A Method to The Box Class
class Box {
double width;
double height;
double depth;
void volume() {
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
class BoxDemo3 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
25
Continue…
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
mybox1.volume();
mybox2.volume();
}
}
26
Returning A Value
• While the implementation of volume( ) does move the computation of a box’s
volume inside the Box class where it belongs, it is not the best way to do it.
• For example, what if another part of your program wanted to know the
volume of a box, but not display its value? A better way to implement volume(
) is to have it compute the volume of the box and return the result to the
caller.
• The following example, does just that.
27
Returning Value Program…
// Now, volume() returns the volume of a box.
class Box {
double width;
double height;
double depth;
// compute and return volume
double volume() {
return width * height * depth;
}
}
class BoxDemo4 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
28
Returning Value Program…
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
} 29
Method That Takes Parameter
• Parameters allow a method to be generalized. That is, a parameterized
method can operate on a variety of data and be used in a number of slightly
different situations. To show this point, let’s use a very simple example.
Here is a method that returns the square of the number 10:
int square()
{
return 10 * 10;
}
30
Method That Takes Parameter
• While the previous method does, indeed, return the value of 10 squared, its
use is very limited.
• However, if we modify the method so that it takes a parameter, as shown next,
then we can make square( ) much more useful.
int square(int i)
{
return i * i;
}
31
Box Program
• Thus, a better approach to setting the dimensions of a box is to create a
method that takes the dimension of a box in its parameters and sets each
instance variable appropriately.
• This concept is implemented by the following program:
32
Box Program…
// This program uses a parameterized method.
class Box {
double width;
double height;
double depth;
// compute and return volume
double volume() {
return width * height * depth;
}
33
Box Program…
// sets dimensions of box
void setDim(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
}
34
Box Program…
class BoxDemo5 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// initialize each box
mybox1.setDim(10, 20, 15);
mybox2.setDim(3, 6, 9);
35
Box Program…
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
36
Constructor
• A constructor is a method that executes automatically whenever an object
is created.
• It has the same name as the class in which it is created.
• Constructors have no return type, not even void. This is because the
implicit return type of a class’ constructor is the class type itself.
37
Example
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box() {
System.out.println("Constructing Box");
width = 10;
height = 10;
depth = 10;
}
// compute and return volume
double volume() {
return width * height * depth;
}
} 38
Example…
class BoxDemo6 {
public static void main(String args[]) {
// declare, allocate, and initialize Box objects
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
39
Parameterized Constructors
• While the Box( ) constructor in the previous example does
initialize a Box object, but it is not very useful because all boxes
have the same dimensions.
• Actually we need a way to construct Box objects of various
dimensions. The easy solution is to add parameters to the
constructor.
40
Example Program
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// compute and return volume
double volume() {
return width * height * depth;
}
} 41
Example Program…
class BoxDemo7 {
public static void main(String args[]) {
// declare, allocate, and initialize Box objects
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box(3, 6, 9);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
} 42
Output
The output from this program is shown here:
Volume is 3000.0
Volume is 162.0
43

More Related Content

What's hot

Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming conceptsGanesh Karthik
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Javabackdoor
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceOUM SAOKOSAL
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
 
Unit3 java
Unit3 javaUnit3 java
Unit3 javamrecedu
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritanceKalai Selvi
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc csKALAISELVI P
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ Dev Chauhan
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHPLorna Mitchell
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 

What's hot (20)

Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - Inheritance
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Java Day-7
Java Day-7Java Day-7
Java Day-7
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Unit3 java
Unit3 javaUnit3 java
Unit3 java
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
6 Inheritance
6 Inheritance6 Inheritance
6 Inheritance
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
 
14 Defining Classes
14 Defining Classes14 Defining Classes
14 Defining Classes
 
Java sessionnotes
Java sessionnotesJava sessionnotes
Java sessionnotes
 
Introduction to php oop
Introduction to php oopIntroduction to php oop
Introduction to php oop
 
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc cs
 
Java Day-3
Java Day-3Java Day-3
Java Day-3
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Unit ii
Unit   iiUnit   ii
Unit ii
 

Viewers also liked

A la-ética
A la-éticaA la-ética
A la-éticassergior
 
Khudozhnya kultura-10-klas-klimova
Khudozhnya kultura-10-klas-klimovaKhudozhnya kultura-10-klas-klimova
Khudozhnya kultura-10-klas-klimovafreegdz
 
Geometriya 11-klas-bevz-2011
Geometriya 11-klas-bevz-2011Geometriya 11-klas-bevz-2011
Geometriya 11-klas-bevz-2011freegdz
 
Informatika 10-klas-morze-2010
Informatika 10-klas-morze-2010Informatika 10-klas-morze-2010
Informatika 10-klas-morze-2010freegdz
 
Istoriya ukrajini-10-klas-kulchickijj-lebedehva
Istoriya ukrajini-10-klas-kulchickijj-lebedehvaIstoriya ukrajini-10-klas-kulchickijj-lebedehva
Istoriya ukrajini-10-klas-kulchickijj-lebedehvafreegdz
 
wilder_teaching_statement
wilder_teaching_statementwilder_teaching_statement
wilder_teaching_statementMichael Wilder
 
Українська література 6 клас Авраменко 2014 от Freegdz.com
Українська література 6 клас Авраменко 2014 от Freegdz.comУкраїнська література 6 клас Авраменко 2014 от Freegdz.com
Українська література 6 клас Авраменко 2014 от Freegdz.comfreegdz
 
Location-based Learning
Location-based LearningLocation-based Learning
Location-based LearningMichael Wilder
 
Preprocessor
PreprocessorPreprocessor
PreprocessorVõ Hòa
 
Permendikbud70 2013 kd-strukturkurikulum-smk-mak
Permendikbud70 2013 kd-strukturkurikulum-smk-makPermendikbud70 2013 kd-strukturkurikulum-smk-mak
Permendikbud70 2013 kd-strukturkurikulum-smk-makkadri yusuf
 
Java non access modifiers
Java non access modifiersJava non access modifiers
Java non access modifiersSrinivas Reddy
 

Viewers also liked (16)

A la-ética
A la-éticaA la-ética
A la-ética
 
Khudozhnya kultura-10-klas-klimova
Khudozhnya kultura-10-klas-klimovaKhudozhnya kultura-10-klas-klimova
Khudozhnya kultura-10-klas-klimova
 
Geometriya 11-klas-bevz-2011
Geometriya 11-klas-bevz-2011Geometriya 11-klas-bevz-2011
Geometriya 11-klas-bevz-2011
 
Informatika 10-klas-morze-2010
Informatika 10-klas-morze-2010Informatika 10-klas-morze-2010
Informatika 10-klas-morze-2010
 
Istoriya ukrajini-10-klas-kulchickijj-lebedehva
Istoriya ukrajini-10-klas-kulchickijj-lebedehvaIstoriya ukrajini-10-klas-kulchickijj-lebedehva
Istoriya ukrajini-10-klas-kulchickijj-lebedehva
 
wilder_teaching_statement
wilder_teaching_statementwilder_teaching_statement
wilder_teaching_statement
 
Українська література 6 клас Авраменко 2014 от Freegdz.com
Українська література 6 клас Авраменко 2014 от Freegdz.comУкраїнська література 6 клас Авраменко 2014 от Freegdz.com
Українська література 6 клас Авраменко 2014 от Freegdz.com
 
Location-based Learning
Location-based LearningLocation-based Learning
Location-based Learning
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
299 2015 norme-sicurezza_i
299   2015   norme-sicurezza_i299   2015   norme-sicurezza_i
299 2015 norme-sicurezza_i
 
Permendikbud70 2013 kd-strukturkurikulum-smk-mak
Permendikbud70 2013 kd-strukturkurikulum-smk-makPermendikbud70 2013 kd-strukturkurikulum-smk-mak
Permendikbud70 2013 kd-strukturkurikulum-smk-mak
 
Target audiece profile
Target audiece profileTarget audiece profile
Target audiece profile
 
Media Evaluation Question 5
Media Evaluation Question 5Media Evaluation Question 5
Media Evaluation Question 5
 
Java non access modifiers
Java non access modifiersJava non access modifiers
Java non access modifiers
 
Lesiones pulmonares cavitadas
Lesiones pulmonares cavitadasLesiones pulmonares cavitadas
Lesiones pulmonares cavitadas
 
2017 01-15 Meetup Slides
2017 01-15 Meetup Slides2017 01-15 Meetup Slides
2017 01-15 Meetup Slides
 

Similar to Mpl 9 oop (20)

5.CLASSES.ppt(MB)2022.ppt .
5.CLASSES.ppt(MB)2022.ppt                       .5.CLASSES.ppt(MB)2022.ppt                       .
5.CLASSES.ppt(MB)2022.ppt .
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in java
 
unit 2 java.pptx
unit 2 java.pptxunit 2 java.pptx
unit 2 java.pptx
 
6.INHERITANCE.ppt(MB).ppt .
6.INHERITANCE.ppt(MB).ppt                    .6.INHERITANCE.ppt(MB).ppt                    .
6.INHERITANCE.ppt(MB).ppt .
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
 
Chapter ii(oop)
Chapter ii(oop)Chapter ii(oop)
Chapter ii(oop)
 
7_-_Inheritance
7_-_Inheritance7_-_Inheritance
7_-_Inheritance
 
Class and object
Class and objectClass and object
Class and object
 
gdfgdfg
gdfgdfggdfgdfg
gdfgdfg
 
Super keyword.23
Super keyword.23Super keyword.23
Super keyword.23
 
OOP.ppt
OOP.pptOOP.ppt
OOP.ppt
 
UNIT_-II_2021R.pptx
UNIT_-II_2021R.pptxUNIT_-II_2021R.pptx
UNIT_-II_2021R.pptx
 
OOP C++
OOP C++OOP C++
OOP C++
 
java classes
java classesjava classes
java classes
 
Java-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdf
Java-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdfJava-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdf
Java-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdf
 
Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2
 
Java misc1
Java misc1Java misc1
Java misc1
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 

Recently uploaded

9654467111 Call Girls In Katwaria Sarai Short 1500 Night 6000
9654467111 Call Girls In Katwaria Sarai Short 1500 Night 60009654467111 Call Girls In Katwaria Sarai Short 1500 Night 6000
9654467111 Call Girls In Katwaria Sarai Short 1500 Night 6000Sapana Sha
 
WheelTug PLC Pitch Deck | Investor Insights | April 2024
WheelTug PLC Pitch Deck | Investor Insights | April 2024WheelTug PLC Pitch Deck | Investor Insights | April 2024
WheelTug PLC Pitch Deck | Investor Insights | April 2024Hector Del Castillo, CPM, CPMM
 
The Concept of Humanity in Islam and its effects at future of humanity
The Concept of Humanity in Islam and its effects at future of humanityThe Concept of Humanity in Islam and its effects at future of humanity
The Concept of Humanity in Islam and its effects at future of humanityJohanAspro
 
定制(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
定制(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一定制(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
定制(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一Fir La
 
如何办理密苏里大学堪萨斯分校毕业证(文凭)UMKC学位证书
如何办理密苏里大学堪萨斯分校毕业证(文凭)UMKC学位证书如何办理密苏里大学堪萨斯分校毕业证(文凭)UMKC学位证书
如何办理密苏里大学堪萨斯分校毕业证(文凭)UMKC学位证书Fir La
 
Collective Mining | Corporate Presentation - April 2024
Collective Mining | Corporate Presentation - April 2024Collective Mining | Corporate Presentation - April 2024
Collective Mining | Corporate Presentation - April 2024CollectiveMining1
 
如何办理北卡罗来纳大学教堂山分校毕业证(文凭)UNC学位证书
如何办理北卡罗来纳大学教堂山分校毕业证(文凭)UNC学位证书如何办理北卡罗来纳大学教堂山分校毕业证(文凭)UNC学位证书
如何办理北卡罗来纳大学教堂山分校毕业证(文凭)UNC学位证书Fir La
 
9654467111 Low Rate Call Girls In Tughlakabad, Delhi NCR
9654467111 Low Rate Call Girls In Tughlakabad, Delhi NCR9654467111 Low Rate Call Girls In Tughlakabad, Delhi NCR
9654467111 Low Rate Call Girls In Tughlakabad, Delhi NCRSapana Sha
 
如何办理(UTS毕业证书)悉尼科技大学毕业证学位证书
如何办理(UTS毕业证书)悉尼科技大学毕业证学位证书如何办理(UTS毕业证书)悉尼科技大学毕业证学位证书
如何办理(UTS毕业证书)悉尼科技大学毕业证学位证书Fis s
 
slideshare_2404_presentation materials_en.pdf
slideshare_2404_presentation materials_en.pdfslideshare_2404_presentation materials_en.pdf
slideshare_2404_presentation materials_en.pdfsansanir
 
如何办理东俄勒冈大学毕业证(文凭)EOU学位证书
如何办理东俄勒冈大学毕业证(文凭)EOU学位证书如何办理东俄勒冈大学毕业证(文凭)EOU学位证书
如何办理东俄勒冈大学毕业证(文凭)EOU学位证书Fir La
 
Osisko Gold Royalties Ltd - Corporate Presentation, April 23, 2024
Osisko Gold Royalties Ltd - Corporate Presentation, April 23, 2024Osisko Gold Royalties Ltd - Corporate Presentation, April 23, 2024
Osisko Gold Royalties Ltd - Corporate Presentation, April 23, 2024Osisko Gold Royalties Ltd
 
Basic Accountants in|TaxlinkConcept.pdf
Basic  Accountants in|TaxlinkConcept.pdfBasic  Accountants in|TaxlinkConcept.pdf
Basic Accountants in|TaxlinkConcept.pdftaxlinkcpa
 
Collective Mining | Corporate Presentation - April 2024
Collective Mining | Corporate Presentation - April 2024Collective Mining | Corporate Presentation - April 2024
Collective Mining | Corporate Presentation - April 2024CollectiveMining1
 

Recently uploaded (20)

young Call girls in Dwarka sector 1🔝 9953056974 🔝 Delhi escort Service
young Call girls in Dwarka sector 1🔝 9953056974 🔝 Delhi escort Serviceyoung Call girls in Dwarka sector 1🔝 9953056974 🔝 Delhi escort Service
young Call girls in Dwarka sector 1🔝 9953056974 🔝 Delhi escort Service
 
Model Call Girl in Uttam Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Uttam Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Uttam Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Uttam Nagar Delhi reach out to us at 🔝9953056974🔝
 
9654467111 Call Girls In Katwaria Sarai Short 1500 Night 6000
9654467111 Call Girls In Katwaria Sarai Short 1500 Night 60009654467111 Call Girls In Katwaria Sarai Short 1500 Night 6000
9654467111 Call Girls In Katwaria Sarai Short 1500 Night 6000
 
Call Girls in South Ex⎝⎝9953056974⎝⎝ Escort Delhi NCR
Call Girls in South Ex⎝⎝9953056974⎝⎝ Escort Delhi NCRCall Girls in South Ex⎝⎝9953056974⎝⎝ Escort Delhi NCR
Call Girls in South Ex⎝⎝9953056974⎝⎝ Escort Delhi NCR
 
WheelTug PLC Pitch Deck | Investor Insights | April 2024
WheelTug PLC Pitch Deck | Investor Insights | April 2024WheelTug PLC Pitch Deck | Investor Insights | April 2024
WheelTug PLC Pitch Deck | Investor Insights | April 2024
 
young call girls in Yamuna Vihar 🔝 9953056974 🔝 Delhi escort Service
young  call girls in   Yamuna Vihar 🔝 9953056974 🔝 Delhi escort Serviceyoung  call girls in   Yamuna Vihar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Yamuna Vihar 🔝 9953056974 🔝 Delhi escort Service
 
The Concept of Humanity in Islam and its effects at future of humanity
The Concept of Humanity in Islam and its effects at future of humanityThe Concept of Humanity in Islam and its effects at future of humanity
The Concept of Humanity in Islam and its effects at future of humanity
 
定制(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
定制(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一定制(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
定制(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
 
如何办理密苏里大学堪萨斯分校毕业证(文凭)UMKC学位证书
如何办理密苏里大学堪萨斯分校毕业证(文凭)UMKC学位证书如何办理密苏里大学堪萨斯分校毕业证(文凭)UMKC学位证书
如何办理密苏里大学堪萨斯分校毕业证(文凭)UMKC学位证书
 
Collective Mining | Corporate Presentation - April 2024
Collective Mining | Corporate Presentation - April 2024Collective Mining | Corporate Presentation - April 2024
Collective Mining | Corporate Presentation - April 2024
 
如何办理北卡罗来纳大学教堂山分校毕业证(文凭)UNC学位证书
如何办理北卡罗来纳大学教堂山分校毕业证(文凭)UNC学位证书如何办理北卡罗来纳大学教堂山分校毕业证(文凭)UNC学位证书
如何办理北卡罗来纳大学教堂山分校毕业证(文凭)UNC学位证书
 
9654467111 Low Rate Call Girls In Tughlakabad, Delhi NCR
9654467111 Low Rate Call Girls In Tughlakabad, Delhi NCR9654467111 Low Rate Call Girls In Tughlakabad, Delhi NCR
9654467111 Low Rate Call Girls In Tughlakabad, Delhi NCR
 
如何办理(UTS毕业证书)悉尼科技大学毕业证学位证书
如何办理(UTS毕业证书)悉尼科技大学毕业证学位证书如何办理(UTS毕业证书)悉尼科技大学毕业证学位证书
如何办理(UTS毕业证书)悉尼科技大学毕业证学位证书
 
slideshare_2404_presentation materials_en.pdf
slideshare_2404_presentation materials_en.pdfslideshare_2404_presentation materials_en.pdf
slideshare_2404_presentation materials_en.pdf
 
如何办理东俄勒冈大学毕业证(文凭)EOU学位证书
如何办理东俄勒冈大学毕业证(文凭)EOU学位证书如何办理东俄勒冈大学毕业证(文凭)EOU学位证书
如何办理东俄勒冈大学毕业证(文凭)EOU学位证书
 
young call girls in Hauz Khas,🔝 9953056974 🔝 escort Service
young call girls in Hauz Khas,🔝 9953056974 🔝 escort Serviceyoung call girls in Hauz Khas,🔝 9953056974 🔝 escort Service
young call girls in Hauz Khas,🔝 9953056974 🔝 escort Service
 
Osisko Gold Royalties Ltd - Corporate Presentation, April 23, 2024
Osisko Gold Royalties Ltd - Corporate Presentation, April 23, 2024Osisko Gold Royalties Ltd - Corporate Presentation, April 23, 2024
Osisko Gold Royalties Ltd - Corporate Presentation, April 23, 2024
 
Model Call Girl in Udyog Vihar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Udyog Vihar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Udyog Vihar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Udyog Vihar Delhi reach out to us at 🔝9953056974🔝
 
Basic Accountants in|TaxlinkConcept.pdf
Basic  Accountants in|TaxlinkConcept.pdfBasic  Accountants in|TaxlinkConcept.pdf
Basic Accountants in|TaxlinkConcept.pdf
 
Collective Mining | Corporate Presentation - April 2024
Collective Mining | Corporate Presentation - April 2024Collective Mining | Corporate Presentation - April 2024
Collective Mining | Corporate Presentation - April 2024
 

Mpl 9 oop

  • 2. What is Object Oriented Programming? • Object-oriented programming (OOP) involves programming using classes and objects • OOP using two mechanisms: • Classes • Objects • How OOP is different from Procedural Programming?
  • 3. What is Class? • Class can be defined in the following ways: • A class is a template for an object • A class is creating a user defined type • A class is a blueprint • A class is a template or blueprint that defines what an object’s data fields and methods will be • Class contain:(class members) • Properties • Methods
  • 4. Properties: • Properties are the attributes of the objects of a class. These are used to differentiate one class of objects from another class. • For example a class that represents a pen could be created with the following attributes: • Color of the pen • Size of the pen • Type of the pen
  • 5. Methods: • Methods represent behavior of the class and consist of codes that operate on data. • A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. Think of a method as a subprogram that acts on data and often returns a value. • How to define a method, lets understand with the help of simple example.
  • 6.
  • 7. How to define a Class in Java? General Syntax: class classname { type instance-variable1; type instance-variable2; type methodname1(parameter-list) { statements; } } A Java class uses variables to define data fields and methods to define actions.
  • 8. What is Object? • Object is an instance of the class, you can create many instances of a class.
  • 9. A Simple Class class Box { double width; double height; double depth; } 9
  • 10. Creating Objects There are two steps to create an object: Method 1: 1st create an object reference variable: Box mybox; 2nd physically create an object and assign it to the reference variable mybox = new Box( ); Method 2: Box mybox = new Box( ); • After executing this statement, an object of class Box will be created with the name mybox. 10
  • 11. 11
  • 12. Object Oriented Program Features • Encapsulation: • Combining data and methods into a single unit called class and the process is known as Encapsulation. • Data encapsulation is important feature of a class that provide security. • The insulation of the data from direct access by the program is called data hiding or information hiding.
  • 13. • Inheritance: • The process by which object of one class acquire the properties or features of objects of another class. • The concept of inheritance provide the idea of reusability means we can add additional features to an existing class without Modifying it. • This is possible by deriving a new class from the existing one. The new class will have the combined features of both the classes. • Polymorphism • A Greek term means ability to take more than one form. • Function name will be the same, but parameter list will be different • Java provides two ways to implement polymorphism. • Static Polymorphism (compile time polymorphism/ Method overloading) • Dynamic Polymorphism (run time polymorphism/ Method Overriding)
  • 14. Access Modifiers • A Java access modifier specifies which classes can access a given class and its fields, constructors and methods. • Classes, fields, constructors and methods can have one of four different Java access modifiers: • private • default (package) • protected • public
  • 15. • private Access Modifier • If a method or variable is marked as private then only code inside the same class can access the variable, or call the method. • Code inside subclasses cannot access the variable or method, nor can code from any external class. • Private access modifier is not allowed for classes • default (package) Access Modifier • The default Java access modifier is declared by not writing any access modifier at all. • The default access modifier means that code inside the class itself as well as code inside classes in the same package as this class, can access the class, field, constructor or method which the default access modifier is assigned to.
  • 16. • protected Access Modifier • The protected access modifier provides the same access as the default access modifier, with the addition that subclasses can access protected methods and member variables (fields) of the superclass. • This is true even if the subclass is not located in the same package as the superclass. • public Access Modifier • The Java access modifier public means that all code can access the class, field, constructor or method, regardless of where the accessing code is located. • The accessing code can be in a different class and different package.
  • 18. Program 1 class Box { double width; double height; double depth; } class BoxDemo { public static void main(String args[]) { Box mybox = new Box(); double vol; mybox.width = 10; 18
  • 19. Program1… mybox.height = 20; mybox.depth = 15; vol = mybox.width * mybox.height * mybox.depth; System.out.println("Volume is " + vol); } } 19
  • 20. Example 2 class Box { double width; double height; double depth; } class BoxDemo2 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); 20
  • 21. Example 2… double vol; mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15; mybox2.width = 3; mybox2.height = 6; mybox2.depth = 9; 21
  • 22. Example 2… // compute volume of first box vol = mybox1.width * mybox1.height *mybox1.depth; System.out.println("Volume is " + vol); // compute volume of second box vol = mybox2.width * mybox2.height * mybox2.depth; System.out.println("Volume is " + vol); } } 22
  • 23. Output of Example 2… The output produced by this program is shown here: Volume is 3000.0 Volume is 162.0 23
  • 24. Adding A Method to The Box Class Since the volume of a box is dependent upon the size of the box, it makes sense to have the Box class compute it. To do this, we must add a method to Box. 24
  • 25. Adding A Method to The Box Class class Box { double width; double height; double depth; void volume() { System.out.print("Volume is "); System.out.println(width * height * depth); } } class BoxDemo3 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); 25
  • 26. Continue… mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15; mybox2.width = 3; mybox2.height = 6; mybox2.depth = 9; mybox1.volume(); mybox2.volume(); } } 26
  • 27. Returning A Value • While the implementation of volume( ) does move the computation of a box’s volume inside the Box class where it belongs, it is not the best way to do it. • For example, what if another part of your program wanted to know the volume of a box, but not display its value? A better way to implement volume( ) is to have it compute the volume of the box and return the result to the caller. • The following example, does just that. 27
  • 28. Returning Value Program… // Now, volume() returns the volume of a box. class Box { double width; double height; double depth; // compute and return volume double volume() { return width * height * depth; } } class BoxDemo4 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; 28
  • 29. Returning Value Program… // assign values to mybox1's instance variables mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15; /* assign different values to mybox2's instance variables */ mybox2.width = 3; mybox2.height = 6; mybox2.depth = 9; // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); } } 29
  • 30. Method That Takes Parameter • Parameters allow a method to be generalized. That is, a parameterized method can operate on a variety of data and be used in a number of slightly different situations. To show this point, let’s use a very simple example. Here is a method that returns the square of the number 10: int square() { return 10 * 10; } 30
  • 31. Method That Takes Parameter • While the previous method does, indeed, return the value of 10 squared, its use is very limited. • However, if we modify the method so that it takes a parameter, as shown next, then we can make square( ) much more useful. int square(int i) { return i * i; } 31
  • 32. Box Program • Thus, a better approach to setting the dimensions of a box is to create a method that takes the dimension of a box in its parameters and sets each instance variable appropriately. • This concept is implemented by the following program: 32
  • 33. Box Program… // This program uses a parameterized method. class Box { double width; double height; double depth; // compute and return volume double volume() { return width * height * depth; } 33
  • 34. Box Program… // sets dimensions of box void setDim(double w, double h, double d) { width = w; height = h; depth = d; } } 34
  • 35. Box Program… class BoxDemo5 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; // initialize each box mybox1.setDim(10, 20, 15); mybox2.setDim(3, 6, 9); 35
  • 36. Box Program… // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); } } 36
  • 37. Constructor • A constructor is a method that executes automatically whenever an object is created. • It has the same name as the class in which it is created. • Constructors have no return type, not even void. This is because the implicit return type of a class’ constructor is the class type itself. 37
  • 38. Example class Box { double width; double height; double depth; // This is the constructor for Box. Box() { System.out.println("Constructing Box"); width = 10; height = 10; depth = 10; } // compute and return volume double volume() { return width * height * depth; } } 38
  • 39. Example… class BoxDemo6 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); } } 39
  • 40. Parameterized Constructors • While the Box( ) constructor in the previous example does initialize a Box object, but it is not very useful because all boxes have the same dimensions. • Actually we need a way to construct Box objects of various dimensions. The easy solution is to add parameters to the constructor. 40
  • 41. Example Program class Box { double width; double height; double depth; // This is the constructor for Box. Box(double w, double h, double d) { width = w; height = h; depth = d; } // compute and return volume double volume() { return width * height * depth; } } 41
  • 42. Example Program… class BoxDemo7 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(3, 6, 9); double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); } } 42
  • 43. Output The output from this program is shown here: Volume is 3000.0 Volume is 162.0 43