What is Inheritance A form of software reuse


















- Slides: 18
What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class’ data and behaviors Enhance with new or modified capabilities Used to eliminate redundant code Example Circle class inherits from Shape class Circle extends Shape 1
Subclass and Superclass Subclass extends superclass Subclass – – Also called child class or derived class More specialized group of objects Inherits data and behaviors from superclass Can add or modify behaviors (methods) Superclass – – Also called parent class or base class Typically represents larger group of objects Supplies data and behaviors to subclass Direct and indirect Single and multiple inheritance 2
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. to. String(), . clone(), . equals() compares addresses, not the contents of objects 3
“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 4
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 dog. Leash; } 5
Inheritance Dog extends (“is-a”) Animal’s Stuff Dog’s Stuff 6
public class Animal { private int num. Legs; public int get. Legs() { return num. Legs; } public void set. Legs(int legs) { num. Legs = legs; } public String noise() { return “? ”; } public class Dog extends Animal { public static void main (String[] args) { Dog d = new Dog(); d. set. Legs(4); System. out. println ("A dog has " + d. get. Legs() + " legs"); System. out. println ("A dog says " + d. noise()); } public String noise() { return "WOOF!"; } } } 7
Inheritance Examples 8
An Inheritance (Class) Hierarchy Community. Member Employee Faculty Administrator Fig. 9. 2 Student Alumnus Staff Teacher Inheritance hierarchy for university Community. Members. 9
Draw an Inheritance Hierarchy for Triangle these classes: Sphere Two. Dimensional. Shape Pyramid Square Three. Dimensional. Shape Cube Circle Sphere Shape Two. Dimensional. Shape Circle Square Three. Dimensional. Shape Triangle Sphere Cube Pyramid 10
Try one more! Walking. Bird Owl Ostrich Parrot Bird Flying. Bird Chicken Talking. Parrot Bird Walking. Bird Ostrich Chicken Flying. Bird Parrot Owl Talking. Parrot 11
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 12
Bird Walking. Bird call: ? color: ? food: ? movement: walk call: ? color: ? food: ? movement: ? Chicken Ostrich call: cluck color: white food: bugs call: neek-neek color: brown food: grass Flying. Bird call: ? color: ? food: ? movement: fly Parrot Owl call: Squawk color: ? food: fruit call: hoo color: brown food: mice Talking. Parrot. . . 13
protected Members Variables or methods available only to derived classes Intermediate level of protection between public and private Accessible to superclass members subclass members 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 14
protected Members Class. C Class. A. m 1() Class. A. m 3() is-a Class. A public m 1() private m 2() protected m 3() has-a Class. B Class. A. m 1() 15
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 Example: Student extends Person 16
Software Engineering with Inheritance Customizing existing software Inherit from existing classes Include additional members Redefine superclass members No direct access to superclass source code Independent software vendors (ISVs) Develop proprietary code for sale/license Users derive new classes – Without accessing ISV proprietary source code 17
Quiz Private/Static/ Constructor _____ methods are not inherited. Object class is at the top of the Java The _____ hierarchy. extends keyword is used in Java to The _____ represent inheritance. A Java class _____ cannot inherit from more than one class. A variable or method that is available only to derived classes is called a protected member ______. 18