©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.

Slides:



Advertisements
Similar presentations
Introduction to OOP with Java 4th Ed, C. Thomas Wu
Advertisements

1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13  Inheritance and Polymorphism  Access Modifiers  Abstract.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Polymorphism.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 13 Inheritance and Polymorphism Animated Version.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
©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.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Intro to OOP with Java, C. Thomas Wu
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Method signature Name and parameter list public static void test() public static int test() => Syntax error, Duplicate method test You cannot declare more.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 Inheritance and Polymorphism.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Object Oriented Programming
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 13 Inheritance and Polymorphism.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Object-Oriented Programming: Inheritance and Polymorphism.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Inheritance and Polymorphism.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Inheritance and Polymorphism
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance and Polymorphism
Presentation transcript:

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces Inheritance

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 2 Software Reuse One goal of object-oriented programming is to promote reuse of existing classes Sometimes you have a class that does some of what you need –The JFrame class provides the basic functionality of a window for a GUI program Inheritance allows you to create a subclass with all the functionality of the existing class (superclass) –You can add new functionality to the subclass by adding new methods and instance data –You can modify the behavior of the subclass by overriding superclass methods

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 3 Polymorphism Roughly, this means many forms In Java programs, what it means is that a variable can refer to different kinds of objects at different times –Calling a method calls the version of the method that is appropriate for the actual type of the object. Two approaches to class polymorphism –Implement an interface –Use inheritance to extend a class Overloading methods is another form of polymorphism

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 4 Interfaces A Java interface includes only constants and abstract methods. A Java interface specifies a behavior. –Any class can implement the interface. –A class can implement any number of interfaces. A class implements an interface by providing a method body for the abstract methods listed in the interface. public class ImplementingClass implements InterfaceName { // provide definitions for all the methods in the interface }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 5 Inheritance Inheritance allows a class to re-use the methods and variables of another class. Every class inherits the methods of the Object class –toString, equals, clone,... –We can override an inherited method to make it behave appropriately for the class that inherited it you have been overriding the toString method in your classes Use the extends keyword to indicate that a class inherits from another class public class Subclass extends Superclass {} –A class can only extend one class (single inheritance)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 6 Polymorphism Example Polymorphism allows a single variable to refer to objects from different subclasses in the same inheritance hierarchy If a Cat is a Pet and a Dog is a Pet, then the following statements are valid: Pet myPet; myPet = new Dog();... myPet = new Cat(); Both inheritance and interfaces support polymorphism.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 7 Visibility Modifiers We've already seen two visibility modifiers –public data members and methods are accessible to everyone. –private data members and methods are accessible only to instances of the class. With inheritance, we need a third visibility modifier –Private members of the superclass are not visible within a subclass. –Using public members violates encapsulation principles –protected data members and methods are visible and accessible in all descendant classes.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 8 Inheritance and Member Accessibility We use the following visual representation of inheritance to illustrate data member accessibility.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 9 The Effect of Three Visibility Modifiers

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 10 Accessibility of Super from Sub Everything except the private members of the Super class is visible from a method of the Sub class.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 11 Case Study Suppose we want implement a class roster that contains both undergraduate and graduate students. Each student’s record will contain –his/her name –three test scores –final course grade The formula for determining the course grade is different for graduate students than for undergraduate students.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 12 Modeling Two Types of Students Three ways to design the classes to model undergraduate and graduate students. –We can define two unrelated classes, one for undergraduates and one for graduates. –We can model the two kinds of students by using classes that are related in an inheritance hierarchy. Superclass can be either abstract or concrete –We can define a Student interface and implement it with classes to represent graduate and undergraduate students

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 13 Classes for the Class Roster For the Class Roster sample, we design three classes: –Student –UndergraduateStudent –GraduateStudent The Student class will incorporate behavior and data common to both UndergraduateStudent and GraduateStudent objects. The UndergraduateStudent class and the GraduateStudent class will each contain behaviors and data specific to their respective objects.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 14 Inheritance Hierarchy

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 15 Creating the roster Array Maintain a class roster using an array of Student references –Each element can be instantiated as a Student, UndergraduateStudent or GraduateStudent Student [] roster = new Student[40]; roster[0] = new GraduateStudent(); roster[1] = new UndergraduateStudent(); roster[2] = new UndergraduateStudent();

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 16 Sample Polymorphic Message To compute the course grade using the roster array, we execute We don't need to know which class any particular object belongs to. –If roster[i] refers to a GraduateStudent, then getCourseGrade from GraduateStudent class is executed. –If roster[i] refers to an UndergraduateStudent, then getCourseGrade from UndergraduateStudent class is executed. for (int i = 0; i < numberOfStudents; i++) { roster[i].getCourseGrade(); }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 17 Inheritance and Constructors Constructors are not inherited by subclasses. When any constructor is called –there is an implicit call to the default constructor from the superclass. –You can explicitly call either the default constructor or another one using super(); The keyword super can also be used to explicitly call the superclass version of other methods super.toString();

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 18 Abstract Classes An interface consists of a list of methods that aren't implemented –all methods in an interface are abstract Occasionally, a superclass will have some methods that it doesn't make sense to implement –think about shapes - can't define draw until you know what the shape is We can make the superclass abstract –Use abstract keyword in method header –Some methods can be implemented –Label unimplemented methods abstract

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 19 Abstract Classes An abstract class is a class –defined with the modifier abstract –that contains an abstract method –that does not provide an implementation of an inherited abstract method An abstract method is a method with the keyword abstract, and it ends with a semicolon instead of a method body. –Private methods and static methods may not be declared abstract. No instances can be created from an abstract class. –Just as no instances can be created from an interface.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 20 Approaches to Student Classes Student is abstract -Need subclass for each type of student Student is instantiable –Use superclass for default behavior

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 21 Inheritance versus Interface The Java interface is used to share common behavior (only method headers) among the instances of different classes. –All classes that implement comparable have compareTo –All classes that implement ActionListener have actionPerformed Inheritance is used to share common code (including both data members and methods) among the instances of related classes. If an entity A is a specialized form of another entity B, then model them by using inheritance. –It should make sense to say an A is a B –Declare A as a subclass of B.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 22 Polymorphism with inheritance Two ways to provide polymorphism using inheritance –Override an inherited method to provide behavior specific to a particular subclass –Implement an abstract method to provide different behavior for different subclasses

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 23 The instanceof Operator If necessary, the instanceof operator can help us learn the class of an object. The following code counts the number of undergraduate students. int undergradCount = 0; for (int i = 0; i < numberOfStudents; i++) { if ( roster[i] instanceof UndergraduateStudent ) { undergradCount++; }