public class Doubler extends Base {

Slides:



Advertisements
Similar presentations
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
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.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
© 2006 Pearson Addison-Wesley. All rights reserved Inheritance Systems Merchandise ElectronicsClothing TelevisionCamcorderShirtDressShoe DigitalAnalog.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance Like father like son Image from:
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Lecture 8.3 The Use of JComponent. © 2006 Pearson Addison-Wesley. All rights reserved More About the Standard Drawing Classes java.awt.Container.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved A Crash Course in the Use of Timed Events What kinds of.
EVENTS Wiring together objects, code, and actions.
Topic 4 Inheritance.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Inheritance ITI1121 Nour El Kadri.
Advanced Programming in Java
OOP Powerpoint slides from A+ Computer Science
Chapter 11 Inheritance and Polymorphism
How to Design Supplier Classes
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
Lecture 17: Polymorphism (Part II)
Interfaces and Inheritance
Continuing Chapter 11 Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Programming
Chapter 9 Inheritance and Polymorphism
Polymorphism.
Polymorphism 2nd Lecture
CSE 143 Lecture 22 The Object class; Polymorphism read
Polymorphism and access control
Advanced Java Topics Chapter 9
CMSC 202 Polymorphism.
CSE 143 Lecture 22 The Object class; Polymorphism read
Building Java Programs
Announcements & Review
Today’s topics UML Diagramming review of terms
Class Hierarchies and Type Conformance
Polymorphism Polymorphism - Greek for “many forms”
Polymorphism.
Lecture 18: Polymorphism (Part II)
Chapter 9 Carrano Chapter 10 Small Java
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 2
CSE 143 Lecture 23 Polymorphism; the Object class read
Classes and Objects Object Creation
Advanced Programming in Java
CMSC 202 Constructors Version 9/10.
Presentation transcript:

public class Doubler extends Base { public class Base { protected int theInt = 100; ... public void printTheInt() { System.out.println( theInt ); } public class Doubler extends Base { ... public void printTheInt() { System.out.println( theInt*2 ); } public class Tripler extends Base { ... public void printTheInt() { System.out.println( theInt*3 ); } public class Squarer extends Tripler { ... public void printTheInt() { System.out.println( theInt*theInt ); } © 2006 Pearson Addison-Wesley. All rights reserved

Some client code output subtype polymorphism As dynamic binding occurs Base theBase; theBase = new Base(); theBase.printTheInt(); theBase = new Doubler(); theBase = new Tripler(); theBase = new Squarer(); ... Squarer Doubler Tripler Base subtype polymorphism As dynamic binding occurs the behavior (i.e., methods) follow the objects. © 2006 Pearson Addison-Wesley. All rights reserved

Polymorphism comes from the words “poly” ... many “morph” ... to change A polymorphic variable can appear to change its type through dynamic binding. The compiler always understands a variable’s type according to its declaration. The compiler permits some flexibility by way of type conformance. At run-time the behavior of a method call depends upon the type of the object and not the variable. theBase = new Squarer(); theBase.printTheInt(); © 2006 Pearson Addison-Wesley. All rights reserved

Another Example of Polymorphism public class Sinker extends Component { ... public void doTheKitchenSink( int k ) { } Sinker sink; SlugWithSink slug; BugWithSink bug; RugWithSink rug; slug = new SlugWithSink(); rug = new RugWithSink(); bug = new BugWithSink(); sink = slug; sink.doTheKitchenSink(5); sink = bug; sink.doTheKitchenSink(10); sink = rug; sink.doTheKitchenSink(2); sink.doTheKitchenSink(3); public class SlugWithSink extends Sinker { ... public void doTheKitchenSink( int k ) { setLocation( k, k ); } public class BugWithSink extends Sinker { ... public void doTheKitchenSink( int k ) { setSize( k, 10 ); } public class RugWithSink extends Sinker { ... public void doTheKitchenSink( int k ) { setLocation( 0, k ); } © 2006 Pearson Addison-Wesley. All rights reserved What if Sinker does NOT include doTheKitchenSink() ?

Why is Polymorphism Useful? Polymorphism permits a superclass to capture commonality, leaving the specifics to subclasses. Suppose that JComponent included an area method, as shown. public class JComponent { ... public double area() { return 0.0; } Then Rectangle could be written as ... public class Rectangle extends JComponent { ... public double area() { return getWidth() * getHeight(); } and Oval could be written as ... public class Oval extends JComponent { ... public double area() { return getWidth()/2. * getHeight()/2. * Math.PI; } now consider public double coverageCost( JComponent v, double costPerSqUnit) { return v.area() * costPerSqUnit; } © 2006 Pearson Addison-Wesley. All rights reserved

Object Every class inherits from a class called Object. . . . «query» + boolean equals( Object ) + String toString() Every class inherits from a class called Object. The println method is overloaded. One version of println is defined as follows. public void println( Object x ) { println( x.toString() ); } Oval could override toString() in the following manner. public String toString() { String result = "Oval object" + "\n"; result = result + " x: " + getX() +", y: "+getY(); result = result + " width: " + getWidth() +", height: "+getHeight() + "\n"; if (getParent() == null) { result = result + " not added, \n"; } else { result = result + " is added, \n"; } result = result + " color: "+colorString(getBackground()); return result; © 2006 Pearson Addison-Wesley. All rights reserved

Equality There are two different kinds of equality for reference data. Identity equality means that two expressions have the same identity. (i.e., the two expressions represent the same object.) Content equality means that two expressions represent objects with the same value/content . The == symbol tests for identity equality, when applied to reference data. Example Oval ov1, ov2; ov1 = new Oval(0, 0, 100, 100); ov2 = new Oval(0, 0, 100, 100); if (ov1 == ov2) { System.out.println( “identity equality” ); } else { System.out.println( “not identity equality” ); } © 2006 Pearson Addison-Wesley. All rights reserved

Content Equality The equals method from Object provides a potential content equality check. Example public class MyOval extends Oval { ... public boolean equals(Object z) { return (z instanceof Oval) && ((Oval)z).getX() == getX() && ((Oval)z).getY() == getY() && ((Oval)z).getWidth() == getWidth() && ((Oval)z).getHeight() == getHeight() && ((Oval)z).getBackground() == getBackground() && ((Oval)z).getParent() == getParent(); } © 2006 Pearson Addison-Wesley. All rights reserved

Arrays are like Objects A whole (aggregate) array can be ...  assigned to another array variable  passed as a parameter private void colorAllRed( Rectangle[] rects ) { int ndx = 0; while ( ndx != rects.length ) { rects[ndx].setBackground( Color.red ); ndx++; } Rectangle[] someRects, otherRects; someRects = new Rectangle[3]; someRects[0] = new Rectangle(0,0,5,5); someRects[1] = new Rectangle(6,6,8,8); someRects[2] = new Rectangle(10,10,10,10); otherRects = someRects; colorAllRed( otherRects ); © 2006 Pearson Addison-Wesley. All rights reserved

Arrays are like Objects 2 Every array conforms to Object ... int[] myArray; myArray = (int[])makeAnObject(); private Object makeAnObject( ) { int[] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; processObject( daysInMonth ); return daysInMonth; } private void processObject( Object stuff ) { © 2006 Pearson Addison-Wesley. All rights reserved

Arrays are unlike Objects There is no way to override Object methods. toString equals Inheritance of arrays is NOT permitted. public class myClass extends someClass[] © 2006 Pearson Addison-Wesley. All rights reserved