Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chapter 1 Inheritance University Of Ha’il.
Chapter 8 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
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.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
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.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
Chapter 10 Classes Continued
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Computer Science I Inheritance Professor Evan Korth New York University.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Chapter 8 More Object Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
What is inheritance? It is the ability to create a new class from an existing class.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Interfaces.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 13 ATM Case Study Part 2: Implementing an Object-Oriented Design Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading:
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Dr. Majed Abdouli Copyright © 2015 adapted from John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
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.
Sections Inheritance and Abstract Classes
Chapter Goals To be able to declare and use interface types
Chapter 9 – Inheritance.
Chapter Three - Implementing Classes
Advanced Java Programming
Inheritance.
Inheritance, Polymorphism, and Interfaces. Oh My
Week 6 Object-Oriented Programming (2): Polymorphism
Class Inheritance Concept of Inheritance Java keywords
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 6 Inheritance.
Presentation transcript:

Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance

Copyright © 2014 by John Wiley & Sons. All rights reserved.2 Chapter Goals  To learn about inheritance  To implement subclasses that inherit and override superclass methods

Copyright © 2014 by John Wiley & Sons. All rights reserved.3 Inheritance Hierarchies  Inheritance: the relationship between a more general class (superclass) and a more specialized class (subclass).  The subclass inherits data and behavior from the superclass.  Cars share the common traits of all vehicles Example: the ability to transport people from one place to another Figure 1 An Inheritance Hierarchy of Vehicle Classes

Copyright © 2014 by John Wiley & Sons. All rights reserved.4 Inheritance Hierarchies  The class Car inherits from the class Vehicle  The Vehicle class is the superclass  The Car class is the subclass Figure 2 Inheritance Diagram

Copyright © 2014 by John Wiley & Sons. All rights reserved.5 Inheritance Hierarchies  Inheritance lets you can reuse code instead of duplicating it.  Two types of reuse A subclass inherits the methods of the superclass Because a car is a special kind of vehicle, we can use a Car object in algorithms that manipulate Vehicle objects  The substitution principle: You can always use a subclass object when a superclass object is expected.  A method that processes Vehicle objects can handle any kind of vehicle

Copyright © 2014 by John Wiley & Sons. All rights reserved.6 Inheritance Hierarchies Figure 3 Inheritance Hierarchy of Question Types  Example: Computer-graded quiz There are different kinds of questions A question can display its text, and it can check whether a given response is a correct answer. You can form subclasses of the Question class.

Copyright © 2014 by John Wiley & Sons. All rights reserved.7 section_1/Question.javaQuestion.java 1 /** 2 A question with a text and an answer. 3 */ 4 public class Question 5 { 6 private String text; 7 private String answer; 8 9 /** 10 Constructs a question with empty question and answer. 11 */ 12 public Question() 13 { 14 text = ""; 15 answer = ""; 16 } /** 19 Sets the question text. questionText the text of this question 21 */ 22 public void setText(String questionText) 23 { 24 text = questionText; 25 } 26 Continued

Copyright © 2014 by John Wiley & Sons. All rights reserved.8 section_1/Question.javaQuestion.java 27 /** 28 Sets the answer for this question. correctResponse the answer 30 */ 31 public void setAnswer(String correctResponse) 32 { 33 answer = correctResponse; 34 } /** 37 Checks a given response for correctness. response the response to check true if the response was correct, false otherwise 40 */ 41 public boolean checkAnswer(String response) 42 { 43 return response.equals(answer); 44 } /** 47 Displays this question. 48 */ 49 public void display() 50 { 51 System.out.println(text); 52 } 53 }

Copyright © 2014 by John Wiley & Sons. All rights reserved.9 section_1/QuestionDemo1.javaQuestionDemo1.java 1 import java.util.Scanner; 2 3 /** 4 This program shows a simple quiz with one question. 5 */ 6 public class QuestionDemo1 7 { 8 public static void main(String[] args) 9 { 10 Scanner in = new Scanner(System.in); Question q = new Question(); 13 q.setText("Who was the inventor of Java?"); 14 q.setAnswer("James Gosling"); q.display(); 17 System.out.print("Your answer: "); 18 String response = in.nextLine(); 19 System.out.println(q.checkAnswer(response)); 20 } 21 } 22 Continued

Copyright © 2014 by John Wiley & Sons. All rights reserved.10 section_1/QuestionDemo1.javaQuestionDemo1.java Program Run: Who was the inventor of Java? Your answer: James Gosling true

Copyright © 2014 by John Wiley & Sons. All rights reserved.11 Implementing Subclasses Like the manufacturer of a stretch limo, who starts with a regular car and modifies it, a programmer makes a subclass by modifying another class.  To get a ChoiceQuestion class, implement it as a subclass of Question Specify what makes the subclass different from its superclass. Subclass objects automatically have the instance variables that are declared in the superclass. Only declare instance variables that are not part of the superclass objects.

Copyright © 2014 by John Wiley & Sons. All rights reserved.12 Implementing Subclasses  A subclass inherits all methods that it does not override. Figure 4 The ChoiceQuestion Class is a Subclass of the Question Class.

Copyright © 2014 by John Wiley & Sons. All rights reserved.13 Implementing Subclasses  The subclass inherits all public methods from the superclass.  You declare any methods that are new to the subclass.  You change the implementation of inherited methods if the inherited behavior is not appropriate.  Override a method: supply a new implementation for an inherited method

Copyright © 2014 by John Wiley & Sons. All rights reserved.14 Implementing Subclasses A ChoiceQuestion object differs from a Question object in three ways:  Its objects store the various choices for the answer.  There is a method for adding answer choices.  The display method of the ChoiceQuestion class shows these choices so that the respondent can choose one of them.

Copyright © 2014 by John Wiley & Sons. All rights reserved.15 Implementing Subclasses  The ChoiceQuestion class needs to spell out the three differences: public class ChoiceQuestion extends Question { // This instance variable is added to the subclass private ArrayList choices; // This method is added to the subclass public void addChoice(String choice, boolean correct) {... } // This method overrides a method from the superclass public void display() {... } }  The extends reserved word indicates that a class inherits from a superclass.

Copyright © 2014 by John Wiley & Sons. All rights reserved.16 Implementing Subclasses  UML of ChoiceQuestion and Question Figure 5 The ChoiceQuestion Class Adds an Instance Variable and a Method, and Overrides a Method

Copyright © 2014 by John Wiley & Sons. All rights reserved.17 Syntax 9.1 Subclass Declaration

Copyright © 2014 by John Wiley & Sons. All rights reserved.18 Implementing Subclasses  A ChoiceQuestion object  You can call the inherited methods on a subclass object: choiceQuestion.setAnswer("2");  The private instance variables of the superclass are inaccessible.  The ChoiceQuestion methods cannot directly access the instance variable answer.  ChoiceQuestion methods must use the public interface of the Question class to access its private data.

Copyright © 2014 by John Wiley & Sons. All rights reserved.19 Implementing Subclasses  Adding a new method: addChoice public void addChoice(String choice, boolean correct) { choices.add(choice); if (correct) { // Convert choices.size() to string String choiceString = "" + choices.size(); setAnswer(choiceString); } }

Copyright © 2014 by John Wiley & Sons. All rights reserved.20 Implementing Subclasses  addChoice method can not just access the answer variable in the superclass:  It must use the setAnswer method  Invoke setAnswer on the implicit parameter: setAnswer(choiceString); OR this.setAnswer(choiceString);

Copyright © 2014 by John Wiley & Sons. All rights reserved.21 Common Error: Replicating Instance Variables from the Superclass  A subclass has no access to the private instance variables of the superclass: public ChoiceQuestion(String questionText) { text = questionText; // Error—tries to access // private superclass variable }  Beginner's error: “solve” this problem by adding another instance variable with same name  Error! public class ChoiceQuestion extends Question { private ArrayList choices; private String text; // Don’t!... }

Copyright © 2014 by John Wiley & Sons. All rights reserved.22 Common Error: Replicating Instance Variables from the Superclass  The constructor compiles, but it doesn’t set the correct text!  The ChoiceQuestion constructor should call the setText method of the Question class.

Copyright © 2014 by John Wiley & Sons. All rights reserved.23 Overriding Methods  If you are not satisfied with the behavior of an inherited method, you override it by specifying a new implementation in the subclass.  An overriding method can extend or replace the functionality of the superclass method.  The display method of the ChoiceQuestion class needs to: Display the question text. Display the answer choices.

Copyright © 2014 by John Wiley & Sons. All rights reserved.24 Overriding Methods  Problem: ChoiceQuestion's display method can’t access the text variable of the superclass directly because it is private.  Solution: It can call the display method of the superclass, by using the reserved word super public void display() { // Display the question text super.display(); // OK // Display the answer choices... }  super is a reserved word that forces execution of the superclass method.