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

Slides:



Advertisements
Similar presentations
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
1. 2 Introduction to Inheritance  Access Modifiers  Methods in Subclasses  Method Overriding  Converting Class Types  Why up-cast?  Why down-cast?
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 The final Modifier A method marked final indicates that it cannot be overridden with a new definition.
CSE 11 February 6, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
1 Further OO Concepts (Part I) Further OO Concepts I - Polymorphism Overview l Polymorphism is Wonderful. l Usefulness of Up casting? l Method call binding?
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Advanced Java Programming CS 537 – Data Structures and Algorithms.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Topic 4 Inheritance.
Inheritance and Access Control CS 162 (Summer 2009)
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Object Oriented Programming Java 1 Some Notes on Cloning, Packages and Visibility Notes from Bruce Eckel’s “Thinking in Java” and “Just Java” by.
Java Type System and Object Model Horstmann ch , 7.7.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Chapter 4 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance ndex.html ndex.htmland “Java.
1 Chapter 2 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
Carnegie Mellon University, Graduate School of Industrial Administration 1 Initialization and Cloning Slides adapted from course text “Thinking in Java”
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Classes Revisited Chapter 8.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
1 clone() Defined in Object Creates an identical copy –Copies pointers to fields (does not copy fields of fields) –Makes a shallow copy if the object’s.
C11, Implications of Inheritance
Lecture 12 Inheritance.
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Computer Science II Exam 1 Review.
CSE 331 Cloning objects slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Extending Classes.
Java Programming Language
Inheritance.
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Polymorphism.
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
Presentation transcript:

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

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 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 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 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 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 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 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 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 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( ); l To solve this problem, the getAccount() should only give away a copy. public BankAccount getAccount() { return (BankAccount)account.clone(); }