Advanced Java Topics Chapter 9

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 211 Inheritance AAA.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
ITEC200 – Week03 Inheritance and Class Hierarchies.
Two 2x2 (block) matrices can be multiplied C==AB
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.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Chapter 10 Classes Continued
© 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 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Inheritance using Java
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
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.
Advanced C++ Topics Chapter 8. CS 308 2Chapter 8 -- Advanced C++ Topics This chapter describes techniques that make collections of reusable software components.
Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Coming up: Inheritance
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
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.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
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.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
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.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
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
Python First Edition STARTING OUT WITH Chapter 10 Inheritance
Inheritance and Polymorphism
One class is an extension of another.
Advanced C++ Topics Chapter 8.
Week 8 Lecture -3 Inheritance and Polymorphism
Advanced Java Topics Chapter 9 (continued)
CSC 205 Java Programming II
Figure 8.1 Inheritance: Relationships among timepieces.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 9 Object-Oriented Programming: Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Polymorphism Polymorphism - Greek for “many forms”
Overriding Methods & Class Hierarchies
Java Programming, Second Edition
Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Chapter 8: Class Relationships
CIS 199 Final Review.
Chapter 8 Inheritance Part 2.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Programming in C# CHAPTER 5 & 6
Figure 8.1 Inheritance: Relationships among timepieces.
Presentation transcript:

Advanced Java Topics Chapter 9 © 2006 Pearson Addison-Wesley. All rights reserved

Core OO Concepts Inheritance Encapsulation Polymorphism Abstraction I ask what these are to any programming interview candidates. © 2006 Pearson Addison-Wesley. All rights reserved

Inheritance Revisited Allows a class to derive the behavior and structure of an existing class © 2006 Pearson Addison-Wesley. All rights reserved

Inheritance Revisited Figure 9-1 Inheritance: Relationships among timepieces © 2006 Pearson Addison-Wesley. All rights reserved

Inheritance Revisited Superclass or base class A class from which another class is derived Subclass, derived class, or descendant class A class that inherits the members of another class Benefits of inheritance It enables the reuse of existing classes It reduces the effort necessary to add features to an existing object © 2006 Pearson Addison-Wesley. All rights reserved

Inheritance Revisited A subclass Can add new members to those it inherits Can override an inherited method of its superclass A method in a subclass overrides a method in the superclass if the two methods have the same declarations © 2006 Pearson Addison-Wesley. All rights reserved

Inheritance Revisited Figure 9-2 The subclass Ball inherits members of the superclass Sphere and overrides and adds methods © 2006 Pearson Addison-Wesley. All rights reserved

Inheritance Revisited A subclass inherits private members from the superclass, but cannot access them directly Methods of a subclass can call the superclass’s public methods Clients of a subclass can invoke the superclass’s public methods An overridden method Instances of the subclass will use the new method Instances of the superclass will use the original method © 2006 Pearson Addison-Wesley. All rights reserved

Inheritance Revisited Figure 9-3 An object invokes the correct version of a method © 2006 Pearson Addison-Wesley. All rights reserved

Java Access Modifiers Membership categories of a class Public members can be used by anyone Members declared without an access modifier (the default) are available to Methods of the class Methods of other classes in the same package Private members can be used only by methods of the class Protected members can be used only by Methods of the subclass © 2006 Pearson Addison-Wesley. All rights reserved

Java Access Modifiers Figure 9-4 Access to public, protected, package access, and private members of a class by a client and a subclass © 2006 Pearson Addison-Wesley. All rights reserved

Lets look at some examples in code © 2006 Pearson Addison-Wesley. All rights reserved

Elephant extends Mammal Java Access Quiz Object public toString public hashCode() public equals(Object eq) Mammal extends Object Inside the Cow class what methods can I call? Which specific implementations are being executed? (From which class) private openMouth() protected int getWeight getBrother() getSister() public getEars() public toString() Elephant extends Mammal Cow extends Mammal public moo() private openMouth() protected int getWeight getBrother() public toString private openMouth() protected int getWeight getBrother() © 2006 Pearson Addison-Wesley. All rights reserved

Elephant extends Mammal Java Access Quiz Object public toString public hashCode() public equals(Object eq) Mammal extends Object BOLD are answers to last slide. What if I am in MyClass instead of the Cow class using Cow c = new Cow()? (Same package as Cow.) Which specific implementations are being executed? (From which class) private openMouth() protected int getWeight getBrother() getSister() public getEars() public toString() Elephant extends Mammal Cow extends Mammal public moo() private openMouth() protected int getWeight getBrother() public toString private openMouth() protected int getWeight getBrother() © 2006 Pearson Addison-Wesley. All rights reserved

Elephant extends Mammal Java Access Quiz Object public toString public hashCode() public equals(Object eq) Mammal extends Object Inside the Cow class: Cow c = new Cow(); What if I am in MyClass instead of the Cow class? (Same package as Cow.) What if MyClass is NOT in the same package as Cow? Which specific implementations are being executed? (From which class) private openMouth() protected int getWeight getBrother() getSister() public getEars() public toString() Elephant extends Mammal Cow extends Mammal public moo() private openMouth() protected int getWeight getBrother() public toString private openMouth() protected int getWeight getBrother() © 2006 Pearson Addison-Wesley. All rights reserved

Elephant extends Mammal Java Access Quiz Object public toString public hashCode() public equals(Object eq) Mammal extends Object Inside the Cow class: Cow c = new Cow(); What if I am in MyClass instead of the Cow class? (Same package as Cow.) On your own - What if MyClass extends Cow? Which specific implementations are being executed? (From which class) private openMouth() protected int getWeight getBrother() getSister() public getEars() public toString() Elephant extends Mammal Cow extends Mammal public moo() private openMouth() protected int getWeight getBrother() public toString private openMouth() protected int getWeight getBrother() © 2006 Pearson Addison-Wesley. All rights reserved

Java Access Quiz - Attributes Object Mammal extends Object Inside a method in the Cow class: What attributes can I reference? private int weight; public String name; protected Mammal brother Elephant extends Mammal Cow extends Mammal private int weight; protected Elephant brother protected Mammal mother private int size public String name final char status=‘c’ © 2006 Pearson Addison-Wesley. All rights reserved

Java Access Quiz - Attributes Object Mammal extends Object BOLD Answers are for previous slide. Previous: What attributes can I reference? Next: If I have a Cow object in another class in this package, what attributes can I reference? private int weight; public String name; protected Mammal brother Elephant extends Mammal Cow extends Mammal private int weight; protected Elephant brother protected Mammal mother private int size public String name final char status=‘c’ © 2006 Pearson Addison-Wesley. All rights reserved

Java Access Quiz - Attributes Object Mammal extends Object BOLD Answers are for previous slide. Previous: If I have a Cow object in another class in this package, what attributes can I reference? Next: If I am in a subclass of Cow object in another package, what attributes can I reference? private int weight; public String name; protected Mammal brother Elephant extends Mammal Cow extends Mammal private int weight; protected Elephant brother protected Mammal mother private int size public String name final char status=‘c’ © 2006 Pearson Addison-Wesley. All rights reserved

Java Access Quiz - Attributes Object Mammal extends Object BOLD Answers are for previous slide. Previous: If I am in a subclass of Cow object in another package, what attributes can I reference? private int weight; public String name; protected Mammal brother Elephant extends Mammal Cow extends Mammal private int weight; protected Elephant brother protected Mammal mother private int size public String name final char status=‘c’ © 2006 Pearson Addison-Wesley. All rights reserved

Is-a and Has-a Relationships Two basic kinds of relationships Is-a relationship Has-a relationship © 2006 Pearson Addison-Wesley. All rights reserved

Is-a Relationship Inheritance should imply an is-a relationship between the superclass and the subclass Example: If the class Ball is derived from the class Sphere A ball is a sphere Figure 9-5 A ball “is a” sphere © 2006 Pearson Addison-Wesley. All rights reserved

Is-a Relationship Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not the other way around I can use a ball as a sphere, but I cannot substitute a sphere for a ball. I can use a Cuckoo Clock as an AnalogClock, but I cannot use an AnalogClock as a CuckooClock. (Because a CuckooClock needs a “makeCuckooSound()” method, that all AnalogClocks will not have. © 2006 Pearson Addison-Wesley. All rights reserved

Has-a Relationships Figure 9-6 A pen “has a” or “contains a” ball © 2006 Pearson Addison-Wesley. All rights reserved

Has-a Relationships Has-a relationship Also called containment Cannot be implemented using inheritance Example: To implement the has-a relationship between a pen and a ball Define a data field point – whose type is Ball – within the class Pen public class Pen { private Ball myBall; } © 2006 Pearson Addison-Wesley. All rights reserved