Chapter 7: Better Living in Objectville - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Chapter 7: Better Living in Objectville

Description:

When writing Java code, be sure to write code that others can extend easily. ... Boxers. Shirt. 6/12/09. From 'Head First Java', Sierra and Bates, (c) 2003. Chapter 7 ... – PowerPoint PPT presentation

Number of Views:121
Avg rating:3.0/5.0
Slides: 20
Provided by: systema181
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7: Better Living in Objectville


1
Chapter 7 Better Living in Objectville
  • Phil FeibishIT 3883

2
Overview
  • When writing Java code, be sure to write code
    that others can extend easily.
  • Write code which can be easily changed and
    updated.
  • Flexibility, polymorphism, and inheritance.

3
Shape example
  • Example

4
Inheritance
superclass(more abstract)

subclass(more specific)
5
Inheritance - Definition
  • Java is a pure object-oriented language.
  • In an OOP is that they are able to extend the
    functionality of existing classes.
  • This extension of functionality is called
    inheritance.
  • Rather than developing from scratch, we can use
    inheritance to add to an existing class whatever
    is needed.
  • Syntax derived-class extends base-class

6
Inheritance
  • Square inherits from Shape, or Circle inherits
    from Shape
  • Subclass extends the superclass. Subclass
    inherits the members of the superclass.
  • Shape is the superclass of the other four
  • The other classes are subclasses
  • Subclasses may contain members not included in
    superclass.
  • Inherited variables are not overridden.
  • "If the Shape class has the functionality, then
    the subclasses automatically get the same
    functionality."

7
Inheritance important point
  • By default, all Java classes are derived from a
    common class called Object.

8
Overriding
  • What if the rotate() method for an Amoeba is
    different than the rotate() method for the other
    objects?
  • You can write a different rotate() method for
    class Amoeba. This method will override the
    definition used by the Shape class.
  • Overriding "just means that a subclass redefines
    one or its inherited methods when it needs to
    change or extend the behavior of that method."

9
Inheritance example (p165)
  • public class Doctor
  • boolean worksAtHospital
  • void treatPatient()
  • // perform a checkup
  • public class FamilyDoctor extends Doctor
  • boolean makesHouseCalls
  • void giveAdvice()
  • // give homespun advice
  • public class Surgeon extends Doctor
  • void treatPatient()
  • // perform surgery

10
Implementation - Doctor
11
Another example Animals
12
Class Hierarchy pg170
  • We decide that Canines could use a common roam()
    method.
  • We also see that Felines could use a common
    roam() method
  • We'll let Hippo continue to use its inherited
    roam() method the generic from Animal

13
Which method is called?The lowest one wins.
  • Wolf w new Wolf()
  • w.makeNoise()
  • w.roam()
  • w.eat()
  • w.sleep()

14
Which method is called?
  • When you call a method on an object reference,
    you're calling the most specific version of the
    method for that object type.
  • The lowest one wins!
  • If the JVM doesn't find a version of the method
    in the Wolf class, it starts walking back up the
    inheritance hierarchy until it finds a match.

15
Inheritance Class DiagramInheritance Tree
16
Using IS-A
  • When you want to know if one thing should extend
    another, apply the IS-A test.
  • Triangle IS-A Shape? Yes.
  • Cat IS-A Feline? Yes.
  • Surgeon IS-A Feline? Yes.
  • Tub IS-A Bathroom? NO.

17
Using HAS-A test
  • Use the HAS-A test to determine if a class has an
    instance variable.
  • Bathroom HAS-A Tub? Yes.
  • Tub HAS-A Bubbles? Yes.
  • So bathroom has a reference to a Tub, but
    Bathroom does not extend Tub.

18
IS-A in Inheritance Tree
  • If class B extends class A, class B IS-A class A.
  • This is true anywhere in the inheritance tree.
  • If class C extends class B, class C passes the
    IS-A test for both B and A.
  • REMEMBER The inheritance IS-A relationship
    works in only one direction!

19
Questions?
Write a Comment
User Comments (0)
About PowerShow.com