Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP Features Object Oriented Programming Main issues in software engineering – –maintainability, reusability, portability, security, integrity, user.

Similar presentations


Presentation on theme: "OOP Features Object Oriented Programming Main issues in software engineering – –maintainability, reusability, portability, security, integrity, user."— Presentation transcript:

1

2 OOP Features

3 Object Oriented Programming Main issues in software engineering – –maintainability, reusability, portability, security, integrity, user friendliness etc. Software become complex. –Programs structure should be easy to implement and modify in a wide variety of situations. Many programming approaches are tried –Modular programming –Top-down programming –Bottom-up programming –Structure programming

4 Programming Approaches Modular programming - –program is broken into several independently compiled modules. –Modular programming is a precursor of object-oriented programming. Top-down programming - –implements a program in top-down fashion. –Typically, this is done by writing a main body with calls to several major routines –Each routine is then coded, calling other, lower-level, routines

5 Programming Approaches Bottom-up programming – –a technique in which lower-level functions are developed and tested first; –higher-level functions are then built using the lower-level functions, and so on. Structure programming – –statements are organized in a specific manner to minimize error or misinterpretation.

6 OOP Object oriented programming is an approach that provides – –a way of modularizing programs by creating partitioned memory area for both data and functions –templates for creating copies of such modules on demand. –e.g. Smalltalk, Ada, C++, Objective C, Delphi, Java. Data Method Object = data + method

7 Different Paradigms in OOP In order to achieve many facilities, OOP adopts the following paradigms: Data abstraction- –facility for the users to define their objects Encapsulation- –definition of an object (physical view) and various functions for communicating with other objects (behavioral view) can be fit into together. –Wrapping data and code in single unit. Data hiding/information hiding- –different data/functions can be protected from unauthorized access, –i.e. data and function can be kept secret

8 Different Paradigms in OOP Polymorphism- –means ‘many forms’ –expressed by ‘one interface, multiple method’ –allows one interface to be used for a general class of actions, which is determined by nature of situation. e.g method overloading, method overriding Inheritance- –the usual features in real world which will allow a programmer to reuse and share data as well as code, in hierarchical manner.

9 Object-Oriented Programming Concepts and Java  Concepts related to object-oriented programming (JAVA) include – –Objects –Encapsulation and message passing –Classes –Libraries (packages in Java) –Inheritance –Access modifiers

10 Data Abstraction In Java there are three types of data values: –primitive data values (int, double, boolean, etc.) –arrays (actually a special type of object) –objects Objects in a program are used to represent "real" (and sometimes not so real) objects from the world around us.

11 Constructed Types: Classes Class is a data type/template/design of an object. –Declaration of a Java class shares many common aspects with classes in C++ Java only supports single inheritance –Effect of multiple inheritance may be achieved through the use of the interface construct. A class may be declared to be abstract - No method implementations provided - All derived classes must provide implementations for all methods defined in the abstract class

12 Constructed Types: Classes Java classes may declare both variables and methods –Variable defined inside of a class is an instance variable - one copy per object –Class methods invoked by applying them to an instance of a class object e.g. ob1.add(x, y)

13 Objects Object is an instance of a class. An object might represent –a string of characters, a planet, a type of food, a student, an employee, a piece of email,... anything that can't be (easily) represented by a primitive value or an array. –Just as ‘3’ is a primitive value of type int, every object must also have a type. –These types are called classes.

14 Classes A class describes a set of objects. –specifies what information will be used to represent an object from the set e.g. name and salary for an employee –specifies what operations can be performed on such an object e.g getName( )// the name of the student sendMail( )// send an email message

15 Elements of a Simple Class A class describes the data values used to represent an object and any operations that can be performed on that object. –The data values are stored in instance variables, also known as fields, or data members. –The operations are described by instance methods, sometimes called procedure members.

16 Class Definition The general form of class definition: class ClassName [extends SuperClassName ] [implements Interface ] { [declaration of member elements] [declaration of methods] } nb: Those included with […] are optional.

17 class An empty class could be: class Empty{ //………… }

18 class Box class Box { double width; double height; double depth; } Box mybox = new Box(); // create a Box object called mybox

19 BoxDemo.java class Box { double width; double height; double depth; } // This class declares an object of type Box. class BoxDemo { public static void main(String args[]) { Box mybox; // type declaration mybox = new Box();// instantiation double vol; // assign values to mybox's instance variables mybox.width = 10; mybox.height = 20; mybox.depth = 15; // compute volume of box vol = mybox.width * mybox.height * mybox.depth; System.out.println("Volume is " + vol); }

20 BoxDemo2.java class Box { double width; double height; double depth; } class BoxDemo2 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; // 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; // 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); }

21 Declaring Objects Obtaining an Object is a two-step process –First, declare a variable of any class type –Doesn’t define an object, a variable to refer an object Box mybox; –Second, acquire a physical copy of that object and assign it to that variable, using new operator mybox = new Box ( ); null Width Height Depth mybox

22 Assigning Object Reference Box b1 = new Box( ); Box b2 = b1; Now, if – b1 = null; Here, b1 is set to null, but b2 still points to the original object Width Height Depth b1 b2

23 Adding method // This program includes a method inside the box class. class Box { double width; double height; double depth; // display volume of a box void volume() { System.out.print("Volume is "); System.out.println(width * height * depth); }

24 class BoxDemo3 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); // 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; // display volume of first box mybox1.volume(); // display volume of second box mybox2.volume(); }

25 Returning value by method // 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; }

26 class BoxDemo4 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; // 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); }

27 Adding methods with parameter class Box { double width; double height; double depth; // compute and return volume double volume() { return width * height * depth; } // sets dimensions of box void setDim(double w, double h, double d) { width = w; height = h; depth = d; }

28 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); // 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 Constructor Constructor initializes an object upon creation. It has the same name as the class resides Box mybox = new Box( ); //new Box( ), is calling Box( ) constructor. If you don’t define a constructor, Java creates a default constructor. Default constructor automatically initializes all variables to ‘zero’.

30 Constructor /* Here, Box uses a constructor to initialize the dimensions of a box. */ 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; }

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

32 Parameterized Constructor /* Here, Box uses a parameterized constructor to initialize the dimensions of a box. */ 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; }

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

34 Use of this Sometimes a method need to refer the object that invoked it. –Allowing this Java provide a way to refer current object from inside the method. –Can be used this to resolve name-space collisions. Between ‘local variables’ & ‘parameters’

35 Use of this Box(double width, double height, double depth) { this.width = width; this.height = height; this.depth = depth; }

36 Method Overloading It is possible to define two or more methods within same class – –Share the same name. –Their parameters declaration will be different. –One ways of implementing ‘polymorphism’ –When an overloaded method invoked, Java uses type & number of arguments to determine, which one will be called. –Overloaded methods may have different return types, but this is not enough to distinguish two versions.

37 // Demonstrate method overloading. class OverloadDemo { void test() { System.out.println("No parameters"); } // Overload test for one integer parameter. void test(int a) { System.out.println("a: " + a); } // Overload test for two integer parameters. void test(int a, int b) { System.out.println("a and b: " + a + " " + b); } // overload test for a double parameter double test(double a) { System.out.println("double a: " + a); return a*a; }

38 class Overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); double result; // call all versions of test() ob.test(); ob.test(10); ob.test(10, 20); result = ob.test(123.25); System.out.println("Result of ob.test(123.25): " + result); }

39 // Automatic type conversions apply to overloading. class OverloadDemo { void test() { System.out.println("No parameters"); } // Overload test for two integer parameters. void test(int a, int b) { System.out.println("a and b: " + a + " " + b); } // overload test for a double parameter and return type void test(double a) { System.out.println("Inside test(double) a: " + a); } Automatic type conversion

40 class Overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); int i = 88; ob.test(); ob.test(10, 20); ob.test(i); // this will invoke test(double) ob.test(123.2); // this will invoke test(double) }

41 Method overloading supports polymorphism – –Java implements ‘one interface, multiple methods’ –In ‘C’ language, don’t support overloading for ‘abs’ function, they need all types of function for each input type, abs(), fabs(), labs() etc. Lots of name is required, even though they perform same operation. –In ‘Java’, its ‘Math’ class handles all types of numeric types using ‘abs()’ method Method Overloading

42 Constructor Overloading

43 /* Here, Box defines three constructors to initialize the dimensions of a box various ways. */ class Box { double width; double height; double depth; // constructor used when all dimensions specified Box(double w, double h, double d) { width = w; height = h; depth = d; } // constructor used when no dimensions specified Box() { width = -1; // use -1 to indicate height = -1; // an uninitialized depth = -1; // box } // constructor used when cube is created Box(double len) { width = height = depth = len; } // compute and return volume double volume() { return width * height * depth; }

44 class OverloadCons { public static void main(String args[]) { // create boxes using the various constructors Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(); Box mycube = new Box(7); double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume of mybox1 is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume of mybox2 is " + vol); // get volume of cube vol = mycube.volume(); System.out.println("Volume of mycube is " + vol); }

45 Using objects as Parameter // Here, Box allows one object to initialize another. class Box { double width; double height; double depth; // construct clone of an object Box(Box ob) { // pass object to constructor width = ob.width; height = ob.height; depth = ob.depth; } // constructor used when all dimensions specified Box(double w, double h, double d) { width = w; height = h; depth = d; }

46 // constructor used when no dimensions specified Box() { width = -1; // use -1 to indicate height = -1; // an uninitialized depth = -1; // box } // constructor used when cube is created Box(double len) { width = height = depth = len; } // compute and return volume double volume() { return width * height * depth; }

47 class OverloadCons2 { public static void main(String args[]) { // create boxes using the various constructors Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(); Box mycube = new Box(7); Box myclone = new Box(mybox1); double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume of mybox1 is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume of mybox2 is " + vol); // get volume of cube vol = mycube.volume(); System.out.println("Volume of cube is " + vol); // get volume of clone vol = myclone.volume(); System.out.println("Volume of clone is " + vol); }

48 Call by value // Simple Types are passed by value. class Test { void meth(int i, int j) { i *= 2; j /= 2; } class CallByValue { public static void main(String args[]) { Test ob = new Test(); int a = 15, b = 20; System.out.println("a and b before call: " + a + " " + b); ob.meth(a, b); System.out.println("a and b after call: " + a + " " + b); } a and b before call: 15 20 a and b after call: 15 20

49 Call by Reference /* Objects are passed by reference. */ class Test { int a, b; Test(int i, int j) { a = i; b = j; }//const. // pass an object void meth(Test o) { o.a *= 2; o.b /= 2; }//meth }//class class CallByRef { public static void main(String args[]) { Test ob = new Test(15, 20); System.out.println("ob.a and ob.b before call: " + ob.a + " " + ob.b); ob.meth(ob); System.out.println("ob.a and ob.b after call: " +ob.a + " " + ob.b); }//main }//class ob.a and ob.b before call: 15 20 ob.a and ob.b before call: 30 10

50 Returning an object. class Test { int a; Test(int i) { a = i; } Test incrByTen() { Test temp = new Test(a+10); return temp; } class RetOb { public static void main(String args[]) { Test ob1 = new Test(2); Test ob2; ob2 = ob1.incrByTen(); System.out.println("ob1.a: " + ob1.a); System.out.println("ob2.a: " + ob2.a); ob2 = ob2.incrByTen(); System.out.println("ob2.a after second increase: “ + ob2.a); } }//class

51 Introducing access control Encapsulation provides access control. How a member can be accessed, determined by access specifiers –Public – can be accessed by any other code in the program. –Private – can be accessed by other members in the class –Default – when no specifier is declared, by default the member is public within own package. –Protected – used in inheritance [discussed later]

52 public int i; private double j; private int myMethod(int a, char b) { //... } Access Specifier An access specifier precedes the member’s type specification –

53 /* This program demonstrates the difference between public and private. */ class Test { int a; // default access public int b; // public access private int c; // private access // methods to access c void setc(int i) { // set c's value c = i; } int getc() { // get c's value return c; } class AccessTest { public static void main(String args[]) { Test ob = new Test(); // These are OK, a and b may be accessed directly ob.a = 10; ob.b = 20; // This is not OK and will cause an error ob.c = 100; // Error! // You must access c through its methods ob.setc(100); // OK System.out.println("a, b, and c: " + ob.a + " " + ob.b + " " + ob.getc()); }

54 Use of ‘static’ There sometimes needs to define a member to be used independently of any object. –It is possible to create a member that can be used without the reference to object. Precede the declaration with ‘static’ keyword. Both methods and variables can be static

55 Use of ‘static’ Instance variables declared as static are – –Global variables –When objects of its class are declared, no copy of a static variable is made. –All objects of class share the same static variable. Methods declared as static has restrictions – –They can only call other static methods. –They must access static data. –They can’t refer to ‘this’ or ‘super’ in any way. Static block can be declared which will execute exactly once –When the class will be loaded first.

56 class UseStatic { static int a = 3; static int b; static void meth(int x) { System.out.println("x = " + x); System.out.println("a = " + a); System.out.println("b = " + b); } static { System.out.println("Static block initialized."); b = a * 4; } public static void main(String args[]) { meth(42); } Static block initialized x = 42 a = 3 b = 12

57 class StaticDemo { static int a = 42; static int b = 99; static void callme() { System.out.println("a = " + a); } class StaticByName { public static void main(String args[]) { StaticDemo.callme(); System.out.println("b = " + StaticDemo.b); }

58 Introducing ‘final’ Variable declared as ‘final’ prevents its contents from being modified. –Similar to ‘const’ in C/C++ Its common to choose uppercase identifiers for final variables. final int COUNT = 5;


Download ppt "OOP Features Object Oriented Programming Main issues in software engineering – –maintainability, reusability, portability, security, integrity, user."

Similar presentations


Ads by Google