Sie sind auf Seite 1von 17

class A {} class B extends A{} class C extends B{}

Object A
<< Employee>>

class testA B << Manager>> { public static void main(String args[]) C { Object o1 = new C(); C c1 = (C) o1; B b1 = (B) o1; Whats Wrong here? A a1 = (A) o1; NOTHING } }

class A {} class B extends A{} class C extends B{}

Object A
<< Employee>>

class testA B << Manager>> { public static void main(String args[]) C { C c1 = new C(); A a1 = (A) c1; B b1 = (B) c1; Whats Wrong here? } NOTHING }

class A {} class B extends A{} class C extends B{}

Object A

class testA B { public static void main(String args[]) C { A a1 = new A(); C c1 = (C) a1; } Whats Wrong here? } D:\OOP>java testA Exception in thread "main" java.lang.ClassCastException: A at testA.main(testA.java:10)

class BOX { private double l,b,h; BOX(double l,double b,double h) { this.l = l; this.b = b; this.h = h; } } class clonetest { public static void main(String args[]) { BOX b1 = new BOX(10,6,8); BOX b2 = (BOX)b1.clone(); } }

D:\OOP>javac clonetest.java clonetest.java:16: clone() has protected access in java.lang.Object BOX b2 = (BOX)b1.clone(); ^ 1 error D:\OOP>

class BOX { private double l,b,h; BOX(double l,double b,double h) { this.l = l; this.b = b; this.h = h; } public Object clone() { return super.clone(); } } class clonetest { public static void main(String args[]) { BOX b1 = new BOX(10,6,8); BOX b2 = (BOX)b1.clone(); } }

D:\OOP>javac clonetest.java clonetest.java:12: unreported exception java.lang.CloneNotSupportedExc eption; mu st be caught or declared to be thrown return super.clone(); ^ 1 error

class BOX implements Cloneable { private double l,b,h; BOX(double l,double b,double h) { this.l = l; this.b = b; this.h = h; } public Object clone() { try { return super.clone(); } catch(Exception e){ return null; } } }

class clonetest { public static void main(String args[]) { BOX b1 = new BOX(10,6,8); BOX b2 = (BOX)b1.clone(); } }

import java.util.*; class datetest { public static void main(String args[]) { Date d1 = new Date(); Date d2 = new Date(); System.out.println(d1); System.out.println(d2); System.out.println(d1.getTime()); d1.setTime(1000); System.out.println(d1); } D:\OOP>java datetest } Sat Mar 10 22:22:40 GMT+05:30 2007
Sat Mar 10 22:22:40 GMT+05:30 2007 1173545560812 Thu Jan 01 05:30:01 GMT+05:30 1970

import java.util.*; class Employee implements Cloneable { private String name; // Immutable instance private Date joiningDate; // Mutable instance Employee(String n, Date d) { name =n; joiningDate =d; } public Object clone() { try { Employee cloned = (Employee) super.clone(); return cloned; } catch(CloneNotSupportedException e) { return null; } } }

class clonetest10 { public static void main(String args[]) { Employee e1 = new Employee("David", new Date()); Employee e2 = (Employee) e1.clone(); } } <<String>>

e1

name Joining Date name Joining Date

David <<Date>> new Date(); SHALLOW COPY

e2

class Point { private double x,y; Point(double x, double y) { this.x =x; this.y =y; } public double getX() { return x;} public double getY() { return y;} }

Immutable Class

class Line implements Cloneable { private Point start,end; Line(Point start, Point end) { this.start = start; this.end = end; } public Object clone() { try { Line l1 = (Line) super.clone(); return l1; } catch(Exception e) {return null;} } }

class clonetest11 { public static void main(String args[]) { Line l1 = new Line(new Point(5,6), new Point(4,5)); Line l2 = (Line) l1.clone(); } <<Point>> } new Point(5,6), start l1 <<Point>> end new Point(4,5), start l2 end NO PROBLEM IN SHARING

class Point { private double x,y; Point(double x, double y) { this.x =x; this.y =y; } public double getX() { return x;} public double getY() { return y;} public void setX(double x) { this.x =x; } public void setY(double x) { this.y =x; }

public Object clone() { try { Point clonedPoint = (Point) super.clone(); return clonedPoint; } catch(CloneNotSupportedException e) { return null; } } }

<< Mutable class>>

class Line implements Cloneable { private Point start,end; Line(Point start, Point end) { this.start = start; this.end = end; } public Object clone() { try { Line l1 = (Line) super.clone(); l1.start = (Point) start.clone(); l1.end = (Point) end.clone(); return l1; } catch(Exception e) {return null;} } }

Clone the mutable instance fields also.

SUFFICIENT class clonetest11 { public static void main(String args[]) DEEP COPY { Line l1 = new Line(new Point(5,6), new Point(4,5)); Line l2 = (Line) l1.clone(); } } new Point(5,6), <<Point>> start l1 end new Point(4,5), <<Point>>
start l2 end new Point(4,5), <<Point>> new Point(5,6), <<Point>>

import java.util.*; class Employee implements Cloneable { private String name; private Date joiningDate; Employee(String name,Date jdate) { this.name =name; joiningDate = jdate; } public String toString() { return "Name:"+name+"Join Date:"+joiningDate; }

public boolean equals(Object other) { if( other == null) return false; if( this.getClass() == other.getClass()) return false; Employee emp = (Employee) other; int a = name.compareTo(emp.name); int b = joiningDate.compareTo(emp.joiningDate); return (a == 0 && b == 0); } public Object clone() { try { Employee clonedEmployee = (Employee) super.clone(); clonedEmployee.joiningDate = (Date) joiningDate.clone(); return clonedEmployee; } catch(CloneNotSupportedException e) { return null; } } } // End of Emplyoee class

class Manager extends Employee { private String dept; private double bonus; Manager(String name,Date jdate, String dept, double bonus) { super(name,jdate); this.dept =dept; this.bonus = bonus; } public String toString() { return super.toString()+"Department:"+dept+"Bo nus:"+bonus; }

public boolean equals(Object other) { if(!super.equals(other)) return false; Manager m1 = (Manager) other; return (bonus== m1.bonus); } public Object clone() { Manager clonedManager = (Manager) super.clone(); return clonedManager; } } // End of manager class

class clonetest12 { public static void main(String args[]) { Manager m1 = new Manager("Davis",new Date(),"Personal",2000); Manager m2 = (Manager) m1.clone(); Manager m3 = new Manager("Davis",new Date(),"Personal",2000); System.out.println(m1); System.out.println(m1.equals(m2)); System.out.println(m1.equals(m3)); } }

D:\OOP>java clonetest12 Name:DavisJoin Date:Sun Mar 11 14:02:53 GMT+05:30 2007Department:PersonalBonus: 2 000.0 false false

Das könnte Ihnen auch gefallen