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.

Slides:



Advertisements
Similar presentations
Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Advertisements

CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
ITEC200 – Week03 Inheritance and Class Hierarchies.
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.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
© 2004 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 : Inheritance Intermediate Java Programming Summer 2007.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
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 - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
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.
Coming up: Inheritance
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
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.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
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.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
© 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.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
Lecture 12 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,
Computing with C# and the .NET Framework
03/10/14 Chapter 9 Inheritance.
Class Inheritance (Cont.)
Interfaces.
Advanced Programming Behnam Hatami Fall 2017.
Inheritance Inheritance is a fundamental Object Oriented concept
Overriding Methods & Class Hierarchies
Java Programming, Second Edition
Presentation transcript:

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

Objectives Use inheritance to relate classes Use polymorphism to improve design Use abstract classes to achieve implementation independence Understand the use of modifiers in specifying access Develop an object-oriented design from use cases and scenarios

Subclasses Inheritance allows deriving a new class from an existing one The existing class is called the parent class, or superclass, or base class The derived class is called the child class or subclass The child class inherits characteristics: data and methods, of the parent Java only support single inheritance

Subclasses The inheritance (child-parent) relationships are often shown graphically in a class diagram like Inheritance exhibits an is-a relationship, meaning the child is a more specific version of the parent Vehicle Car

Class Hierarchies Inheritance relationships create hierarchy of classes Two children of the same parent are called siblings An inherited member is continually passed down the line Class hierarchies often have to be extended and modified to keep up with changing needs

Deriving Subclasses To create a subclass, we extend a class, inheriting all the attributes and behavior of that class public class Car extends Vehicle { // class contents } The keyword extends tells us that Car is a subclass of the superclass Vehicle A subclass usually have data and methods defined in addition to the ones inherited

Deriving Subclasses Words.java (extra) Book.java (extra) Dictionary.java (extra) BankAccount.java SavingAccount.java UseSavingAccount.java CheckingAccount.java UseCheckingAccount.java

Deriving Subclasses A child class inherits the public methods of its parent, but it may choose to override some of them to implement its own specific behavior Messages.java (extra) Thought.java (extra) Advice.java (extra) Overloading - multiple methods in the same class with the same name but different signatures Overriding - two methods of the same name, one in a parent class and one in a child class, that have the same signature

Reference and Inheritance An object reference can refer to an object of its class, or to an object of any class related to it by inheritance For example, if the Christmas is a subclass of the Holiday class, then a Holiday reference can be used to point to a Christmas object Holiday day; day = new Christmas();

The Keyword super Constructors are not inherited, even though they are publicly visible Java uses the keyword super to reference the superclass, it is often used to invoked the parent’s constructor Words2.java (extra) Book2.java (extra) Dictionary2.java (extra)

Polymorphism Polymorphism - “many forms” A polymorphic reference is one which can refer to different types of objects at different times A reference to the superclass can actually refer to an object in any subclass An interface can also be used to create polymorphic reference An object reference variable has –Compile-time type - the variable’s declared type –Run-time type - the type of the object to which the variable currently refers

Polymorphism Compile-time vs. run-time types Animal herman; // compile-time type Animal Animal zelda; // compile-time type Animal herman = new Lion(); // run-time type Lion zelda = new Giraffe(); // run-time type Giraffe Animal[] two = {herman, zelda}; feed(two); where public void feed(Animal[] theZoo) { for (int i = 0; i < theZoo.length; i++) theZoo[i].eat(); }

Polymorphism Animal.java Lion.java Giraffe.java UseAnimals.java Duck.java UseNewAnimals.java Withdraw.java

Abstract Classes An abstract class is a placeholder in a class hierarchy that represents a generic concept An abstract class cannot be instantiate We use the modifier abstract on the class header to declare a class as abstract An abstract class often contains abstract methods (like an interface does), though it doesn’t have to The child of an abstract class must override the abstract methods of the parent, or it too will be considered abstract

Abstract Classes The keyword final - Specify that a method cannot be overridden An Abstract method cannot be defined as final or static The use of abstract classes is a design decision; it helps us establish common elements in a class that is too general to instantiate Polymorphism often works with abstract classes Operations of an abstract class will work for objects of any subclass

A Shape Classes Shape is an abstract concept Shape has subclass like Line, Circle, etc. that have instances Shape.java Line.java Circle.java UseShape.java

Package A java package corresponds to a directory in the directory structure of the host machine To put a class in a named package, use a package statement as the first statement in the file For example: package personData; The compiled byte code file (.class) must be placed under the directory with the same name of the package An import statement tells Java to look in the directory for classes referenced in the program

Package Using import statement, we can ommit the package prefix of the classes In addition to the default directories that Java will automatically search, the classpath enviroment variable specifies user- defined directories to search For example: javac -classpath.;c:\booklr Test.java java -classpath.;c:\booklr Test

Modifiers and Access public - anywhere the class name is accessible protected - in the package that contains the class in which the member is declared, and in any subclass of that class (no modifier) - in the package that contains the class in which the member is declared private - only in the class in which the member is declared The visibility package: A.java, B.java, C.java, D.java

OO Design Understand the problem Solve the problem by identifying objects Each object provides certain services Objects use each other's services A use case defines one function that the system should provide For each case, several scenarios to describe the interaction among the user and other parts of the system to provide the function

OO Design Example Problem - Simple automatic teller machine Use cases –deposit –withdrawal –get balance Several scenarios for each use case –at least one for success –several for failure AtmScreen.java

A Success Scenario for Deposit The user asks the teller to accept an ATM card The teller asks the user to enter a PIN The user asks the teller to accept a PIN The teller asks the user to select a transaction type The user asks the teller to accept a deposit The teller asks the user to select an account type The user asks the teller to accept a saving account type The teller asks the bank to find the account of the chosen type for the user with the specified PIN The bank gives the teller a reference to the account

A Success Scenario for Deposit The teller asks the user to specify an amount The user asks the teller to accept an amount The teller asks the account to de[posit the specified amount The teller asks the user to select another transaction

A Failure Scenario The user asks the teller to accept an ATM card The teller asks the user to enter a PIN The user asks the teller to accept a PIN The teller asks the user to select a transaction type The user asks the teller to accept a get balance transaction type The teller asks the user to select an account type The user asks the teller to accept a checking account type The teller asks the bank to find the account of the chosen type for the user with the specified PIN The bank gives the teller a null account The teller asks the user to select another transaction,...