Chapter 9 Advanced Java Topics 2006 Pearson AddisonWesley

  • Slides: 25
Download presentation
Chapter 9 Advanced Java Topics © 2006 Pearson Addison-Wesley. All rights reserved 1

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

Core OO Concepts • • Inheritance Encapsulation Polymorphism Abstraction I ask what these are

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

Inheritance Revisited • Inheritance – Allows a class to derive the behavior and structure

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

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

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

Inheritance Revisited • Superclass or base class – A class from which another class

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 5

Inheritance Revisited • A subclass – Can add new members to those it inherits

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 6

Inheritance Revisited Figure 9 -2 The subclass Ball inherits members of the superclass Sphere

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 7

Inheritance Revisited • A subclass inherits private members from the superclass, but cannot access

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 8

Inheritance Revisited Figure 9 -3 An object invokes the correct version of a method

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

Java Access Modifiers • Membership categories of a class – Public members can be

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 class • Methods of other classes in the same package • Methods of the subclass © 2006 Pearson Addison-Wesley. All rights reserved 10

Java Access Modifiers Figure 9 -4 Access to public, protected, package access, and private

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 11

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

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

Java Access Quiz Object public to. String public hash. Code() public equals(Object eq) Elephant

Java Access Quiz Object public to. String public hash. Code() public equals(Object eq) Elephant extends Mammal public to. String private open. Mouth() protected int get. Weight get. Brother() Mammal extends Object private open. Mouth() protected int get. Weight get. Brother() get. Sister() public get. Ears() public to. String() Inside the Cow class what methods can I call? Which specific implementations are being executed? (From which class) Cow extends Mammal public moo() private open. Mouth() protected int get. Weight get. Brother() © 2006 Pearson Addison-Wesley. All rights reserved 13

Java Access Quiz Object Mammal extends Object private open. Mouth() public to. String protected

Java Access Quiz Object Mammal extends Object private open. Mouth() public to. String protected int get. Weight public hash. Code() get. Brother() public equals(Object eq) get. Sister() public get. Ears() public to. String() Elephant extends Mammal public to. String private open. Mouth() protected int get. Weight get. Brother() Cow extends Mammal public moo() private open. Mouth() protected int get. Weight get. Brother() © 2006 Pearson Addison-Wesley. All rights reserved BOLD are answers to last slide. What if I am in My. Class instead of the Cow class using Cow c = new Cow()? (Same package as Cow. ) Which specific implementations are being executed? (From which class) 14

Java Access Quiz Object Mammal extends Object private open. Mouth() public to. String protected

Java Access Quiz Object Mammal extends Object private open. Mouth() public to. String protected int get. Weight public hash. Code() get. Brother() public equals(Object eq) get. Sister() public get. Ears() public to. String() Elephant extends Mammal public to. String private open. Mouth() protected int get. Weight get. Brother() Cow extends Mammal public moo() private open. Mouth() protected int get. Weight get. Brother() © 2006 Pearson Addison-Wesley. All rights reserved Inside the Cow class: Cow c = new Cow(); What if I am in My. Class instead of the Cow class? (Same package as Cow. ) What if My. Class is NOT in the same package as Cow? Which specific implementations are being executed? (From which class) 15

Java Access Quiz Object Mammal extends Object private open. Mouth() public to. String protected

Java Access Quiz Object Mammal extends Object private open. Mouth() public to. String protected int get. Weight public hash. Code() get. Brother() public equals(Object eq) get. Sister() public get. Ears() public to. String() Elephant extends Mammal public to. String private open. Mouth() protected int get. Weight get. Brother() Cow extends Mammal public moo() private open. Mouth() protected int get. Weight get. Brother() © 2006 Pearson Addison-Wesley. All rights reserved Inside the Cow class: Cow c = new Cow(); What if I am in My. Class instead of the Cow class? (Same package as Cow. ) On your own - What if My. Class extends Cow? Which specific implementations are being executed? (From which class) 16

Java Access Quiz - Attributes Object Mammal extends Object private int weight; public String

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

Java Access Quiz - Attributes Object Mammal extends Object BOLD Answers are for previous

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

Java Access Quiz - Attributes Object Mammal extends Object BOLD Answers are for previous

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

Java Access Quiz - Attributes Object Mammal extends Object BOLD Answers are for previous

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

Is-a and Has-a Relationships • Two basic kinds of relationships – Is-a relationship –

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

Is-a Relationship • Inheritance should imply an is-a relationship between the superclass and the

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 22

Is-a Relationship • Object type compatibility – An instance of a subclass can be

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 Analog. Clock, but I cannot use an Analog. Clock as a Cuckoo. Clock. (Because a Cuckoo. Clock needs a “make. Cuckoo. Sound()” method, that all Analog. Clocks will not have. © 2006 Pearson Addison-Wesley. All rights reserved 23

Has-a Relationships Figure 9 -6 A pen “has a” or “contains a” ball ©

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

Has-a Relationships • Has-a relationship – Also called containment – Cannot be implemented using

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 my. Ball; } © 2006 Pearson Addison-Wesley. All rights reserved 25