Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 6 Object-Oriented Programming (2): Polymorphism

Similar presentations


Presentation on theme: "Week 6 Object-Oriented Programming (2): Polymorphism"— Presentation transcript:

1 Week 6 Object-Oriented Programming (2): Polymorphism
MSIS 670 Object-Oriented Software Engineering Week 6 Object-Oriented Programming (2): Polymorphism In this chapter you will learn: The concept of polymorphism. To use overridden methods to effect polymorphism. To distinguish between abstract and concrete classes. To declare abstract methods to create abstract classes. How polymorphism makes systems extensible and maintainable. To determine an object's type at execution time. To declare and implement interfaces. 11/29/2018 6.1

2 Object-Oriented Programming
Polymorphism Enables developers to write programs in general fashion – handle variety of existing and yet-to-be specified classes Helps add new capabilities to a system Polymorphism enables programmers to deal in generalities and let the execution-time environment handle the specifics. Programmers can command objects to behave in manners appropriate to those objects, without knowing the types of the objects (as long as the objects belong to the same inheritance hierarchy). Polymorphism promotes extensibility: Software that invokes polymorphic behavior is independent of the object types to which messages are sent. New object types that can respond to existing method calls can be incorporated into a system without requiring modification of the base system. Only client code that instantiates new objects must be modified to accommodate new types. 11/29/2018

3 Introduction to Polymorphism
Helps build extensible systems Programs generically process objects as superclass objects Can add classes to systems easily Classes must be part of generically processed hierarchy Polymorphism enables programmers to deal in generalities and let the execution-time environment handle the specifics. Programmers can command objects to behave in manners appropriate to those objects, without knowing the types of the objects (as long as the objects belong to the same inheritance hierarchy). For example, given a base class shape, polymorphism enables the programmer to define different circumference methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the circumference method to it will return the correct results. Polymorphism is considered to be a requirement of any true object-oriented programming language (OOPL). 11/29/2018

4 final Methods and Classes
Cannot be overridden in subclass final class Cannot be superclass (cannot be extended) Class cannot inherit final classes The ability of a subclass to override a method in its superclass allows a class to inherit from a superclass whose behavior is "close enough" and then override methods as needed. For example, all classes are descendents of the Object class. Object contains the toString method, which returns a String object containing the name of the object's class and its hash code. Most, if not all, classes will want to override this method and print out something meaningful for that class. Sometimes, you don't want to completely override a method. Rather, you want to add more functionality to it. To do this, simply call the overridden method using the super keyword. A subclass cannot override methods that are declared final in the superclass (by definition, final methods cannot be overridden). If you attempt to override a final method, the compiler displays an error message and refuses to compile the program. A subclass must override methods that are declared abstract in the superclass, or the subclass itself must be abstract (next page). 11/29/2018

5 Abstract Superclasses and Concrete Classes
Abstract classes Objects cannot be instantiated Too generic to define real objects TwoDimensionalShape Provides superclass from which other classes may inherit Normally referred to as abstract superclasses Concrete classes Classes from which objects are instantiated Provide specifics for instantiating objects Square, Circle and Triangle Sometimes, a class that you define represents an abstract concept and, as such, should not be instantiated. Take, for example, food in the real world. Have you ever seen an instance of food? No. What you see instead are instances of carrot, apple, and (our favorite) chocolate. Food represents the abstract concept of things that we all can eat. It doesn't make sense for an instance of food to exist. Similarly in object-oriented programming, you may want to model an abstract concept without being able to create an instance of it. For example, the Number class in the java.lang package represents the abstract concept of numbers. It makes sense to model numbers in a program, but it doesn't make sense to create a generic number object. Instead, the Number class makes sense only as a superclass to classes like Integer and Float, both of which implement specific kinds of numbers. A class such as Number, which represents an abstract concept and should not be instantiated, is called an abstract class. An abstract class is a class that can only be subclassed-- it cannot be instantiated. An abstract class may contain abstract methods, that is, methods with no implementation. In this way, an abstract class can define a complete programming interface, thereby providing its subclasses with the method declarations for all of the methods necessary to implement that programming interface. An abstract class is not required to have an abstract method in it. But any class that has an abstract method in it or that does not provide an implementation for any abstract methods declared in its superclasses must be declared as an abstract class. 11/29/2018

6 Shape cannot be instantiated
1 // Fig. 9.22: Shape.java 2 // Definition of abstract base class Shape 3 4 public abstract class Shape extends Object { 5 // return shape's area public double area() { return 0.0; } 11 // return shape's volume public double volume() { return 0.0; } 17 // abstract method must be defined by concrete subclasses // to return appropriate shape name public abstract String getName(); 21 22 } // end class Shape Shape cannot be instantiated abstract class can have nonabstract methods for subclasses Concrete subclasses must implement method getName 11/29/2018

7 Point inherits Shape’s public methods
1 // Fig. 9.23: Point.java 2 // Definition of class Point 3 4 public class Point extends Shape { protected int x, y; // coordinates of the Point 6 // no-argument constructor public Point() { setPoint( 0, 0 ); } 12 // constructor public Point( int xCoordinate, int yCoordinate ) { setPoint( xCoordinate, yCoordinate ); } 18 // set x and y coordinates of Point public void setPoint( int xCoordinate, int yCoordinate ) { x = xCoordinate; y = yCoordinate; } 25 Point inherits Shape’s public methods protected members prevent clients from direct access (unless clients are Point subclasses or are in same package) 11/29/2018

8 Implementation of Shape’s method getName
// get x coordinate public int getX() { return x; } 31 // get y coordinate public int getY() { return y; } 37 // convert point into String representation public String toString() { return "[" + x + ", " + y + "]"; } 43 // return shape name public String getName() { return "Point"; } 49 50 } // end class Point Implementation of Shape’s method getName Point does not override methods area and volume, because points have neither area nor volume 11/29/2018

9 Case Study: Creating and Using Interfaces
Use interface Shape Replace abstract class Shape Interface Definition begins with interface keyword Classes implement an interface (and its methods) Contains public abstract methods Classes (that implement the interface) must implement these methods Later, we will learn ActionListener as a interface. If we create a class with this interface implemented but we do not define ActionPerformed() in the class, we get error message. This is because that the ActionListner is an interface and in which a public abstract method ActionPerformed() is declared, without any implementations in it. 11/29/2018

10 Classes that implement Shape must implement these methods
1 // Fig. 9.27: Shape.java 2 // Definition of interface Shape 3 4 public interface Shape { 5 // calculate area public abstract double area(); 8 // calculate volume public abstract double volume(); 11 // return shape name public abstract String getName(); 14 } Classes that implement Shape must implement these methods 11/29/2018

11 Point implements interface Shape
1 // Fig. 9.28: Point.java 2 // Definition of class Point 3 4 public class Point extends Object implements Shape { protected int x, y; // coordinates of the Point 6 // no-argument constructor public Point() { setPoint( 0, 0 ); } 12 // constructor public Point( int xCoordinate, int yCoordinate ) { setPoint( xCoordinate, yCoordinate ); } 18 // Set x and y coordinates of Point public void setPoint( int xCoordinate, int yCoordinate ) { x = xCoordinate; y = yCoordinate; } 25 // get x coordinate public int getX() { return x; } 31 Point implements interface Shape 11/29/2018

12 Implement methods specified by interface Shape
// get y coordinate public int getY() { return y; } 37 // convert point into String representation public String toString() { return "[" + x + ", " + y + "]"; } 43 // calculate area public double area() { return 0.0; } 49 // calculate volume public double volume() { return 0.0; } 55 // return shape name public String getName() { return "Point"; } 61 62 } // end class Point Implement methods specified by interface Shape 11/29/2018

13 Lab activities (Week 6) Implement an interface Shape and modify MyRectangle as necessary. Test the new MyRectangle like the example. 11/29/2018


Download ppt "Week 6 Object-Oriented Programming (2): Polymorphism"

Similar presentations


Ads by Google