Mpl 9 oop

Post on 22-Jan-2017

60 views 0 download

transcript

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.

9

A Simple Classclass Box {

double width;double height;double depth;}

10

Creating ObjectsThere 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 variablemybox = new Box( );

Method 2: Box mybox = new Box( );

• After executing this statement, an object of class Box will be created with the name mybox.

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

18

Program 1class Box {double width;double height;double depth;}

class BoxDemo {public static void main(String args[]) {Box mybox = new Box();double vol;mybox.width = 10;

19

Program1…mybox.height = 20;mybox.depth = 15;vol = mybox.width * mybox.height * mybox.depth;System.out.println("Volume is " + vol);}}

20

Example 2class Box {double width;double height;double depth;}class BoxDemo2 {public static void main(String args[]) {Box mybox1 = new Box();Box mybox2 = new Box();

21

Example 2…double vol;mybox1.width = 10;mybox1.height = 20;mybox1.depth = 15;

mybox2.width = 3;mybox2.height = 6;mybox2.depth = 9;

22

Example 2…// compute volume of first boxvol = mybox1.width * mybox1.height *mybox1.depth;System.out.println("Volume is " + vol);// compute volume of second boxvol = mybox2.width * mybox2.height * mybox2.depth;System.out.println("Volume is " + vol);}}

23

Output of Example 2…The output produced by this program is shown here:Volume is 3000.0Volume is 162.0

24

Adding A Method to The Box ClassSince 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.

25

Adding A Method to The Box Classclass 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();

26

Continue…mybox1.width = 10;mybox1.height = 20;mybox1.depth = 15;

mybox2.width = 3;mybox2.height = 6;mybox2.depth = 9;

mybox1.volume();

mybox2.volume();

}}

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.

28

Returning Value Program…// Now, volume() returns the volume of a box.class Box {double width;double height;double depth;// compute and return volumedouble volume() {return width * height * depth;}}class BoxDemo4 {public static void main(String args[]) {Box mybox1 = new Box();Box mybox2 = new Box();double vol;

29

Returning Value Program…// assign values to mybox1's instance variablesmybox1.width = 10;mybox1.height = 20;mybox1.depth = 15;/* assign different values to mybox2'sinstance variables */mybox2.width = 3;mybox2.height = 6;mybox2.depth = 9;// get volume of first boxvol = mybox1.volume();System.out.println("Volume is " + vol);// get volume of second boxvol = mybox2.volume();System.out.println("Volume is " + vol);}}

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;}

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;}

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:

33

Box Program…// This program uses a parameterized method.class Box {double width;double height;double depth;// compute and return volumedouble volume() {return width * height * depth;}

34

Box Program…// sets dimensions of boxvoid setDim(double w, double h, double d) {width = w;height = h;depth = d;}}

35

Box Program…class BoxDemo5 {public static void main(String args[]) {Box mybox1 = new Box();Box mybox2 = new Box();double vol;// initialize each boxmybox1.setDim(10, 20, 15);mybox2.setDim(3, 6, 9);

36

Box Program…// get volume of first boxvol = mybox1.volume();System.out.println("Volume is " + vol);// get volume of second boxvol = mybox2.volume();System.out.println("Volume is " + vol);}}

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.

38

Exampleclass 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 volumedouble volume() {return width * height * depth;}}

39

Example…class BoxDemo6 {public static void main(String args[]) {// declare, allocate, and initialize Box objectsBox mybox1 = new Box();Box mybox2 = new Box();double vol;// get volume of first boxvol = mybox1.volume();System.out.println("Volume is " + vol);// get volume of second boxvol = mybox2.volume();System.out.println("Volume is " + vol);}}

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.

41

Example Programclass 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 volumedouble volume() {return width * height * depth;}}

42

Example Program…class BoxDemo7 {public static void main(String args[]) {// declare, allocate, and initialize Box objectsBox mybox1 = new Box(10, 20, 15);Box mybox2 = new Box(3, 6, 9);double vol;// get volume of first boxvol = mybox1.volume();System.out.println("Volume is " + vol);// get volume of second boxvol = mybox2.volume();System.out.println("Volume is " + vol);}}

43

OutputThe output from this program is shown here:

Volume is 3000.0Volume is 162.0