Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Generics and the ArrayList Class
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Threads Part I.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Threads Part II.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 The final Modifier A method marked final indicates that it cannot be overridden with a new definition.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
CS102--Object Oriented Programming Lecture 15: Interfaces Copyright © 2008 Xiaoyan Li.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Writing Classes (Chapter 4)
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Java Interfaces. Interfaces An interface is something like an extreme case of an abstract class – However, an interface is not a class – It is a type.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 3 Boolean Expressions Section 3.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
Interfaces An interface is like an extreme case of an abstract class – However, an interface is not a class – It is a type that can be satisfied by any.
Interfaces and Inner Classes
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
ISBN Chapter 12 Support for Object-Oriented Programming.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Packages, Interfaces & Exception Handling
Comp 249 Programming Methodology
Comp 249 Programming Methodology
Comp 249 Programming Methodology
Interface.
CMSC 202 Interfaces.
Advanced Java Programming
CSE 1030: Implementing GUI Mark Shtern.
Interfaces and Inner Classes
Java Inheritance.
CMSC 202 Interfaces.
Chapter 14 Abstract Classes and Interfaces
Chapter 4 Constructors Section 4.4
Information Hiding and Encapsulation Section 4.2
CMSC 202 Interfaces.
Presentation transcript:

Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes

© 2006 Pearson Addison-Wesley. All rights reserved13-2 Reading Assignment: –Sections 1 (except the Comparable interface), 2, 3 Self-Test Exercises: – 1..4, 8..11,

© 2006 Pearson Addison-Wesley. All rights reserved13-3 Interfaces An interface is something like an extreme case of an abstract class –However, an interface is not a class –It is a type that can be satisfied by any class that implements the interface The syntax for defining an interface is similar to that of defining a class –Except the word interface is used in place of class An interface specifies a set of methods that any class that implements the interface must have –It contains method headings and constant definitions only –It contains no instance variables nor any complete method definitions

© 2006 Pearson Addison-Wesley. All rights reserved13-4 Interfaces An interface serves a function similar to a base class, though it is not a base class –Some languages allow one class to be derived from two or more different base classes –This multiple inheritance is not allowed in Java –Instead, Java's way of approximating multiple inheritance is through interfaces

© 2006 Pearson Addison-Wesley. All rights reserved13-5 Interfaces An interface and all of its method headings should be declared public –They cannot be given private, protected, or package access When a class implements an interface, it must make all the methods in the interface public Because an interface is a type, a method may be written with a parameter of an interface type –That parameter will accept as an argument any class that implements the interface

© 2006 Pearson Addison-Wesley. All rights reserved13-6 The Ordered Interface

© 2006 Pearson Addison-Wesley. All rights reserved13-7 Interfaces To implement an interface, a concrete class must do two things: 1.It must include the phrase implements Interface_Name at the start of the class definition –If more than one interface is implemented, each is listed, separated by commas 2.The class must implement all the method headings listed in the definition(s) of the interface(s) Note the use of Object as the parameter type in the following examples

© 2006 Pearson Addison-Wesley. All rights reserved13-8 Implementation of an Interface

© 2006 Pearson Addison-Wesley. All rights reserved13-9 Implementation of an Interface

© 2006 Pearson Addison-Wesley. All rights reserved13-10 Abstract Classes Implementing Interfaces Abstract classes may implement one or more interfaces –Any method headings given in the interface that are not given definitions are made into abstract methods A concrete class must give definitions for all the method headings given in the abstract class and the interface

© 2006 Pearson Addison-Wesley. All rights reserved13-11 An Abstract Class Implementing an Interface

© 2006 Pearson Addison-Wesley. All rights reserved13-12 Derived Interfaces Like classes, an interface may be derived from a base interface –This is called extending the interface –The derived interface must include the phrase extends BaseInterfaceName A concrete class that implements a derived interface must have definitions for any methods in the derived interface as well as any methods in the base interface

© 2006 Pearson Addison-Wesley. All rights reserved13-13 Extending an Interface

© 2006 Pearson Addison-Wesley. All rights reserved13-14 Pitfall: Interface Semantics Are Not Enforced When a class implements an interface, the compiler and run-time system check the syntax of the interface and its implementation –However, neither checks that the body of an interface is consistent with its intended meaning Required semantics for an interface are normally added to the documentation for an interface –It then becomes the responsibility of each programmer implementing the interface to follow the semantics If the method body does not satisfy the specified semantics, then software written for classes that implement the interface may not work correctly

© 2006 Pearson Addison-Wesley. All rights reserved13-15 Defined Constants in Interfaces An interface can contain defined constants in addition to or instead of method headings –Any variables defined in an interface must be public, static, and final –Because this is understood, Java allows these modifiers to be omitted Any class that implements the interface has access to these defined constants

© 2006 Pearson Addison-Wesley. All rights reserved13-16 Pitfall: Inconsistent Interfaces In Java, a class can have only one base class –This prevents any inconsistencies arising from different definitions having the same method heading In addition, a class may implement any number of interfaces –Since interfaces do not have method bodies, the above problem cannot arise –However, there are other types of inconsistencies that can arise

© 2006 Pearson Addison-Wesley. All rights reserved13-17 Pitfall: Inconsistent Interfaces When a class implements two interfaces: –One type of inconsistency will occur if the interfaces have constants with the same name, but with different values In this case, the class will be compiled correctly but when any of the constants is accessed via a reference of the class type, a compilation error will occur. If the reference is up-casted to one of the interfaces, the constant from that interface will be used without any error –Another type of inconsistency will occur if the interfaces contain methods with the same name but different return types This is an error, the class will not compile