JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN 0136130887 © 2008 Pearson Education, Inc., Upper.

Slides:



Advertisements
Similar presentations
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Advertisements

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
OOP: Inheritance By: Lamiaa Said.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 10: Inheritance and Polymorphism
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Coming up: Inheritance
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance Chapter 7. Outline Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 11: Inheritance and Polymorphism
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Inheritance, Polymorphism, and Interfaces
Inheritance Basics Programming with Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Inheritance, Polymorphism, and Interfaces. Oh My
Computer Programming with JAVA
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Presentation transcript:

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Walter Savitch Frank M. Carrano Walter Savitch Frank M. Carrano Polymorphism and Inheritance Chapter 8

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Objectives Describe polymorphism and inheritance in general Define interfaces to specify methods Describe dynamic binding Define and use derived classes in Java

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Polymorphism: Outline Class Interfaces Java Interfaces Implementing an Interface An Interface as a Type Extending an Interface

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Class Interfaces … Consider a set of behaviors for pets  Move  Eat  Make a noise We could specify method headings for these behaviors These method headings can form a class interface

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Class Interfaces Now consider different classes that implement this interface  They will each have the same behaviors  Nature of the behaviors will be different Each of the classes implements the behaviors/methods differently

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Java Interfaces … Are program components that contain headings for a number of public methods, and include comments that describe those methods Can also define public named constants View example interface in listing 8.1 ( interface Measurable )example interface

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Java Interfaces Interface names follow the same rules for capitalization as class names Also stored in a file with suffix.java An interface does not include  Declarations of constructors  Instance variables  Method bodies

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Implementing an Interface To implement an interface, a class must  Include the phrase implements Interface_name  Define each specified method View sample class, listing 8.2 class Rectangle implements Measurablesample class View another class in listing 8.3 which also implements Measurable class Circleanother class

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved An Interface as a Type It is possible to write a method that has a parameter which is an interface type, since an interface is a reference type When a program invokes that method, it may pass to the method an object of any class which implements that interface, which is one form of polymorphism This is made possible by dynamic binding, also known as late binding

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Method with Interface Parameter public static void display(Measurable figure) { double perimeter = figure.getPerimeter(); double area = figure.getArea(); System.out.println(″Perimeter = ″ + perimeter + ″\nArea = a″ + area); }

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Extending an Interface It is possible to define a new interface which builds on an existing interface  It is said to extend the existing interface A class that implements the new interface must implement all the methods of both interfaces

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Inheritance Basics: Outline Derived Classes Overriding Method Definitions Overriding Versus Overloading The final Modifier Private Instance Variables and Private Methods of a Base Class UML Inheritance Diagrams

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Inheritance Basics Inheritance allows programmer to define a general class, and then later define a more specific class by adding new details to the general definition The new, more specific, class inherits all properties of the initial, more general class View example class, listing 8.4 class Personexample class

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Derived Classes … Class Person used as a base class  Also called superclass or parent class Now we declare derived class Student  Also called subclass or child class  Inherits methods from the superclass View derived class, listing 8.5 class Student extends Personderived class View demo program, listing 8.6 class InheritanceDemodemo program Sample screen output

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Derived Classes Figure 8.1 A class hierarchy

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Overriding Method Definitions Note method writeOutput in class Student Class Person also has a method with that name The method in the subclass with same signature overrides the method from base class, and the overriding method is the one used for objects of the derived class The overriding method must return same type of value

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Overriding Versus Overloading Do not confuse overriding with overloading Overriding  Takes place in a subclass when you have a new method with the same signature as a method in the base class Overloading  Takes place in the same class when you have two or more methods with the same name but different signatures (i.e., different parameter lists)

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved The final Modifier It is possible to specify that a method cannot be overridden in a subclass Add the modifier final to the heading public final void specialMethod() An entire class may be declared final, which would mean that this class cannot be used as a base class to derive any other class

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Private Instance Variables and Methods Consider private instance variable in a base class  It is not inherited/available in subclass  It can be manipulated only by public accessor, modifier methods Similarly, private methods in a superclass not inherited/available in subclass

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved UML Inheritance Diagrams… Figure 8.2 A class hierarchy in UML notation

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved …UML Inheritance Diagrams Figure 8.3 Some details of UML class hierarchy from figure 8.2

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Programming with Inheritance: Outline … Constructors in Derived Classes The this Method – Again Calling an Overidden Method Derived Class of a Derived Class Type Compatibility

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved …Programming with Inheritance: Outline The class Object A Better equals Method Case Study: Character Graphics Abstract Classes Dynamic Binding and Inheritance

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Constructors in Derived Classes A derived class does not inherit constructors from base class A constructor in a subclass must invoke constructor from base class Accomplished with the reserved word super  The call to super must be the first statement in the constructor

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved The this Method – Again It is also possible to use the this keyword to call any constructor in the class When used in a constructor, this calls another constructor in same class Contrast with the use of super, which invokes a constructor of the base class

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Using both this and super If this is used as a statement in a constructor, it must be the first statement. Likewise, if super is used as a statement in a constructor, it must be the first statement. So, if you want to use both, use this to call a constructor that has super as its first statement.

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Calling an Overridden Method Reserved word super can also be used to call the corresponding base class method in overriding method in a derived class This statement calls the method with the same name in the base class

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Programming Example A derived class of a derived class View sample class, listing 8.7 class Undergraduatesample class Has all public members of both  Person  Student This reuses the code in both superclasses

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Programming Example Figure 8.4 More details of the UML class hierarchy

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Type Compatibility … In the class hierarchy  Each Undergraduate is also a Student  Each Student is also a Person An object of a derived class can serve as an object of the base class  Note this is not typecasting An object of a class can be referenced by a variable of an ancestor type

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Type Compatibility Be aware of the "is-a" relationship  A Student is a Person Another relationship is the "has-a"  A class can contain (as an instance variable) an object of another type  If we specify a date of birth variable for Person – it "has-a" Date object

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved The Class Object … Java has a class that is the ultimate ancestor of every class  That class is Object Thus it is possible to write a method with parameter of type Object  Then the actual parameter in the call can be object of any type Example: method println(Object theObject)

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … The Class Object Class Object has some methods that every Java class inherits Examples  Method equals  Method toString Method toString called when println(theObject) invoked  Best to define your own toString to handle this

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved A Better equals Method The designer of a class should override method equals from Object View code of sample override, listing 8.8 public boolean equals (Object theObject)sample override

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Case Study … Character Graphics View interface for simple shapes, listing 8.9 interface ShapeInterfacesimple shapes If we wish to create classes that draw rectangles and triangles  We could create interfaces that extend ShapeInterface  View interfaces, listing 8.10interfaces

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Case Study … Now view base class, listing 8.11 which uses (implements) previous interfaces class ShapeBasicsbase class Note  Method drawAt calls drawHere  Derived classes must override drawHere  Modifier extends comes before implements

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Case Study … Figure 8.5 A sample rectangle and triangle

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Case Study … Note algorithm used by method drawHere to draw a rectangle 1. Draw the top line 2. Draw the side lines 3. Draw the bottom line Subtasks of drawHere are realized as private methods View class definition, listing 8.12 class Rectangleclass definition

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Case Study … View next class to be defined (and tested) in listing 8.13 (class Triangle)next class It is good practice to test classes as we go View demo program, listing 8.14 class TreeDemodemo program

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Case Study Sample screen output

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Abstract Classes … Class ShapeBasics is designed to be a base class for other classes  Method drawHere will be redefined for each subclass  It should be declared abstract – a method that has no body This makes the class abstract You cannot create an object of an abstract class; thus its role must be as a base class

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Abstract Classes … Not all methods of an abstract class are abstract methods Abstract class makes it easier to define a base class  Specifies the obligation of designer to override the abstract methods for each subclass

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Abstract Classes Cannot have an instance of an abstract class  But OK to have a parameter of that type View abstract version, listing 8.15 abstract class ShapeBaseabstract version

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Dynamic Binding and Inheritance… Note how drawAt (in ShapeBasics ) makes a call to drawHere Class Rectangle overrides method drawHere  How does drawAt know where to find the correct drawHere ? Happens with dynamic or late binding  Address of correct code to be executed determined at run time

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved …Dynamic Binding and Inheritance When an overridden method is invoked  Action matches method defined in class used to create object using new  Not determined by type of variable naming the object Variable of any ancestor class can reference an object of a descendant class  The object always remembers which method actions to use for each method name

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Summary … An interface contains  Headings of public methods  Definitions of named constants  No constructors, no private instance variables Class which implements an interface must  Define a body for every interface method specified Interface enables designer to specify methods for another programmer

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Summary … Interface is a reference type  Can be used as variable or parameter type Interface can be extended to create another interface Dynamic (late) binding enables objects of different classes to substitute for one another  Must have identical interfaces  Called polymorphism

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Summary … Derived class obtained from base class by adding instance variables and methods  Derived class inherits all public elements of base class Constructor of derived class must first call a constructor of base class  If not explicitly called, Java automatically calls default constructor

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Summary … Within constructor  this calls constructor of same class  super invokes constructor of base class Method from base class can be overridden  Must have same signature If signature is different, method is overloaded

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Summary Overridden method can be called with preface of super Private elements of base class cannot be accessed directly by name in derived class Object of derived class has type of both base and derived classes Legal to assign object of derived class to variable of any ancestor type Every class is a descendant of class Object