Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010.

Similar presentations


Presentation on theme: "CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010."— Presentation transcript:

1 CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010

2 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? wolves in East Asia?

3 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)

4 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

5 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"

6 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?

7 Is-A (subclass) Relationship a subclass is more specific than parentsubclass 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

8 Has-A (attribute) Relationship a class can have 2 nd class as attribute object within a class Car class has-a object of type Transmission int price; Tranny tran; Public class Car(int price, Tranny tran) { this.price = price; this.tran = tran;}

9 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")

10 Extending Classes put keyword extends in class header public class ConvertCurrencyConvertCurrency extends ConvertToUSDollars can use methods from parent class public double net(String curr, double amt) { return 1.019 * calculate(curr, amt); } // calculate is in parent class

11 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

12 Using Superclass Methods - 2 //create a class inheriting from another public class NextClass extends FirstClass..... //code below is in a different class NextClass myObj = new NextClass(); myObj.getData(); // getData can be method in parent or child

13 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

14 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

15 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

16 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 Dr. Jeckyll morphed into Mr. Hydeimage videosphoto morphing web site polymorphism = many shapes in OO programming, means somewhat different ways of doing the same general thing

17 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

18 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 diagrams Java has thousands of classes most of these inherit from other classes awt diagram

19 Web Links on Inheritance links found with Google, searching on:Google "oo programming" "+what +is inheritance" Sun Microsystems tutorial "Don't Fear the OOP" (cutesy) tutorial slide 5 tutorial slide 5 tutorial slide 6

20 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

21 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?

22 More Review What does overriding mean? What is polymorphism? Which one was the "good guy" – Dr. Jekyll or Mr. Hyde?


Download ppt "CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010."

Similar presentations


Ads by Google