Chap. 2. Object-Oriented Design. Inheritance Building ApartmentHouse Commercial Building Low-rise Apartment High-rise Apartment Two-story House RanchSkyscraper.

Slides:



Advertisements
Similar presentations
Data Structures Lecture 2 Fang Yu Department of Management Information Systems National Chengchi University Fall 2011.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Written by: Dr. JJ Shepherd
Class Hierarchy (Inheritance)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
March 2004Object Oriented Design1 Object-Oriented Design.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
UCD Computer Science COMP Object-oriented design Learning the basic building blocks of programming is straightforward... On the other hand.... Designing/implementing.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 L39 Generics (1). 2 OBJECTIVES  To create generic methods that perform identical tasks on arguments of different types.  To create a generic Stack.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Chap. 1 Classes, Types, and Objects. How Classes Are Declared [ ] class [extends ] [implements,, … ] { // class methods and instance variable definitions.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 5 Generic.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Classes, Interfaces and Packages
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.
Classes - Intermediate
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
1 COMP9024: Data Structures and Algorithms Week One: Java Programming Language (II) Hui Wu Session 2, 2014
Data Structures and Algorithms in JAVA Chapter 2.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
COMP9024: Data Structures and Algorithms
COMP9024: Data Structures and Algorithms
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Computer Science II Exam 1 Review.
Chapter 9 Inheritance and Polymorphism
Java Programming Language
CS201: Data Structures and Discrete Mathematics I
Java – Inheritance.
Presentation transcript:

Chap. 2. Object-Oriented Design

Inheritance Building ApartmentHouse Commercial Building Low-rise Apartment High-rise Apartment Two-story House RanchSkyscraper An example of an “is a” hierarchy involving architectural buildings

class: S fields: x methods: a() b() c() class: T fields: y methods: d() e() extends

Polymorphism Polymorphism refers to the ability of an object variable to take different forms. If both S and T define an a() method, when o refers to an object from class T, it uses T’s a() when o.a() is called; when o refers to an object from class S, then it uses S’s a() when o.a() is called. Thus, the object o is polymorphic.

Overloading Overloading is a useful technique related to polymorphism. Overloading occurs when a single class T has multiple methods with the same name, but different signature. The signature of a method is a combination of its name and the type and number of arguments that are passed to it.

For example, suppose a class T, which defines a method a(), extends a class U, which defines a method a(x,y). If an object o from class T receives the message “o.a(x,y)”, then U’s version of method a is invoked. Inheritance, polymorphism, and method overloading support the development of reusable software. In Java, each class can extend exactly one other class.

Java uses two kinds of method overriding, refinement and replacement. In the replacement type of overriding, a method completely replaces the method of the superclass that it is overriding. In the refinement type of overriding, a method does not replace the method of its superclass, but instead adds additional code to that of its superclass. In Java, constructors use the refinement type of method overriding whereas regular methods use replacement.

The Keyword this this is used as reference to the current instance of a class. It is useful, for example, when we want to pass the current object as a parameter to some method. public class ThisTester { // instance variable public int dog = 2; // constructor ThisTester( ) { // null constructor } public void clobber( ) { double dog = 5.0; this.dog = (int) dog; // two different dogs! }

public static void main(String args[]) { ThisTester t = new ThisTester(); System.out.println(“The dog field = “+ t.dog); t.clobber(); System.out.println(“After clobbering, dog = “ + t.dog); } Output: The dog field = 2 After clobbering, dog = 5

An example to pass arguments to methods class Car { String licensePlate = ""; // e.g. "New York 543 A23" double speed = 0.0; // in kilometers per hour double maxSpeed = 120.0; // in kilometers per hour // accelerate to maximum speed // put the pedal to the metal void floorIt() { this.speed = this.maxSpeed; } void accelerate(double deltaV) { this.speed = this.speed + deltaV; if (this.speed > this.maxSpeed) { this.speed = this.maxSpeed; } if (this.speed < 0.0) { this.speed = 0.0; } }

class: Progression fields: long first long cur methods: Progression( ) long firstValue() long nextValue() void printProgession(int) class: ArithProgression fields: long inc methods: ArithProgression( ) ArithProgression(long) long nextValue() class: GeomProgression methods: GeomProgression( ) GeomProgression(long) long nextValue() class: FibonacciProgression fields: long prev methods: FibonacciProgression( ) FibonacciProgression(long, long) long nextValue( ) fields: extends

Inheritance Example A generic class for numeric progressions public class Progression { // First value of the progression. protected long first; // Current value of the progression. protected long cur; // Default constructor Progression() { cur = first = 0; }

// Resets the progression to the first value protected long firstValue( ) { cur = first; return cur; } // Advances the progression to the next value. protected long nextValue( ) { return ++cur; // default next value } // Prints the first n values of the progression public void printProgression(int n) { System.out.print(firstValue( )); for (int i=2; i<= n; i++) System.out.print(“ “+nextValue()); System.out.println( ); // ends the line }

Inheritance Example An Arithmetic Progression class class ArithProgression extends Progression { // Increment protected long inc; // Inherits variables first and cur // Default constructor setting a unit increment ArithProgression( ) { this(1); } // Parametric constructor providing the increment ArithProgression(long increment) { inc = increment; } // Advances the progression by adding the increment to the current value protected long nextValue( ) { cur += inc; return cur; } // Inherits methods firstValue( ) and printProgression(int) }

Inheritance Example A Geometric Progression class class GeomProgression extends Progression { // Inherits variables first and cur // Default constructor setting base 2 GeomProgression( ) { this(2); } // Parametric constructor providing the base GeomProgression(long base) { first = base; cur = first; } // advances the progression by multiplying the base with the current value protected long nextValue( ) { cur *= first; return cur; } // Inherits methods firstValue( ) and printProgression(int) }

Inheritance Example A Fibonacci Progression class class FibonacciProgression extends Progression { // Previous value long prev; // Inherits variables first and cur FibonacciProgression( ) { this(0,1); } // Parametric constructor providing the first and the second values FibonacciProgression(long value1, long value2) { first = value1; prev = value2 – value1; // fictitious value preceding the first } // Advances the progression by adding the previous value to the current value protected long nextValue( ) { long temp = prev; prev = cur; cur += temp; return cur; } // Inherits methods firstValue( ) and printProgression(int) }

// Test program for the progression classes class Tester { public static void main (String[ ] args) { Progression prog; // test ArithProgression System.out.println(“Arithmetic progression with default increment:”); prog = new ArithProgression( ); prog.printProgression(10); System.out.println(“Arithmetic progression with increment 5:”); prog = new ArithProgression(5); prog.printProgression(10); // test GeomProgression System.out.println(“Geometric progression with default base:”); prog = new GeomProgression( ); prog.printProgression(10); System.out.println(“Geometric progression with base 3:”); prog = new GeomProgression(3); prog.printProgression(10); // test FibonacciProgression System.out.println(“Fibonacci progression with default start values:”); prog = new FibonacciProgression( ); prog.printProgression(10); System.out.println(“Fibonacci progression with start values 4 and 6:”); prog = new FibonacciProgression(4,6); prog.printProgression(10); }

Exceptions In Java, exceptions are objects that are “thrown” by code that encounters some sort of unexpected condition. They can also be thrown by the Java run- time environment should it encounter an unexpected condition, like running out of object memory.

A throw statement is typically written as: throw new ([,,…]); e.g.: if (insertIndex > size( )) { throw new BoundaryViolationException(“No element at index “ + insertIndex); e.g.: // can specify all the exceptions that might be thrown by a method public void goShopping( ) throws ShoppingListTooSmallException, OutOfMoneyException { // method body … }

Catching Exceptions The general syntax for a try-catch block in Java is as follows: try catch ( ) [catch ( ) ] … [finally ]

E.g.: int index = Integer.MAX_VALUE; //2.14 Billion try // This code might have a problem … { String toBuy = shoppingList[index]; } catch (ArrayIndexOutOfBoundsException aioobx) { System.out.println(“The index “+index+” is outside the array.”); } E.g.: catch (ArrayIndexOutOfBoundsException aioobx) { throw new ShoppingListTooSmallException( “Product index is not in the shopping list”); }

Interface An interface is a collection of method declarations with no data and no bodies. When a class implements an interface, it must implement all of the methods declared in the interface. The compiler or run-time system requires that the types of parameters that are actually passed to methods rigidly conform with the type specified in the interface. This requirement is known as strong typing.

Example // Interface for objects that can be sold public interface Sellable { // description of the object public String description(); // list price in cents public int listPrice(); // lowest price in cents we will accept public int lowestPrice(); }

// class for photographs that can be sold public class Photograph implements Sellable { private String descript; // description of this photo private int price; // the price we are setting private boolean color; // true if photo is in color public Photograph(String desc, int p, boolean c) { // constructor descript = desc; price = p; color = c; } public String description() { return descript; } public int listPrice() { return price; } public int lowestPrice() { return price/2; } public boolean isColor() { return color; } }

// Interface for objects that can be transported public interface Transportable { // weight in grams public int weight(); // whether the object is hazardous public boolean isHazardous(); }

// Class for objects that can be sold, packed, and shipped public class BoxedItem implements Sellable, Transportable { private String descript; // description of this item private int price; // list price in cents private int weight; // weight in grams private boolean haz; // true if object is hazardous private int height=0; // box height in centimeters private int width=0; // box width in centimeters private int depth=0; // box depth in centimeters // constructors public BoxedItem (String desc, int p, int w, boolean h) { descript = desc; price = p; weight = w; haz = h; } public String description() { return descript; } public int listPrice() { return price; } public int lowestPrice() {return price/2;} public int weight() { return weight; } public boolean isHazardous() {return haz;} public int insuredValue() {return price*2;} public void setBox(int h, int w, int d) { height = h; width = w; depth = d; }

Multiple Inheritance in Interfaces In Java, multiple inheritance is allowed for interfaces but not for classes. e.g.: public interface InsurableItem extends Transportable, Sellable { public int insuredValue(); // return insured Value in cents } public class BoxedItem implements InsurableItem { // … rest of code exactly as before }

Abstract Classes An abstract class is a class that contains empty method declarations (that is, declarations of methods without bodies) as well as concrete definitions of methods and/or instance variables. An abstract class lies between an interface and a complete concrete class. E.g.: A concrete number class, such as java.lang.Integer and java.lang.Double, extends the java.lang.Number class and fills in the details for the abstract methods of the superclass.

Casting in an Inheritance Hierarchy Number n; Integer i; n = new Integer(3); i = (Integer)n; // This is legal n = new Double(3.1415); i = (Integer) n; // This is illegal

Number n; Integer i; n = new Integer(3); if (n istanceof Integer) i = (Integer) n; // This is legal n = new Double(3.1415); if ( n instanceof Integer) i = (Integer) n; // This will not be attempted

Casting with Interface public interface Person { public boolean equalTo (Person other); // is this the same person public String getName( ); // get this person’s name public int getAge( ); // get this person’s age }

public class Student implements Person { String id; String name; int age; public Student (String i, String n, int a) {// simple constructor id = i; name = n; age = a; } protected int studyHours( ) { return age/2;} // just a guess public String getID( ) { return id; } // ID of the student public String getName( ) { return name;} // from Person interface public int getAge( ) { return age; }// from Person interface public boolean equalTo(Person other) { // from Person interface Student otherStudent = (Student) other; // cast Person to Student return (id.equals(otherStudent.getID( ))); // compare IDs }

public class PersonPairDirectory { // … instance variables would go here … public PersonPairDirectory() {//default constructor goes here} public void insert (Person person, Person other) { // insert code goes here } public Person findOther (Person person) { return null; } // stub for find public void remove (Person person, Person other) { // remove code goes here } } Student cute_one = myDirectory.findOther(smart_one); // wrong! Student cute_one = (Student) (myDirectory.findOther(smart_one));

Recursion Factorial(n)= 1 if n=0 n*factorial(n-1) if n>=1. public static int recursiveFactorial(int n) { // recursive factorial function if (n==0) return 1; // base case else return n*recursiveFactorial(n-1); // recursive case }

The Adapter Pattern The adapter pattern applies to any context where we want to modify an existing class so that its methods match those of a related, but different, class or interface. One general way for applying the adapter pattern is to define the new class in such a way that it contains an instance of the old class as a hidden field, and implement each method of the new class using methods of this hidden instance variable.

class StudentPairDirectory adapting class PersonPairDirectory // Class for a directory storing pairs of Student objects public class StudentPairDirectory { protected PersonPairDirectory directory; public StudentPairDirectory() { directory=new PersonPairDirectory();} public void insert(Student s, Student t) {directory.insert(s,t);} public Student findOther(Student s) { return (Student) directory.findOther(s); } public void remove(Student s, Student t) { directory.remove(s,t); } }