Week 6 Object-Oriented Programming (2): Polymorphism

Slides:



Advertisements
Similar presentations
Inheritance Inheritance Reserved word protected Reserved word super
Advertisements

Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 27 - Java Object-Oriented Programming Outline.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
UML Class Diagram: class Rectangle
Chapter 10 Classes Continued
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Inheritance & Polymorphism1. 2 Introduction Besides composition, another form of reuse is inheritance. With inheritance, an object can inherit behavior.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Object Oriented Programming: Inheritance Chapter 9.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Coming up: Inheritance
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
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.
Inheritance ndex.html ndex.htmland “Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Object-Oriented Programming: Polymorphism Chapter 10.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
1 More About Derived Classes and Inheritance Chapter 9.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Polymorphism, Interfaces & Operator Overloading
Inheritance ITI1121 Nour El Kadri.
Inheritance and Polymorphism
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object-Oriented Programming: Polymorphism
Object Oriented Programming
Lecture 23 Polymorphism Richard Gesick.
One class is an extension of another.
Object-Oriented Programming: Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
MSIS 670 Object-Oriented Software Engineering
Advanced Java Topics Chapter 9
Object Oriented Programming: Inheritance
Week 3 Object-based Programming: Classes and Objects
Java Inheritance.
Fundaments of Game Design
Chapter 9 Carrano Chapter 10 Small Java
Advanced Inheritance Concepts
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Inheritance Part 2.
Final and Abstract Classes
Presentation transcript:

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

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

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

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

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

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 6 // return shape's area 7 public double area() 8 { 9 return 0.0; 10 } 11 12 // return shape's volume 13 public double volume() 14 { 15 return 0.0; 16 } 17 18 // abstract method must be defined by concrete subclasses 19 // to return appropriate shape name 20 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

Point inherits Shape’s public methods 1 // Fig. 9.23: Point.java 2 // Definition of class Point 3 4 public class Point extends Shape { 5 protected int x, y; // coordinates of the Point 6 7 // no-argument constructor 8 public Point() 9 { 10 setPoint( 0, 0 ); 11 } 12 13 // constructor 14 public Point( int xCoordinate, int yCoordinate ) 15 { 16 setPoint( xCoordinate, yCoordinate ); 17 } 18 19 // set x and y coordinates of Point 20 public void setPoint( int xCoordinate, int yCoordinate ) 21 { 22 x = xCoordinate; 23 y = yCoordinate; 24 } 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

Implementation of Shape’s method getName 26 // get x coordinate 27 public int getX() 28 { 29 return x; 30 } 31 32 // get y coordinate 33 public int getY() 34 { 35 return y; 36 } 37 38 // convert point into String representation 39 public String toString() 40 { 41 return "[" + x + ", " + y + "]"; 42 } 43 44 // return shape name 45 public String getName() 46 { 47 return "Point"; 48 } 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

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

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 6 // calculate area 7 public abstract double area(); 8 9 // calculate volume 10 public abstract double volume(); 11 12 // return shape name 13 public abstract String getName(); 14 } Classes that implement Shape must implement these methods 11/29/2018

Point implements interface Shape 1 // Fig. 9.28: Point.java 2 // Definition of class Point 3 4 public class Point extends Object implements Shape { 5 protected int x, y; // coordinates of the Point 6 7 // no-argument constructor 8 public Point() 9 { 10 setPoint( 0, 0 ); 11 } 12 13 // constructor 14 public Point( int xCoordinate, int yCoordinate ) 15 { 16 setPoint( xCoordinate, yCoordinate ); 17 } 18 19 // Set x and y coordinates of Point 20 public void setPoint( int xCoordinate, int yCoordinate ) 21 { 22 x = xCoordinate; 23 y = yCoordinate; 24 } 25 26 // get x coordinate 27 public int getX() 28 { 29 return x; 30 } 31 Point implements interface Shape 11/29/2018

Implement methods specified by interface Shape 32 // get y coordinate 33 public int getY() 34 { 35 return y; 36 } 37 38 // convert point into String representation 39 public String toString() 40 { 41 return "[" + x + ", " + y + "]"; 42 } 43 44 // calculate area 45 public double area() 46 { 47 return 0.0; 48 } 49 50 // calculate volume 51 public double volume() 52 { 53 return 0.0; 54 } 55 56 // return shape name 57 public String getName() 58 { 59 return "Point"; 60 } 61 62 } // end class Point Implement methods specified by interface Shape 11/29/2018

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