Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques.

Slides:



Advertisements
Similar presentations
Object-Oriented Application Development Using VB.NET 1 Chapter 5 Object-Oriented Analysis and Design.
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.
Abstract Classes and Interfaces The objectives of this chapter are: To explore the concept of abstract classes To understand interfaces To understand the.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Chapter 10: Introduction to Inheritance
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Basic OOP Concepts and Terms
Object-Oriented Application Development Using VB.NET 1 Creating a String Array Code to create a String array: ' declare a String array with 4 elements.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
UML Class Diagram: class Rectangle
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Object-Oriented Application Development Using VB.NET 1 Chapter 9 Implementing Association Relationships.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Chapter 15 - Creating More Complex Database Applications 1 Chapter 15 Creating More Complex Database Applications.
Java™ How to Program, 9/e Presented by: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 9 - Implementing Association Relationships1 Chapter 9 Implementing Association Relationships.
Chapter 12: Adding Functionality to Your Classes.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Basic OOP Concepts and Terms. In this class, we will cover: Objects and examples of different object types Classes and how they relate to objects Object.
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.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
Object-Oriented Application Development Using VB.NET 1 Chapter 5 Object-Oriented Analysis and Design.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Object-Oriented Application Development Using VB.NET 1 Chapter 9 Implementing Association Relationships.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Programming in Java: lecture 7
The Movement To Objects
One class is an extension of another.
UML Class Diagram: class Rectangle
One class is an extension of another.
Interface.
Java Programming Language
Interfaces.
Advanced Java Programming
Java Inheritance.
Basic OOP Concepts and Terms
Presentation transcript:

Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques

Chapter 8 - Additional Inheritance Concepts and Techniques2 Chapter 8 Topics Require a subclass to override a superclass method by including an abstract method in the superclass Creating a Java interface and using an interface to require a class to implement methods Creating and using custom exceptions that provide detailed information about an error by extending the Exception class How all Java classes implicitly extend the Object class

Chapter 8 - Additional Inheritance Concepts and Techniques3 Introducing the Initial Lease Class Bradshaw Marina Case Study –Includes three generalization/specialization hierarchies that require inheritance Boat (Sail, Power), Slip (Covered), Lease (Annual, Daily) –Lease hierarchy Introduces additional inheritance concepts –Introduces problem domain classes that contain attributes that hold references to other objects

Chapter 8 - Additional Inheritance Concepts and Techniques4

5 Adding an Abstract Method to Lease Abstract Method (Figure 8-5) –Method without any statements –Requires that a subclass include or implement the method –If a class has an abstract method: The class must also be abstract –Keyword abstract

Chapter 8 - Additional Inheritance Concepts and Techniques6 Implementing the AnnualLease Subclass AnnualLease Class (Figure 8-6) –Extends Lease class –Adds attributes –Constructors and methods more complex –Imports java.util package for Date and Calendar classes Both subclass and superclass must do this

Chapter 8 - Additional Inheritance Concepts and Techniques7 Implementing the DailyLease Subclass DailyLease Class (Figure 8-7) –Extends Lease class –Adds attribute –Constructors and methods more complex –Imports java.util package for Date and Calendar classes Both subclass and superclass must do this

Chapter 8 - Additional Inheritance Concepts and Techniques8 Testing the AnnualLease and DailyLease Classes TesterTwo Class (Figure 8-8) –Tests AnnualLease and DailyLease classes Instantiates objects of both classes Instantiates date objects Retrieves information about each lease object

Chapter 8 - Additional Inheritance Concepts and Techniques9 Understanding and Using Java Interfaces Two methods to require that class override a method: –Add an abstract method to the superclass that the class derives from –Define an interface that the class implements Interface –Java component that defines abstract methods and constants that must be included by implementing classes

Chapter 8 - Additional Inheritance Concepts and Techniques10 Understanding and Using Java Interfaces Concept –Knowing how to use something means knowing how to interface with it –The methods an instance can respond to can be defined as the interface to the instance –To assure instance has a defined set of methods: Define an interface and have class implement it

Chapter 8 - Additional Inheritance Concepts and Techniques11 Understanding and Using Java Interfaces Component-based development –System components interact using well- defined interface built from a variety of technologies If interface is known  can be used in system Do not need to know technology used or internal structure of component

Chapter 8 - Additional Inheritance Concepts and Techniques12 Understanding and Using Java Interfaces Multiple inheritance –Ability to “inherit” from more than one class –Java allows only single inheritance Using extends keyword –Interfaces allow Java an alternative mechanism to emulate the concept of multiple inheritance

Chapter 8 - Additional Inheritance Concepts and Techniques13 Understanding and Using Java Interfaces Creating a Java Interface –Created much the same way as a class Header –Use interface keyword: »public interface LeaseInterface Abstract methods –Implement using implements keyword: public class AnnualLease extends Lease implements LeaseInterface

Chapter 8 - Additional Inheritance Concepts and Techniques14

Chapter 8 - Additional Inheritance Concepts and Techniques15 Understanding and Using Java Interfaces Implementing More Than One Interface –Java classes can implement more than one interface Use comma separated list: –Interfaces can include static final variables (constants) – Figure 8-11 All classes that implement the interface have the constants available –DailyLease in Figure 8-12

Chapter 8 - Additional Inheritance Concepts and Techniques16 Understanding and Using Java Interfaces Testing the Complete Interface Example –TesterThree class (Figure 8-13) Tests: –AnnualLease with one interface –DailyLease with two interfaces

Chapter 8 - Additional Inheritance Concepts and Techniques17 Using Custom Exceptions Custom exception –Exception written specifically for a particular application –Extends a built-in class Any class that is not declared final can be extended

Chapter 8 - Additional Inheritance Concepts and Techniques18 Using Custom Exceptions Defining the LeasePaymentException (Fig. 8-15) –Extend custom exception from Exception class –Add appropriate components: Attributes Constructors Methods –Sequence diagram Shows interaction between the LeasePaymentException class and the AnnualLease class

Chapter 8 - Additional Inheritance Concepts and Techniques19

Chapter 8 - Additional Inheritance Concepts and Techniques20 Using Custom Exceptions Throwing a Custom Exception –AnnualLease class (Figure 8-17) Uses LeasePaymentException class –Use throws keyword in method declaration –Use throw keyword in method body

Chapter 8 - Additional Inheritance Concepts and Techniques21 Using Custom Exceptions Testing the LeasePaymentException –TesterFour class Tests: –Revised AnnualLease class –LeasePaymentException class –Sequence diagram Shows interactions for TesterFour class

Chapter 8 - Additional Inheritance Concepts and Techniques22

Chapter 8 - Additional Inheritance Concepts and Techniques23

Chapter 8 - Additional Inheritance Concepts and Techniques24

Chapter 8 - Additional Inheritance Concepts and Techniques25 Using Custom Exceptions Handling Payments as a Batch –Batch processing Takes a collection of transactions and processes them one after the other –If successful: »Success message is displayed –If unsuccessful: »Exception message is displayed (but processing continues) Useful in testing validation and exceptions

Chapter 8 - Additional Inheritance Concepts and Techniques26

Chapter 8 - Additional Inheritance Concepts and Techniques27

Chapter 8 - Additional Inheritance Concepts and Techniques28

Chapter 8 - Additional Inheritance Concepts and Techniques29 Understanding the Object Class and Inheritance All Java classes extend from Object class –Object class Defines basic functionality that any other class of objects need in Java –All built-in class hierarchies have Object as common ancestor –All custom problem domain classes extend Object as well (implicitly)

Chapter 8 - Additional Inheritance Concepts and Techniques30