cis 234: inheritance dr. ralph d. westfall april, 2010
TRANSCRIPT
![Page 1: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/1.jpg)
CIS 234: Inheritance
Dr. Ralph D. WestfallApril, 2010
![Page 2: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/2.jpg)
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?
![Page 3: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/3.jpg)
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)
![Page 4: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/4.jpg)
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
![Page 5: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/5.jpg)
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"
![Page 6: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/6.jpg)
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?
![Page 7: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/7.jpg)
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
![Page 8: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/8.jpg)
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;}
![Page 9: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/9.jpg)
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")
![Page 10: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/10.jpg)
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
![Page 11: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/11.jpg)
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
![Page 12: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/12.jpg)
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
![Page 13: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/13.jpg)
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
![Page 14: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/14.jpg)
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
![Page 15: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/15.jpg)
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
![Page 16: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/16.jpg)
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
![Page 17: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/17.jpg)
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
![Page 18: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/18.jpg)
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
![Page 19: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/19.jpg)
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
![Page 20: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/20.jpg)
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
![Page 21: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/21.jpg)
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?
![Page 22: CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d0b5503460f949decde/html5/thumbnails/22.jpg)
More Review What does overriding mean? What is polymorphism?
Which one was the "good guy" – Dr. Jekyll or Mr. Hyde?