Docstoc

Inheritance

Document Sample
Inheritance Powered By Docstoc
					    CSCI 143

OOP – Inheritance


                    1
          What is Inheritance?
• A form of software reuse
• Create a new class from an existing class
  – Absorb existing class data and methods
  – Enhance with new or modified capabilities
• Used to eliminate redundant code
• Example
  – Dog class inherits from Animal class
  – Dog extends Animal

                                                2
       Subclass and Superclass
– Subclass extends superclass
   • Subclass
      –   Also called child class or derived class
      –   More specialized group of objects
      –   Inherits data and methods from superclass
      –   Can add or modify methods
            » Modifying methods is called overriding
   • Superclass
      –   Also called parent class or base class
      –   Typically represents larger group of objects
      –   Supplies data and behaviors to subclass
      –   May be direct or indirect
– Java does not support multiple inheritance
                                                         3
            The Object class
• Top of the Java class hierarchy
• Located in package java.lang
• Class from which every other Java class
  inherits
• A class implicitly extends Object if no other
  class is specified
• .toString(), .clone(),
  .equals()
                                                  4
  “is-a” vs. “has-a” relationships
– “is-a”
   • Represents inheritance
   • subclass object is an example of the superclass
     object
   • Example: a Car is a Vehicle
   • Car is subclass; Vehicle is superclass
   • Keywords: extends, implements
– “has-a”
   • Represents composition
   • Object contains one or more objects of other
     classes as members
   • Example: Car has a Steering Wheel

                                                       5
               Composition
• Dog “has-a” owner, mother, father, leash…

                public class Dog
                {
                  private String name;
                  private int age;
                  private Person owner;
                  private Dog mother, father;
                  private Leash dogLeash;
                }




                                                6
                 Inheritance
• Dog extends (“is-a”) Animal
       Animal

                 Animal’s Stuff




         Dog
                 Animal’s Stuff

                  Dog’s Stuff




                                  7
                                                  This is an overridden method,
public class Animal                               Inherited from animal.
{
  private String name;
                                       public class Dog extends Animal
    public String getName()
                                       {
    {
                                         public String talk()
      return name;
                                         {
    }
                                           return "WOOF!";
                                         }
    public void setName(String name)
    {
                                           public void fetch(String toy)
      this.name = name;
                                           {
    }
                                             System.out.println(“Fetching a ” + toy);
                                           }
    public String talk()
                                       }
    {
      return “?”;
    }                                                      This is a new method.
}

                                                                                   8
 public class PetStore
 {
   public static void main (String[] args)
   {
       Dog d = new Dog();
       d.setName(“Henry”);
       System.out.println (d.getName() + " says "
       + d.talk());
   }
}
                            Henry says WOOF!


                                                    9
An Inheritance (Class) Hierarchy
                                              CommunityMember




                                    Employee      Student   Alumnus




                          Faculty              Staff




             Administrator          Teacher



Fig. 9.2   Inheritance hierarchy for university CommunityMembers.

                                                                      10
Draw an Inheritance
Hierarchy for these
     classes:
                      Triangle
                      Sphere
                      2DShape
                      Shape
                      Pyramid
                      Square
                      3DShape
                      Cube
                      Circle

                                 11
 Create an
inheritance
hierarchy…

 •   Automobile
 •   Plant
 •   Bird
 •   ???

                  12
                   Strategy
• Design classes for objects
• Identify characteristics classes have in
  common
  – Abstraction: focus on commonalities among
    objects in a system
• Design superclasses to store common
  characteristics


                                                13
Inherited           Not Inherited
Public members      Constructors
Private data        Private methods
Protected members   Static members




                                      14
         protected Members
– Intermediate level of protection between public and
  private
– Accessible to
   • superclasses
   • subclasses
   • classes in the same package
– Use super. to access a superclass method that has been
  overridden by a subclass method
– Don’t use protected instance variables!
   • “Fragile” software can “break” if superclass changes




                                                            15
           protected Members

Poodle       is-a   Dog                      Owner
                                     has-a
dog.m1()            public m1()              dog.m1()
dog.m3()            private m2()
                    protected m3()




                                                        16
    Constructors in Subclasses
– Constructors are not inherited!
– Chain of constructor calls
   • subclass constructor invokes superclass constructor
      – Implicitly or explicitly
      – To call explicitly, use super()
      – Superclass constructor call must be first statement in subclass
        constructor
   • Object constructor is always fired last
– All instance variables are inherited
   • Private variables not directly accessible

                                                                      17
                         Quiz
  Private/Static/
• _________
  Constructor  methods are not inherited.
• The _____ class is at the top of the Java
        Object
  hierarchy.
• The extends keyword is used in Java to represent
       _____
  inheritance.
• A Java class _____ inherit from more than one
                cannot
  class.
• A variable or method that is available within an
  inheritance hierarchy and its package is a
  ____________.
  protected member

                                                     18

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:8
posted:8/9/2011
language:English
pages:18