CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010.
-
Upload
megan-hubbard -
Category
Documents
-
view
215 -
download
0
Transcript of CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010.
CIS 234: Inheritance
Dr. Ralph D. WestfallApril, 2010
What Is Inheritance? getting something from somewhere
else analogy with biological inheritance
inherit some characteristics from parents height, eye color, etc.
characteristics may be inherited from common ancestors
all breeds of dogs originally came from wolves in East Asia?
OO Programming Inheritance new class can inherit from existing
class properties (variables) methods (actions)
can add additional properties and methods to the new class
another new class can inherit from the new class that inherited from the previous class, etc. (see p. 227)
Instantiation vs. Inheritance instantiation = creating an object from
an existing class each new object has all the properties and
methods from the class properties = defaults or assigned values
inheritance = creating a new class from an existing class has all previous properties and methods can add more properties and methods to
it can instantiate objects from this new class
Inheritance Example Wolf class code
"base class" from which other code is derived
also called "superclass" or "parent class"
Dog class inherits from Wolf class "derived" class also called "subclass" or "child class"
Review What is inheritance?
What does a class get from a class it inherits from?
What does instantiation mean in terms of classes and objects?
Can a class inherit from a class that inherits from another class? If so, how many times?
Is-A (subclass) Relationship a subclass is more specific than
parent Employee is general (all people
working for the company) EmployeeWithTerritory is specific (a
certain type of employee) is-a relationship
EmployeeWithTerritory is-a Employee Chihuahua is-a Dog
Has-A (attribute) Relationship a class can have 2nd class as attribute
object within a class Car class has-a object of type
Transmissionint price; Tranny tran;Public class Car(int price, Tranny tran) { this.price = price;
this.tran = tran;}
Advantages of Inheritance key concept: inheritance makes code
reusable don't "reinvent the wheel" saves time: add what you need rather
than writing a whole class fewer errors: the other class has already
been fully debugged (hopefully) more understandable code: only need to
look at added code (rest is a "black box")
Extending Classes put keyword extends in class headerpublic class ConvertCurrency
extends ConvertToUSDollars can use methods from parent classpublic double net(String curr, double
amt){ return 1.019 * calculate(curr, amt);} // calculate is in parent class
Using Superclass Methods create a new class that extends an
existing class can write a new program that:
instantiates an object from the new class has this object call methods that are in its
superclass or just calls a class (not using as an
object) method that is in parent class' methods
Using Superclass Methods - 2 //create a class inheriting from
anotherpublic class NextClass extends FirstClass.....//code below is in a different classNextClass myObj = new NextClass();
myObj.getData(); // getData can be method in parent or child
Using Superclass Methods - 3 child class can use word super
instead of an object name to call methods in its superclass
super.toString(); //runs toString method in parent
Inheritance Is One-Way subclass can use superclass
methods and properties keyword extends makes all
superclass methods known to subclasses
superclass can't use subclass methods and properties nothing tells superclass which
subclasses are derived from it subclass methods are not "visible" to
superclass
Overriding Superclass Methods some superclass methods may not be
appropriate for subclass Employee class calculatePay method
provides overtime after 40 hours/week programmers don't get overtime
make subclass: ProgrammerEmployee add calculatePay method // no overtime new method overrides (replaces) other
Overriding Polymorphism poly means many
polytechnic = many technologies morph means shape or form
Dr. Jeckyll morphed into Mr. Hyde; image, videos; photo morphing web site
polymorphism = many shapes in OO programming, means somewhat
different ways of doing the same general thing
Polymorphism Generality create a general superclass
e.g., GeometricFigure add a general method
e.g., calculateArea as height x width create subclasses
Rectangle, Square & Parallelogram can use calculateArea method from superclass
Triangle has different code for this method
Class Diagram shows from what classes other
classes in Java inherit from subclasses have same methods as their
superclasses (if not overridden) diagrams of inheritance
Java has thousands of classes most of these inherit from other classes awt diagram
Web Links on Inheritance links found with Google, searching
on: "oo programming" "+what +is
inheritance" Sun Microsystems tutorial "Don't Fear the OOP" (cutesy)
tutorial slide 5 tutorial slide 6
Exercise identify subclasses of a superclass
Food, MusicalInstrument, MotorVehicle, Pet, or something else
identify a general method that will work for subclasses derived from superclass e.g., cookFood: boil for 10 minutes
identify subclasses & override methods e.g., Hamburger.cookFood: fry 4 minutes
Review What is the difference between is-a
and has-a? (give examples) Name some advantages of
inheritance Where is the keyword extends
used in relation to inheritance? super?
More Review What does overriding mean? What is polymorphism?
Which one was the "good guy" – Dr. Jekyll or Mr. Hyde?