1 / 44

IS F213 Object Oriented Programming

IS F213 Object Oriented Programming. Dr. Yashvardhan Sharma CSIS Dept., BITS-Pilani. Objects as Parameters To Methods.

adamma
Download Presentation

IS F213 Object Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IS F213 Object Oriented Programming Dr. Yashvardhan Sharma CSIS Dept., BITS-Pilani

  2. Objects as Parameters To Methods • Objects are always passed by reference w.r.t its instance field values i.e state of the object. ( State of object Can be changed by using the same reference variable ) • If you are changing the values of instance fields by using same reference variable then changes will be reflected in the calling program. [ That’s what we mean by callByReference ] • Objects are always passed by value w.r.t. its reference. The Called Method can not change the passed parameter reference to some another object . The called method if tries to change the reference of passed parameter then that change remains local only and will not be reflected to calling method.

  3. Example1 (Objects as parameters to methods) void changeContents(A other) { other.a = 20; other.b = 100; } void changeReference(A other) { other = new A(-10,-20); other.a = 20; other.b = 100; } } // End of class A class A { int a,b; A(int a,int b) {this.a =a; this.b=b;} void show() { System.out.println("a="+a); System.out.println("b="+b); }

  4. a b a1 40 50 Objects as Parameters Methods class objref { public static void main(String args[]) { A a1=new A(40,50); a1.show(); a1.changeReference(a1); a1.show(); } } E:\New Folder\Java>java objref a=40 b=50 a=40 b=50

  5. a b a1 40 50 Objects as Parameters Methods class objref { public static void main(String args[]) { A a1=new A(40,50); a1.show(); a1.changeContents(a1); a1.show(); } } other E:\New Folder\Java>java objref a=40 b=50 a=20 b=100 changeContents(A other) Method does not change the reference for other

  6. Changing other name value with this name value [ State Change] By Reference w.r.t State Changing other reference to another greeter [ Reference Change] By value w.r.t to reference change TEXT BOOK EXAMPLE class greeter { String name; greeter(String name) { this.name = name; } String sayHello() { return "Hello"+name+"!"; } void copyNameTo(greeter other) { other.name = this.name; } void copyGreeterTo(greeter other) { other = new greeter(“Ram"); } }

  7. class greetertest { public static void main(String args[]) { greeter worldgreeter = new greeter("World"); greeter davegreeter = new greeter("Dave"); System.out.println(worldgreeter.name); System.out.println(davegreeter.name); worldgreeter.copyNameTo(davegreeter); System.out.println(worldgreeter.name); System.out.println(davegreeter.name); greeter g1 = new greeter("KAMAL"); greeter g2 = new greeter("SHARMA"); System.out.println(g1.name); System.out.println(g2.name); g1.copyGreeterTo(g2); System.out.println(g1.name); System.out.println(g2.name); } }

  8. class greeter { String name; greeter(String name) { this.name = name; } void setName(String name) { this.name = name; } void copyNameTo(greeter other) { other.name = this.name; } void copyLengthTo(int n) { n = name.length(); } TEXT BOOK PROBLEM 1.13, 1.14 void copyGreeterTo(greeter other) { other = new greeter("Pankaj Vyas"); } void swapNames(greeter other) { String temp = other.name; other.name = this.name; this.name = temp; } void swapReference(greeter other) { greeter temp = other; other = this; this = temp; } } this is final reference. Cannot be changed by method swapReference Cont…..

  9. class greetertest { public static void main(String args[]) { greeter g1 = new greeter("OOP"); g1.setName("BITS"); greeter g2 = new greeter("KAMAL"); g1.copyNameTo(g2); } } Continue Problem…… D:\Java1>javac greetertest.java greetertest.java:34: cannot assign a value to final variable this this = temp; ^ 1 error

  10. class BOX { private double length,width,height; BOX(double l,double b,double h) { length = l; width = b; height =h; } // Mutator Methods public void setLength(double l) { this.length = l;} public void setWidth(double b) { this.width = b; } public void setHeight(double h) { this.height = h;} // Accessor Methods public double getLength() { return this.length;} public double getWidth() { return this.width; } public double getHeight() { return this.height;} void showDimension() { System.out.println("Length:"+this.length); System.out.println("Width:"+this.width); System.out.println("Height:"+this.height); }

  11. double area() { return 2 * (length * width + width*height + height*length);} double volume() { return length*width*height ;} void swapBoxes(BOX b1, BOX b2) { BOX temp = b1; b1 = b2; b2 = temp; } }// End of BOX

  12. class BOXtest { public static void main(String args[]) { BOX b1 = new BOX(10,40,60); BOX b2 = new BOX(20,30,80); BOX b3 = new BOX(100,200,300); b1.showDimension(); b2.showDimension(); b3.swapBoxes(b1,b2); b1.showDimension(); b2.showDimension(); } }//End of Main D:\java\bin>java BOXtest Length:10.0 Width:40.0 Height:60.0 Length:20.0 Width:30.0 Height:80.0 Length:10.0 Width:40.0 Height:60.0 Length:20.0 Width:30.0 Height:80.0

  13. class BOXtest { public static void main(String args[]) { BOX b1 = new BOX(10,40,60); BOX b2 = new BOX(20,30,80); BOX b3 = new BOX(100,200,300); b1.showDimension(); b2.showDimension(); // b3.swapBoxes(b1,b2); BOX temp = b1; b1 = b2; b2 = temp; b1.showDimension(); b2.showDimension(); } }//End of Main D:\java\bin>java BOXtest Length:10.0 Width:40.0 Height:60.0 Length:20.0 Width:30.0 Height:80.0 Length:20.0 Width:30.0 Height:80.0 Length:10.0 Width:40.0 Height:60.0

  14. Reading Inputs in Java • Use BufferedReader (Use io package [import java.io.*;]) • throws IOException • BufferedReaderbr = new BufferedReader(new InputStreamReader(System.in))); • String str = br.readLine(); • int n = Integer.parseInt(str); • float f = Float.parseFloat(str); • double a = Double.parseDouble(str);

  15. Point x: double y: double +getX() : double +getY() : double +setX(x: double) : void +setY(y: double) : void +equals(other : Point) : boolean +computeDistance(other : Point) : double +show() : void UML Representation for Class Point class Point { double x; // x – coordinate double y; // y –coordinate public double getX() { return x; } public double getY() { return y; } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } public boolean equals(Point other) { return this.x == other. x && this.y == other.y ; } public double computeDistance(Point other) { double a = (this.y – other.y) * (this.y – other.y); double b = this.x – other.x) * (this.x – other.x); return Math.sqrt(a+b); } Attributes Operations public void show() { S.O.P(“ x= “+x); S.O.P(“ y= “+y); } } End of Point Class

  16. Class PointTest class PointTest { public static void main(String args[ ]) { Point P1 = new Point(); P1.show(); Point P2 = P1; P2.show(); System.out.println(P1.equals(P2)); System.out.println(P1.computeDistance(P2)); }// End of main() Method }// End of PointTest

  17. OBJECT CONTAINMENT • When one class contains instance of other class as its instance field then it is known as object containment. • Object Containment defines one of the important concepts of UML known as composition or aggregation or a-part-of or has a relationship Player Team Aggregation Engine Car Composition

  18. Point Line Example Object Containment Line class class Line { private Point x; private Point y; private double length; Line(Point x,Point y) { this.x = x; this.y = y; length = x.computeDistance(y); } Line(double x1, double y1, double x2, double y2) { x = new Point(x1,y1); y = new Point(x2,y2); length = x.computeDistance(y); } instance variables of class Line Constructor where Line can be created from two points computeDistance() method of point class Constructor where Line can be created from 4 double values

  19. void show() { System.out.println("x="+x); System.out.println("y="+y); System.out.println("Length="+length); } // Introduce a method for computing slope } // End of Line class Method for printing the values for x and y and length

  20. class LineDemo { public static void main(String args[]) { Point p1 = new Point(); Point p2 = new Point(4,true); Point p4 = new Point(10,8); Point p5 = new Point(-10,-8); Line l2 = new (new Point(),new Point(3,5)); Line l1 = new Line(p4,p5); l1.show(); } } Origin point X =4 , Y =0 X =10,Y =8 X =-10,Y =-8 E:\New Folder\Java>java LineDemo x=Point@256a7c y=Point@720eeb Length=25.612496949731394

  21. Use of static keyword in Java • There are Four different uses of static keyword in java. • static instance variables • static methods • static classes • static blocks Note : static field/methods of a class can be accessed/referenced even without creating the objects of that class [ Provided They are not static]. Syntax : <classname> . <fieldname> OR <classname> . <methodname>

  22. static instance variables/fields • Any Field/Attribute/instance field of a class can be declared as static. • Declaring an instance field as static makes it class variable that belongs • to a whole class not an individual object. • For a static fields of any class, all the objects of that class share a • common copy. • Static field is just like a global variable for a class that is allocated • memory once and all objects of that class share that common copy. • Static field/instance variables are allocated space in the heap area.

  23. Example (Static Fields) Static Member class circle { static double PI=3.14156; double radius; double area() { return PI * radius * radius; } double perimeter() { return 2*PI*radius; } } // End of circle class Non-static instance field circle c1 = new circle(); circle c2 = new circle(); Memory Map PI = 3.14156 radius radius c1 c2

  24. Example (Static Fields) A a1 = new A(); A a2 = new A(); class A { static int a = 10; double b , c; ………. } Memory Map a=10 b c b c a2 a1

  25. Static Methods • static methods can use only static data • static methods can be called/accessed/referenced even without creating the objects that class. • Syntax <class name> . < method name(parameter list)> • static method can not call non static methods. • Examples: Math.sqrt (all math functions) • Static method can declare local variables but from outside it can access only static data(fields/methods)

  26. non static instance fields Static Method Example class num { int a,b,c; static int d = 10; static double e = 20.56; num(int a,int b,int c) { this.a = a; this.b =b; this.c =c; } static int sum(int a1 , int b1) { //System.out.println(“a=”+a+”b=”+b+”c=”+c); System.out.println(“d=”+d+”e=”+e); return 40; } static instance fields static method a,b,c are non static fields and can not be accessed from a static method

  27. static double sum(double a , double b) { System.out.println(“d=”+d+”e=”+e); return 40.56; } static void pr() { System.out.println(“This is method pr”); } void print() { System.out.println(“This is method print”); pr(); //  call to static method from a non staticmethod ----Vaild System.out.println(“a=”+a+”b=”+b+”c=”+c); System.out.println(“d=”+d+”e=”+e); } }

  28. class BOX { private double l,b,h; // Instance Fields BOX(double a,double b,double c) { l=a;this.b=b;h=c;} // Constructor boolean isEqual(BOX other) { if (this.l == other.l && this.b == other.b && this.h == other.h) return true; else return false; } static boolean isEqual(BOX b1, BOX b2) { if (b1.l == b2.l && b1.b == b2.b && b1.h == b2.h) return true; else return false; } } // End of BOX class

  29. class statictest { public static void main(String args[]) { BOX b1 = new BOX(10,6,8); BOX b2 = new BOX(10,6,8); BOX b3 = new BOX(1,16,18); BOX b4 = new BOX(2,6,8); System.out.println(b1.isEqual(b2)); System.out.println(BOX.isEqual(b1,b2)); System.out.println(b3.isEqual(b1,b2)); System.out.println(b4.isEqual(b2)); System.out.println(b4.isEqual(b4,b2)); } } D:\Java1>java statictest true true true false false

  30. Use of final keyword in java • final keyword in java can be used for following • (i) class declaration • (ii) variable declartion • (iii) method decalaration • final keyword for class means class cannot be inherited. • Final classes can not have subclasses. • final keyword used with variable declaration makes it • constant whose value cannot be changed. • Final variables should be initialized to some values • at the time of declaration. • final keyword used with method definition means • method can not be overridden by subclasses ( Makes sense only when inheritance is used)

  31. final instance variable final class final methods final class ABC { ………………… ……………….. } class a extends ABC { …… } // Wrong / Invalid final int x = 40; final double x = 3.4; final double PI = 3.14 class ABC { ………………… final void show().. } class a extends ABC { void show()…… } // Wrong / Invalid If it is declared as private final void show() then a can have method void show()

  32. final Keyword Example1 class ABC { //final int a; final int a = 10; int b; double c; //final static int d; static int d; ABC(int x,double c,int d) { b=x; this.c = c; this.d =d ;} void show() { System.out.println("a="+a); System.out.println("b="+b); System.out.println("c="+c); System.out.println("d="+d); } } Final field/variable should be initialized to some default value

  33. class ABCDemo { public static void main(String args[]) { ABC a1 = new ABC(10,8,5); ABC a2 = new ABC(-10,18,15); ABC a3 = new ABC(67,80.56,50); ABC a4 = new ABC(76,-6.45,95); a1.show(); } }

  34. final Keyword Example2 class A { final int a = 10; } class ABC { public static void main(String args[]) { A a1 = new A(); final int a=10; System.out.println("a="+a); } } Instance field is declared as final Local Variable is declared as final

  35. final Object Reference • You can declare any object reference as final • Declaring a Object reference as final does not mean that Object’s state i.e attribute values can not be changed. We can change the values of attributes provided it is allowed. • Declaring a Object reference as final makes the reference itself as final and it can not point to any other object.

  36. final Object reference Example class circle { double radius; circle(double radius) { this.radius = radius; } double getRadius() { return radius; } void setRadius(double radius) { this.radius = radius; } }// End of circle final circle c1 = new circle(20); c1.radius = 30; circle c2 = new circle(30); c1 = c2 OK WRONG c1 final object reference it can not point to another Object

  37. Use of toString() Method • toString() method is defined by Object class(Supermost super class for all java classes • toString() defines the String form of any object. • Syntax : • public String toString() { …… } • Any class can override this method to provide String form of any object. • If it is not overridden then this method will be called from Object class.

  38. class box { int l,b,h; box(int a,int b,int c) { l=a; this.b=b; this.h=c; } void show() { System.out.println("length="+l); System.out.println(“Width="+b); System.out.println(“height="+h); } } // End of class box

  39. class boxtest { public static void main(String args[]) { box b1 = new box(10,20,30); box b2 = new box(4,5,6); System.out.println(10); System.out.println(10.25+10); System.out.println(b1); System.out.println(b2); } } // End of boxtest ?

  40. /* Out Put E:\New Folder\Java>java boxtest 10 20.25 box@256a7c box@720eeb */ Output Generated By calling toString() from Object

  41. class box { int l,b,h; box(int a,int b,int c) { l=a; this.b=b; this.h=c; } void show() { System.out.println("length="+l); System.out.println("Width="+b); System.out.println("height="+h); } public String toString() { return "Hello How are You"; } } // End of box class

  42. class boxtest1 { public static void main(String args[]) { box b1 = new box(10,20,30); box b2 = new box(4,5,6); System.out.println(10); System.out.println(10.25+10); System.out.println(b1); System.out.println(b2); } } ?

  43. /* Out Put E:\New Folder\Java>java boxtest1 10 20.25 Hello How are You Hello How are You */ Out Put generated By toString() defined by box class

  44. Replace toString() method in box class as follows public String toString() { return “Length: ”+l+” “+”Width: ”+b+” ”+”Height: ”+h; } OUTPUT 10 20.25 Length:10 Width:20 Height:30 Length:4 Width:5 Height:6

More Related