Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Inheritance and Polymorphism. Object-oriented programming is based on a paradigm in which objects are used to model a specification. Objects.

Similar presentations


Presentation on theme: "Chapter 9 Inheritance and Polymorphism. Object-oriented programming is based on a paradigm in which objects are used to model a specification. Objects."— Presentation transcript:

1 Chapter 9 Inheritance and Polymorphism

2 Object-oriented programming is based on a paradigm in which objects are used to model a specification. Objects are created from classes, which provide encapsulation. Inheritance extends a class and provides a means of polymorphism. This chapter discusses inheritance and polymorphism. Abstract classes and interfaces are also discussed.

3 Extending a Class Often times there is an existing class that provides a basis for an object that models a specification. However, the existing class may need additional methods or different implementations of existing methods to more closely represent the object for the model. For example, consider a disk, which has a circular shape. It is similar to a circle. However, a disk is three-dimensional and also has a thickness. Rather than create a whole new class to represent a disk, a class named Disk could extend the Circle class. Circle class Disk class extends the Circle class Disk class

4 Extending a Class Making one class an extension of another involves inheritance. Inheritance allows a class to define a specialized type of an already existing class. In this case, a disk is a solid circle with a thickness. Classes that are derived from existing classes demonstrate an is-a relationship. A class “is a” type of another class. In this case, a disk is a circle with a thickness. Circle class Disk class extends the Circle class Disk class

5 Extending a Class A class can have many levels of inheritance. For example, consider the following class hierarchy: Circle class Disk class Puck class The Puck class inherits the Disk class, which inherits the Circle class. The Circle class is the superclass of Disk. Disk is the subclass of Circle and the superclass of Puck. Puck is subclass of Disk. Superclass

6 Implementing a Subclass A class that inherits another class includes the keyword extends in the class declaration and takes the form: public class extends { } Designing a subclass requires selecting the superclass, or base class, and then defining any additional variable and method members for the subclass. In many cases, existing methods in the base class will also be overridden by new definitions in the subclass, also called the derived class.

7 Implementing a Subclass For example, the Disk class design appears similar to: Disk Inherits Circle Variable: thickness methods: setThickness—changes the thickness getThickness—returns the thickness volume—returns the volume of the disk equals—overrides the equals() method in Circle toString—overrides the toString() method in Circle Check out the Disk class implementation from the design above on the next slides.

8 /** * Disk class. */ public class Disk extends Circle { private double thickness; /** * constructor * pre: none * post: A Disk object has been created with radius r and * thickness t. */ public Disk(double r, double t) { super(r); thickness = t; } /** * Changes the thickness of the disk. * pre: none * post: Thickness has been changed. */ public void setThickness(double newThickness) { thickness = newThickness; }

9 /** * Returns the thickness of the disk. * pre: none * post: The thickness of the disk has been returned. */ public double getThickness() { return(thickness); } /** * Returns the volume of the disk. * pre: none * post: The volume of the disk has been returned. */ public double volume() { double v; v = super.area() * thickness; return(v); }

10 /** * Determines if the object is equal to another Disk * object. * pre: d is a Disk object. * post: true has been returned if objects have the same * radii and thickness. false has been returned otherwise. */ public Boolean equals(Object d) { Disk testObj = (Disk)d; if (testObj.getRadius() == super.getRadius() && testObj.getThickness() == thickness) { return(true); } else { return(false); }

11 /** * Returns a String that represents the Disk object. * pre: none * post: A string representing the Disk object has been * returned. */ public String toString() { String diskString; diskString = “The disk has radius “ + super.getRadius() + “ and thickness “ + thickness + “.”; return(diskString); }

12 In a subclass, the keyword super is used to access methods of the base class. For example, the statement super(r) calls the constructor of the superclass, Circle, and passes an argument for setting the radius value. Members that are declared private are not accessible to derived classes. Therefore, accessor methods are used to get inherited member variable values. For example, the equals() method is n the Disk class calls getRadius(). Inherited methods are called directly from an object, just as any method of the class is called. Whether a method is original to the Disk class or inherited from the Circle class, it is transparent to client code, as demonstrated in the TesksDisk application on the next slide.

13 public class TestDisk { public static void main(String[] args) { Disk saucer = new Disk(10, 0.02); System.out.println(“Disk radius: “ + saucer.getRadius()); System.out.println(“Disk surface area: “ + saucer.area()); System.out.println(“Disk volume: “ + saucer.volume()); Disk plate1 = new Disk(12, 0.05); Disk plate2 = new Disk(12, 0.07); if (plate1.equals(plate2)) { System.out.println(“Objects are equal.”); } else { System.out.println(“Objects are not equal.”); } System.out.println(plate1); System.out.println(plate2); }

14 The TestDisk application displays the following output: Disk radius: 10.0 Disk surface area: 314.0 Disk volume: 6.28 Objects are not equal. The disk has radius 12.0 and thickness 0.05. The disk has radius 12.0 and thickness 0.07.

15 Polymorphism Polymorphism is an OOP property in which objects have the ability to assume different types. In object-oriented programming, polymorphism is based on inheritance. Because a subclass is derived from a superclass, a superclass object can reference an object of the subclass. For example, the following statements are valid because Disk inherits Circle: Circle wafer; Disk cookie = new Disk(2, 0.5); wafer = cookie;//wafer now references cookie The wafer object, declared a Circle, is polymorphic, as demonstrated in the statement wafer = cookie where wafer assumes the form of cookie, a Disk object.

16 Polymorphism Polymorphism is further demonstrated when the referenced object determines which method to execute. This is possible when a subclass overrides a superclass method. In this case, the Disk class has overridden the equals() and toString() methods. Because of this, the following statement executes the Disk toString() method even though wafer was declared a Circle object: /* displays: The disk has radius 2.0 and thickness 0.5 */ System.out.println(wafer)

17 Polymorphism To further demonstrate polymorphism, the Music application will be developed in the succeeding slides. The Music application allows the user to assemble a small band. The user can assign a band member either vocals (voice) or a woodwind instrument (piccolo or clarinet). The user can then select to hear either a solo, duet, or trip performance from this band. The Instrument class, its subclasses, and the Performance class are used to model the objects for the Music application. The diagram on the next slide illustrates the client code and classes for the Music application. Note the hierarchy of the Instrument class and its subclasses.

18 Performance Music Instrument VocalWoodwind PiccoloClarinet Client code class superclass

19 /** * Music.java */ import java.util.Scanner public class Music { /**Returns a selected instrument. * pre: none * post: An instrument object has been returned. */ public static Instrument assignInstrument() { String instrumentChoice; Scanner input = new Scanner(System.in); System.out.println(“Select an instrument for the band member. ”); System.out.println(“Vocals, Piccolo, or Clarinet: “); instrumentChoice= input.nextLine(); if (instrumentChoice.equalsIgnoreCase(“V”)) { return(new Vocal(name)); } else if (instrumentChoice.equalsIgnoreCase(“P”)) {

20 return(new Piccolo(name)); } else { //default to clarinet return(new Clarinet(name)); } public static void main(String[] args) { Performance band; Instrument bandMember1, bandMember2, bandmember3; Scanner input = new Scanner(System.in); String performanceChoice; /* assign instruments */ bandmember1 = assignInstrument(); bandmember2 = assignInstrument(); bandmember3 = assignInstrument(); System.out.println(bandMember1 + “ “ + bandMember2 + “ “ + bandMember3 + “\n”); System.out.print(“Would you like to hear a Solo, a Duet, a Trio, or Leave? “); performanceChoice = input.nextLine();

21 while (!performanceChoice.equalsIgnoreCase(“L”)) { if (performanceChoice.equalsIgnoreCase(“S”)) { band = new Performance(bandmember1); } else if(performanceChoice.equalsIgnoreCase(“D”)) { band = new Performance(bandMember1, bandMember2); } else { //default to trio band = new Performance(bandmember1, bandMember2, bandMember3); } band.begin(); System.out.println(“\nWould you like to hear a Solo, a Duet, a Trio, or Leave? “); performanceChoice = input.nextLine(); } The assignInstrument() method declares an Instrument return type, but the individual return statements return Vocal, Piccolo, and Clarinet types. The Instrument object returned by the method is polymorphic, changing to whichever subclass is actually returned.

22 The Music application produces output similar to: Select an instrument for the band member. Vocals, Piccolo, or Clarinet: c Enter the band member’s name: Anthony Select an instrument for the band member. Vocals, Piccolo, or Clarinet: v Enter the band member’s name: Lyn Select an instrument for the band member. Vocals, Piccolo, or Clarinet: p Enter the band member’s name: Dana Anthony plays squawk. Lyn sings LaLaLa. Dana plays peep. Would you like to hear a Solo, a Duet, a Trio, or Leave? s squawk Would you like to hear a Solo, a Duet, a Trio, or Leave? d squawkLaLaLa Would you like to hear a Solo, a Duet, a Trio, or Leave? t squawkLaLaLapeep Would you like to hear a Solo, a Duet, a Trio, or Leave? l

23 The Music application allows the user numerous combinations for selecting a band and hearing performances. The code for such an application would be more complicated and less flexible without the object-oriented principles of inheritance and polymorphism. Music is versatile because ti takes advantage of inheritance and polymorphism. The documentation for the Instrument, Vocal, Woodwind, Piccolo, and Clarinet classes are on the next slides. Note that the makeSound() method in the Instrument class is a method that must be implemented (written) in a subclass. This is discussed further in the next slides. The code for the classes is also shown on the next slides where abstract classes are discussed.

24 Class Instrument Constructor/Methods Instrument(String name) creates an instrument object with musician name. getMusician() returns a string that is the musician’s name. makeSound() an abstract method that should return a String representing the instrument’s sound. Class Vocal (inherits Instrument) Constructor/Methods Vocal(String name) creates a singer object with singer name. makeSound() returns the String LaLaLa. toString() returns a String that represents the singer. Class Woodwind (inherits Instrument) Constructor/Methods Woodwind(String name) creates a woodwind instrument object with musician name. makeSound() returns the String toot.

25 Class Piccolo (inherits Woodwind) Constructor/Methods Piccolo(String name) creates an piccololoist object with musician name. makeSound() returns the String peep. toString() returns a String that represents the object. Class Clarinet (inherits Woodwind) Constructor/Methods Clarinet(String name) creates a clarinetist object with musician name. makeSound() returns the String squawk. toString() returns a String that represents the object.

26 The Peformance class creates an arrangement of Instrument objects. The constructors require Instrument objects, but polymorphism enables objects of Instructor subclasses to be passed: /** * Performance class */ public class Performance{ private String arrangement; private Instrument solo; private Instrument duet_1, duet_2; private Instrument trio_1, trio_2, trio_3; /** * constructor * pre: none * post: A soloist has been selected */ public Performance(Instrument s) { solo = s; arrangement = solo.makeSound(); }

27 /** * constructor * pre: none * post: The members of a duet have been selected. */ public Performance(Instrument d1, Instrument d2) { duet_1 = d1; duet_2 = d2; arrangement = duet_1.makeSound() + duet_2.makeSound(); } /** * constructor * pre: none * post: The members of a trio have been selected. */ public Performance(Instrument t1, Instrument t2, Instrument t3) { trio_1 = t1; trio_2 = t2; trio_3 = t3; arrangement = trio_1.makeSound() + trio_2.makeSound() + trio_3.makeSound(); }

28 /** * Begins the performance * pre: none * post: The performance has been played. */ public void begin() { System.out.println(arrangement); } /** * Returns a String that represents the performers. * pre: none * post: A string representing the performers has been * returned. */ public String toString() { String program = “The performance includes “; program += arrangement; return(program); }

29 Abstract Classes An abstract class models an abstract concept. For example, a musical instrument is an abstract concept. An instrument is something that can be played, but there is no such thing an “instrument” instrument. There are however, flutes, piccolos, drums, and cymbals. Abstract classes cannot be instantiated because they should not represent objects. They instead describe the more general details and actions of a type of object. For example, the Instrument class describes the very basics of an instrument—it can make a sound. The Woodwind class is also an abstract class because it describes a group of instruments. It includes a general sound that woodwind instruments make. Abstract classes are declared with the keyword abstract in the class declaration. They are intended to be inherited.

30 Abstract Classes The public members of the abstract class are visible to derived objects. However, an abstract class can also contain an abstract method. An abstract method is declared with the keyword abstract and contains a method declaration, but no body. The abstract class must be implemented in its subclass.

31 The Instrument class is an abstract class with an abstract method. The makeSound() method must be implemented in an Instrument subclass: /** * Instrument class. */ abstract class Instrument { String musician; /** * constructor * pre: none * post: A musician has been assigned to the instrument. */ public Instrument(String name) { musician = name; } /** * Returns the name of the musician * pre: none * post: The name of the musician playing the instrument * has been returned. */ public String getMusician() { return(musician); }

32 /** * Should return the sound of the instrument. * pre: none * post: The sound made by the instrument is returned. */ abstract String makeSound(); } The Vocal class is a subclass of Instrument. It provides the body for the makeSound() method: /** * Vocal class. */ public class Vocal extends Instrument { /** * constructor * pre: none * post: A singer has been created. */ public Vocal(String singerName) { super(singerName); }

33 /** * Returns the sound of the instrument. * pre: none * post: The sound made by the singer. */ public String makeSound() { return(“LaLaLa”); } /** * Returns a String that represents the instrument. * pre: none * post: A string representing the singer. */ public String toString() { return(super.getMusician() + “ sings “ + makeSound() + “.”);

34 The Piccolo class is a subclass of Woodwind. It overrides the makeSound() method: /** * Piccolo class. */ public class Piccolo extends Woodwind { /** * constructor * pre: none * post: A piccolo has been created. */ public Piccolo(String piccoloist) { super(piccoloist); } /** * Returns the sound of the instrument. * pre: none * post: The sound made by the instrument is returned. */ public String makeSound() { return(“peep”); }

35 /** * Returns a String that represents the instrument. * pre: none * post: A string representing the instrument has been * returned. */ public String toString() { return(super.getMusician() + “ plays “ + makeSound() + “.”); } The Clarinet class is also a Woodwind subclass. It too overrides the makeSound() method: /** * Clarinet class */ public class Clarinet extends Woodwind { /** * constructor * pre: none * post: A clarinet has been created. */ public Clarinet(String clarinetist) { super(clarinetist); }

36 /** * Returns the sound of the instrument. * pre: none * post: The sound made by the instrument is returned. */ public String makeSound() { return(“squawk”); } /** * Returns a String that represents the instrument. * pre: none * post: A string representing the instrument has been * returned. */ public String toString() { return(super.getMusician() + “ plays “ + makeSound() + “.”); } Through inheritance and abstraction, a hierarchy of classes can be created that begin with a general abstraction and lead to a specific object.

37 Interfaces An interface is a class with method declarations that have no implementations. Although an interface may seem similar to an abstract class, it is very different. An interface cannot be inherited. It may only be implemented in a class. An interface can add behavior to a class, but it does not provide a hierarchy for the class. An interface takes the form: interface { (method_param>); …additional methods }

38 The Comparable interface is part of the java.lang package. It contains one method: Interface Comparable (java.lang.Comparable) Method compareTo(Object obj) returns 0 when obj is the same as the object, a negative integer is returned when obj is less than the object, and a positive integer is returned when obj is greater than the object. When an interface is implemented in a class, the class must implement each method defined in the interface. In this case, the Comparable interface contains just one method. The Circle class shown on the next slides have been modified to implement the Comparable interface.

39 /** * Circle class. */ public class Circle implements Comparable { private static final double PI = 3.14; private double radius; /** * constructor * pre: none * post: A Circle object created. Radius initialized to 1. */ public Circle() { radius = 1;//default radius } …getRadius(), setRadius(), and other Circle class methods

40 /** * Determines if object c is smaller, the same, or larger * than this Circle object. * pre: c is a Circle object * post: -1 has been returned if c is larger than this * this Circle, 0 has been returned if they are the same * size, and 1 has been returned if c is smaller than this * Circle. */ public int compareTo(Object c) { Circle testCircle = (Circle)c; if (radius < testCircle.getRadius()) { return(-1); } else if (radius == testCircle.getRadius()) { return(0); } else { return(1); }

41 The TestCircle client code tests the compareTo method(): /** * The Circle class is tested. */ public class TestCircle { public static void main(string[] args) { Circle spot1 = new Circle(3); Circle spot2 = new Circle(4); if (spot1.compareTo(spot2) == 0) { System.out.println(“Objects are equal.”); } else if (spot1.compareTo(spot2) < 0) { System.out.println(“spot1 is smaller than spot2.”); } else { System.out.println(“spot1 is larger than spot2); } System.out.println(spot1); System.out.println(spot2); }

42 The TestCircle application produces the output: spot1 is smaller than spot2. Circle has radius 3.0 Circle has radius 4.0 A class can implement multiple interfaces. When more than one interface is implemented, the interface names are separated by commas in the class declaration.


Download ppt "Chapter 9 Inheritance and Polymorphism. Object-oriented programming is based on a paradigm in which objects are used to model a specification. Objects."

Similar presentations


Ads by Google