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

Slides:



Advertisements
Similar presentations
OOP: Inheritance By: Lamiaa Said.
Advertisements

1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Java Class and Inheritance.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Computer Science I Inheritance Professor Evan Korth New York University.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Object Oriented Programming: Inheritance Chapter 9.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Chapter 10 Inheritance and Polymorphism
Peyman Dodangeh Sharif University of Technology Fall 2014.
Topic 4 Inheritance.
CSCI-383 Object-Oriented Programming & Design Lecture 10.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance and Access Control CS 162 (Summer 2009)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Object Oriented Programming: Inheritance Chapter 9.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Coming up: Inheritance
AP Computer Science A – Healdsburg High School 1 Inheritance - What is inheritance? - “Is-a” vs. “Has-a” relationship - Programming example “CircleBug”
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
CS102 – OOP_2 Inheritance, Polymorphism, Interfaces & Abstract classes. David Davenport.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
CIS 234: Object-Oriented Programming with Java
Advanced Programming in Java
Inheritance ITI1121 Nour El Kadri.
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
An Introduction to Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Comp 249 Programming Methodology
Advanced Programming Behnam Hatami Fall 2017.
Object Oriented Programming: Inheritance
Two big words Inheritance Polymorphism
Java Programming, Second Edition
Inheritance.
Advanced Inheritance Concepts
Inheritance and Polymorphism
Presentation transcript:

CIS 234: Inheritance Dr. Ralph D. Westfall April, 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? 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 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

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;}

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 header public class ConvertCurrencyConvertCurrency extends ConvertToUSDollars can use methods from parent class public double net(String curr, double amt) { return * 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 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

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

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

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

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?