Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.

Similar presentations


Presentation on theme: "1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning."— Presentation transcript:

1 1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.

2 2 Object: The father of all classes l Every class that does not specifically extend another class is a subclass of the class Object. l Thus every class directly or indirectly inherits from the standard class, Object. If we have: class Parent { int size; } The compiler automatically converts it to: class Parent extends Object { int size; } Consider the following example: class TestObject { public static void main(String[] args) { Parent watchThis = new Parent(); String s = watchThis.toString(); System.out.println(s); // Where does toString() comes from? }

3 3 Some methods of the Object class clone() Creates a clone of the object. equals(Object) Compares two Objects for equality. Uses "==" to test for equality finalize() Code to perform when this object is garbage collected. getClass() Returns the Class of this Object. hashCode() Returns a hashcode for this Object. toString() Returns a String value of this Object. The toString, equals and clone methods are often overridden when classes are created

4 4 Casting and Classes … Up-Casting l Just like Java allows a smaller primitive type (e.g. int) to be assigned to a bigger type (e.g. double), It also allow a reference to a subclass object to be assigned to a reference variable of a super class object. l This is called up-casting because we are moving up the inheritance tree. l Example: Suppose we have the following. class Parent { int data; } class Child extends Parent { String name; } Then we could have the following: Object object; Parent parent; Child child = new Child(); parent = child; object = child;

5 5 Casting and Classes … Up-Casting l Up-casting is very useful because it allows a single method to be written to handle all classes derived from a single class. l For example, suppose we are writing an a application to print the standing of students based on their GPA: class TestReserchAssistant { static String standing(Student s) { if (s.getGPA() <= 2.0) return "Under Warning"; else return "Good Standing"; } static void main (String[] args) { Student s = new Student(991234,"Ahmed", 1.95); ResearchAssistant ra = new ResearchAssistant(995678, "Imran", 3.45, 15); s.print(); System.out.println(standing(s)); ra.print(); System.out.println(standing(ra)); } l Notice that the standing method can be called with ether Student or ResearchAssistant object.

6 6 Casting and Classes … Down-Casting l Java does not automatically allow a reference to a super class object to be assigned to a reference variable of a subclass type. However, we can force it by using casting. This is called down-casting. e.g. Object object; Parent parent; Child child;........ parent = (Parent) object; child = (Child) parent; l Here we are taking a serious risk. If the original object was created from the super class and we now cast it to a subclass type, then if we try to access a method that does not exists in the super class, it will result in run-time error. l How ever, if the object was originally created from the subclass, then up-casted to super class type and then down-casted back to sub-class type, then there is no problem. l We can always know the class from which an object is created by using the instanceOf operator. l In fact it is safer to always do the following: if (parent instanceOf Child) child = (Child) parent;

7 7 Object Cloning l Cloning means making a copy of an object and we saw example of cloning with arrays. l The clone method of the Object class can be used to clone an object. l However, if an object contains a reference to another object, the clone method does not clone the other object. It creates what is called a shallow copy of the object. l For this reason, Java put two restrictions to ensure that the clone method is not called accidentally. l First is that the method has protected access. Thus, it cannot be called as x.clone() if the class to which x belongs hasn’t redefined clone to be public.

8 8 Object Cloning (cont’d) l The second restriction is that the clone method checks that the object being cloned implements the Cloneable interface, else it throws an exception. l Thus, to clone an object, the class from which the object is created must implement Cloneable and must also throw or handle the Exception. public class Customer implements Cloneable {...... public Object clone() { try { Customer clonedCustomer = super.clone(); return clonedCustomer; } catch(CloneNotSupportedException e) { //never happened but must do it anyway. return null; } l Suppose the customer object has a reference to a BankAccount object, then the above cloning will not copy the account object -- it is shallow. l To get a deep-copy, we need to modify the clone method as follows:

9 9 Object Cloning (Cont’d) public class Customer implements Cloneable { private String name; private BankAccount account;...... public Object clone() { try { Customer clonedCustomer = (Customer)super.clone(); clonedCustomer.account = (BankAccount)account.clone(); return clonedCustomer; } catch(CloneNotSupportedException e) { //never happened but must do it anyway. return null; }

10 10 Importance of cloning l Consider the following class: public class Customer { private String name; Private BankAccount account;.... public String getName() { return name; } public BankAccount getAccount() { return account; }.... } l Do you observe anything wrong with the getAccount() method? l The problem is it gives away everything – see blow Customer amr = new Customer("Amr"); BankAccount account = amr.getAccount(); Account.withdraw(1000000); l To solve this problem, the getAccount() should only give away a copy. public BankAccount getAccount() { return (BankAccount)account.clone(); }


Download ppt "1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning."

Similar presentations


Ads by Google