November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.

Slides:



Advertisements
Similar presentations
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Advertisements

Class Hierarchy (Inheritance)
CS 211 Inheritance AAA.
Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Inheritance Inheritance Reserved word protected Reserved word super
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
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,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
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.
Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted.
Computer Science I Inheritance Professor Evan Korth New York University.
Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
© 2004 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 : Inheritance Intermediate Java Programming Summer 2007.
LECTURE 07 Programming using C# Inheritance
Inheritance using Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 8 More Object Concepts
Intro to OOP with Java, C. Thomas Wu
1 Given the Radio class  We may define other derivative types: Cassette walkman IS-A radio Alarm clock radio IS-A radio Car radio IS-A radio.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Inheritance Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
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.
1 Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Lecture 12 Inheritance.
Chapter 8 Inheritance.
Inheritance and Polymorphism
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,
03/10/14 Chapter 9 Inheritance.
An Introduction to Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Lecture 14 - Abstract Classes
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
Class Inheritance (Cont.)
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Inheritance, Polymorphism, and Interfaces. Oh My
Overriding Methods & Class Hierarchies
Final and Abstract Classes
Programming in C# CHAPTER 5 & 6
Presentation transcript:

November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and hierarchy Visibility modifier protected Reserved word super  Reading (JV): Sec 7.1 – 7.3  Announcement: Project 6 posted, due Thursday Dec 6

November 27, 2001Lecture 232 Inheritance  Allows programmer to derive a class from an existing one  Existing class is called the parent class, or superclass  Derived class is called the child class or subclass  The child class inherits the methods and data defined for the parent class  Inherited trait can be accessed as though it were locally declared (defined)

November 27, 2001Lecture 233 Inheritance, cont’d Inheritance relationships often shown graphically in a class diagram, with the arrow pointing to the parent class Create an is-a relationship, meaning the child is a more specific version of the parent Single inheritance: one parent only Vehicle Car

November 27, 2001Lecture 234 Deriving a subclass Reserved word extends establishes an inheritance relationship: class Vehicle { // class contents } class Car extends Vehicle { // class contents }

November 27, 2001Lecture 235 protected visibility  Visibility modifiers control which members get inherited  private Not inherited, can be accessed by local class only  public Inherited, can be accessed by all classes  protected Inherited, can be accessed by subclasses  Access: access as though declared locally  All variables from a superclass exist in the subclass, but some ( private ) cannot be accessed directly

November 27, 2001Lecture 236 public class Vehicle { protected int plateNum; //plate # private int numWheels; //# of wheels public void writeOut() { System.out.println(“Plate number:” + plateNum); } } //class Vehicle public class Plane extends Vehicle { protected double wingSpan; private boolean hasPropeller; public void writeProperties() { System.out.println(plateNum); //? System.out.println(numWheels); //? if (hasPropeller) //? System.out.println(“Prop plane”); } } //class Plane

November 27, 2001Lecture 237 Reserved word super  Invoke constructor of superclass super(parameter-list); parameter-list must match that in superclass’ constructor  Access methods and variables from superclass

November 27, 2001Lecture 238 // New definition of class Vehicle public class Vehicle { protected int plateNum; //plate # private int numWheels; //# of wheels public Vehicle(int plate, int wheels) { plateNum = plate; numWheels = wheels; } public void writeOut() { System.out.println(“Plate number:” + plateNum); } } //class Vehicle

November 27, 2001Lecture 239 // New definition of class Plane public class Plane extends Vehicle { protected double wingSpan; private boolean hasPropeller; public Plane(int plate, int wheels, double span, boolean prop) { super(plate,wheels); wingSpan = span; hasPropeller = prop; } public void writeProperties() { System.out.println(plateNum); //System.out.println(numWheels); if (hasPropeller) System.out.println(“Prop plane”); } } //class Plane

November 27, 2001Lecture 2310 Overriding methods  Subclass can override definition of inherited method in favor of its own  New method in subclass must have same signature as superclass (but has different method body)  Which method gets used?? The object that is used to invoke a method determines which version is used  Method declared to be final cannot be overridden  Do not confuse overriding with overloading!

November 27, 2001Lecture 2311 // Better definition of class Plane public class Plane extends Vehicle { protected double wingSpan; private boolean hasPropeller; public Plane(int plate, int wheels, double span, boolean prop) { super(plate,wheels); wingSpan = span; hasPropeller = prop; } // Override method writeOut // Also access method from superclass public void writeOut() { super.writeOut(); System.out.println(“Wing Span: “ + wingSpan); if (hasPropeller) System.out.println(“Prop plane”); } } //class Plane

November 27, 2001Lecture 2312 Important ideas in inheritance  Use different hierarchies for different problems  Single inheritance  Keep common features as high in the hierarchy as reasonably possible  Inherited features are continually passed down the line  Use the superclass’ features as much as possible  “Inherited”  “can be accessed as though declared locally” ( private variables in superclass exists in subclasses; they just cannot be accessed directly)