1 2000 Prentice Hall, Inc. All rights reserved. Java Object-Oriented Programming Outline 1 Introduction 2 Superclasses and Subclasses 3 protected Members 4 Relationship between Superclass Objects and Subclass Objects 5 Implicit Subclass-Object-to-Superclass-Object Conversion 6 Software Engineering with Inheritance 7 Composition vs. Inheritance 8 Introduction to Polymorphism 9 Type Fields and switch Statements 10 Dynamic Method Binding 11 final Methods and Classes 12 Abstract Superclasses and Concrete Classes 13 Polymorphism Example 14 New Classes and Dynamic Binding 15 Case Study: Inheriting Interface and Implementation 16 Case Study: Creating and Using Interfaces 17 Inner Class Definitions 18 Notes on Inner Class Definitions 2 2000 Prentice Hall, Inc. All rights reserved. 1. Introduction • Object-Oriented Programming (OOP) – Inheritance - form of software reusability • New classes created from existing ones • Absorb attributes and behaviors, and add in their own – Override methods - redefine inherited methods • Subclass inherits from superclass – Direct superclass - subclass explicitly inherits – Indirect superclass - subclass inherits from two or more levels up the class hierarchy – Polymorphism • Write programs in a general fashion to handle a wide variety of classes – Abstraction - seeing the big picture 3 2000 Prentice Hall, Inc. All rights reserved. 1 Introduction (II) • Object-Oriented Programming – Introduce protected member access – Relationships • "is a" - inheritance – Object of subclass "is a" object of the superclass • "has a" - composition – Object "has a" object of another class as a member – Class libraries • New classes can inherit from them • Someday software may be constructed from standardized, reusable components (like hardware) • Create more powerful software 4 2000 Prentice Hall, Inc. All rights reserved. 2 Superclasses and Subclasses • Inheritance example – A rectangle "is a" quadrilateral • Rectangle is a specific type of quadrilateral • Quadrilateral is the superclass, rectangle is the subclass • Incorrect to say quadrilateral "is a" rectangle – Naming can be confusing because subclass has more features than superclass • Subclass more specific than superclass • Every subclass "is an" object of its superclass, but not vice- versa – Form tree-like hierarchal structures • Create a hierarchy for class Shape (next slide) 5 2000 Prentice Hall, Inc. All rights reserved. 2 Superclasses and Subclasses (II) • Using inheritance – Use keyword extends class TwoDimensionalShape extends Shape{ } – private members of superclass not directly accessible to subclass – All other variables keep their member access Shape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle Sphere Cube Tetrahedron 6 2000 Prentice Hall, Inc. All rights reserved. 3 protected Members • In a superclass – public members • Accessible anywhere program has a reference to a superclass or subclass type – private members • Accessible only in methods of the superclass – protected members • Intermediate protection between private and public • Only accessible by methods of superclass, of subclass, or classes in the same package • Subclass methods – Can refer to public or protected members by name – Overridden methods accessible with super.methodName 7 2000 Prentice Hall, Inc. All rights reserved. 4 Relationship between Superclass Objects and Subclass Objects • Object of subclass – Can be treated as object of superclass • Reverse not true – Suppose many classes inherit from one superclass • Can make an array of superclass references • Treat all objects like superclass objects – Explicit cast • Convert superclass reference to a subclass reference (downcasting) • Can only be done when superclass reference actually referring to a subclass object – instanceof operator • if (p instanceof Circle) • Returns true if the object to which p points "is a" Circle 8 2000 Prentice Hall, Inc. All rights reserved. 4 Relationship between Superclass Objects and Subclass Objects (II) • Overriding methods – Subclass can redefine superclass method • When method mentioned in subclass, subclass version used • Access original superclass method with super.methodName – To invoke superclass constructor explicitly (called implicitly by default) • super(); //can pass arguments if needed • If called explicitly, must be first statement • Every Applet has used these techniques – Inheritance concept formalized – Java implicitly uses class Object as superclass for all classes – We have overridden init and paint when we extended JApplet 2000 Prentice Hall, Inc. All rights reserved. Outline 1. Point definition 1.1 Data members 1.2 Constructors 1.3 Methods 1 // Fig. 27.3: Point.java 2 // Definition of class Point 3 4 public class Point { 5 protected int x, y; // coordinates of the Point 6 7 // No-argument constructor 8 public Point() 9 { 10 // implicit call to superclass constructor occurs here 11 setPoint( 0, 0 ); 12 } 13 14 // Constructor 15 public Point( int a, int b ) 16 { 17 // implicit call to superclass constructor occurs here 18 setPoint( a, b ); 19 } 20 21 // Set x and y coordinates of Point 22 public void setPoint( int a, int b ) 23 { 24 x = a; 25 y = b; 26 } 27 28 // get x coordinate 29 public int getX() { return x; } 30 2000 Prentice Hall, Inc. All rights reserved. Outline 1.2 Methods 1. Circle Definition 1.1 extends Point 1.2 Multiple constructors 38 // Fig. 27.3: Circle.java 39 // Definition of class Circle 40 41 public class Circle extends Point { // inherits from Point 42 protected double radius; 43 44 // No-argument constructor 45 public Circle() 46 { 47 // implicit call to superclass constructor occurs here 48 setRadius( 0 ); 49 } 50 51 // Constructor 52 public Circle( double r, int a, int b ) 53 { 54 super( a, b ); // call to superclass constructor 55 setRadius( r ); 56 } 57 58 // Set radius of Circle 59 public void setRadius( double r ) 60 { radius = ( r >= 0.0 ? r : 0.0 ); } 31 // get y coordinate 32 public int getY() { return y; } 33 34 // convert the point into a String representation 35 public String toString() 36 { return "[" + x + ", " + y + "]"; } 37 } 37 } [...]... 1.3 Overridden toString method 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 // Fig 27.3: InheritanceTest .java // Demonstrating the "is a" relationship import java. text.DecimalFormat; import javax.swing.JOptionPane; public class InheritanceTest { public static void main( String args[] ) { Point pointRef, p; Circle circleRef, c; String output; Outline... Overridden method volume 1.6 Overridden method toString } 1.4 Overridden method getName © 2000 Prentice Hall, Inc All rights reserved 123 // Fig 27.10: Test .java 124 // Driver for point, circle, cylinder hierarchy 125 import javax.swing.JOptionPane; 126 import java. text.DecimalFormat; 127 128 public class Test { 129 { Driver public static void main( String args[] ) 130 Outline 1 import 131 Point point = new... // Fig 27.4: Shape .java 2 // Definition of abstract base class Shape Outline 3 4 public abstract class Shape extends Object { 5 public double area() { return 0.0; } 6 public double volume() { return 0.0; } 7 public abstract String getName(); 1 abstract class Shape 8 } 1.1 Member functions 1.2 abstract method getName © 2000 Prentice Hall, Inc All rights reserved 9 // Fig 27.4: Point .java 10 // Definition... – Might forget to test all possible cases – Every addition/deletion of a class requires all switch statements to be changed • Tracking all these changes is time consuming and error prone – Polymorphic programming can eliminate the need for switch logic • Avoids all these problems automatically © 2000 Prentice Hall, Inc All rights reserved 19 10 Dynamic Method Binding • Dynamic Method Binding – At execution... must be abstract © 2000 Prentice Hall, Inc All rights reserved 24 13 Polymorphism Example (III) • Iterator classes – Walks through all the objects in a container (such as an array) – Used in polymorphic programming • Walk through an array of superclass references • Call draw method for each reference © 2000 Prentice Hall, Inc All rights reserved 25 14 New Classes and Dynamic Binding • Dynamic binding... Print area 110 111 // Attempt to refer to Point object 112 // with Circle reference 113 if ( p instanceof Circle ) { 114 circleRef = (Circle) p; 115 output += "\n\ncast successful"; 116 // line 40 in Test .java } 117 3 if statement else 118 output += "\n\np does not refer to a Circle"; 119 120 JOptionPane.showMessageDialog( null, output, 121 "Demonstrating the \"is a\" relationship", 122 JOptionPane.INFORMATION_MESSAGE... 1.1 protected data members 1.2 Constructors 1.3 New methods 1.4 Overridden method getName 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 // Fig 27.10: Circle .java // Definition of class Circle public class Circle extends Point { protected double radius; Outline // inherits from Point // no-argument constructor public Circle() { // implicit call to superclass... Overridden method getName © 2000 Prentice Hall, Inc All rights reserved 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 // Fig 27.10: Cylinder .java // Definition of class Cylinder Outline public class Cylinder extends Circle { protected double height; // height of Cylinder // no-argument constructor public Cylinder() { // implicit call to superclass... reserved 15 6 Software Engineering with Inheritance • Inheritance – Customize existing software • Create a new class, add attributes and behaviors as needed • Software reuse key to large-scale projects – Java and OOP does this – Availability of class libraries and inheritance • Superclass – Specifies commonality – Look for commonality among a set of classes • "Factor it out" to form the superclass – Subclasses . 1 2000 Prentice Hall, Inc. All rights reserved. Java Object-Oriented Programming Outline 1 Introduction 2 Superclasses and Subclasses 3 protected Members 4. Downcast 2.3 toString 75 // Fig. 27.3: InheritanceTest .java 76 // Demonstrating the "is a" relationship 77 import java. text.DecimalFormat; 78 import javax.swing.JOptionPane; 79 80 public class. the big picture 3 2000 Prentice Hall, Inc. All rights reserved. 1 Introduction (II) • Object-Oriented Programming – Introduce protected member access – Relationships • "is a" - inheritance – Object