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

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
Generics and the ArrayList Class
Author: Takdir, S.ST. © Sekolah Tinggi Ilmu Statistik.
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
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 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Threads Part II.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
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.
Inner Classes. Lecture Objectives Learn about inner classes. Know the differences between static and non- static inner classes. Designing and using inner.
CS102--Object Oriented Programming Lecture 16: – Inner classes + review Copyright © 2008 Xiaoyan Li.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 8 Polymorphism Part I.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Interfaces and Inner Classes. What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
CS102--Object Oriented Programming Lecture 15: Interfaces Copyright © 2008 Xiaoyan Li.
1 Introduction to Searching and Sorting Comparable Interface -Reading p Comparator Interface.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
CMSC 202 Inner Classes. Aug 7, Simple Uses of Inner Classes Inner classes are classes defined within other classes  The class that includes the.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Classes CS 21a: Introduction to Computing I First Semester,
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.
Slides prepared by Rose Williams, Binghamton University Chapter 15 Linked Data Structures.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C 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.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Object-Oriented Programming Chapter Chapter
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Interfaces and Inner Classes
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
Chapter 5: Enhancing Classes
Chapter 8 Classes and Objects
Comp 249 Programming Methodology
Comp 249 Programming Methodology
Interfaces and Inner Classes
Inner Classes 29-Nov-18.
Interfaces and Inner Classes
Inner Classes.
CMSC 202 Inner Classes.
Comp 249 Programming Methodology
Classes CS 21a: Introduction to Computing I
Chapter 8 Class Inheritance and Interfaces
Chapter 4 Constructors Section 4.4
Information Hiding and Encapsulation Section 4.2
Presentation transcript:

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

© 2006 Pearson Addison-Wesley. All rights reserved13-2 The Serializable Interface An extreme but commonly used example of an interface is the Serializable interface –It has no method headings and no defined constants: It is completely empty –It is used merely as a type tag that indicates to the system that it may implement file I/O in a particular way

© 2006 Pearson Addison-Wesley. All rights reserved13-3 The Cloneable Interface The Cloneable interface is another unusual example of a Java interface –It does not contain method headings or defined constants –It is used to indicate how the method clone (inherited from the Object class) should be used and redefined

© 2006 Pearson Addison-Wesley. All rights reserved13-4 The Cloneable Interface The method Object.clone() does a bit- by-bit copy of the object's data in storage If the data is all primitive type data or data of immutable class types (such as String ), then this is adequate –This is the simple case The following is an example of a simple class that has no instance variables of a mutable class type, and no specified base class –So the base class is Object

© 2006 Pearson Addison-Wesley. All rights reserved13-5 Implementation of the Method clone: Simple Case

© 2006 Pearson Addison-Wesley. All rights reserved13-6 The Cloneable Interface If the data in the object to be cloned includes instance variables whose type is a mutable class, then the simple implementation of clone would cause a privacy leak When implementing the Cloneable interface for a class like this: –First invoke the clone method of the base class Object (or whatever the base class is) –Then reset the values of any new instance variables whose types are mutable class types – This is done by making copies of the instance variables by invoking their clone methods

© 2006 Pearson Addison-Wesley. All rights reserved13-7 The Cloneable Interface Note that this will work properly only if the Cloneable interface is implemented properly for the classes to which the instance variables belong – And for the classes to which any of the instance variables of the above classes belong, and so on and so forth The following shows an example

© 2006 Pearson Addison-Wesley. All rights reserved13-8 Implementation of the Method clone: Harder Case

© 2006 Pearson Addison-Wesley. All rights reserved13-9 Simple Uses of Inner Classes Inner (or nested) classes are classes defined within other classes –The class that includes the inner class is called the outer class There are four categories of inner classes in Java: 1.Inner classes (non-static) 2.Static inner classes 3.Local classes (defined inside a block of Java code) 4.Anonymous classes (defined inside a block of Java code)

© 2006 Pearson Addison-Wesley. All rights reserved13-10 Simple Uses of Inner Classes An inner class definition is a member of the outer class in the same way that the instance variables and methods of the outer class are members –An inner class is local to the outer class definition –The name of an inner class may be reused for something else outside the outer class definition –If the inner class is private, then the inner class cannot be accessed by name outside the definition of the outer class

© 2006 Pearson Addison-Wesley. All rights reserved13-11 Simple Uses of Inner Classes There are two main advantages to inner classes –They can make the outer class more self- contained since they are defined inside a class –Both of their methods have access to each other's private methods and instance variables Using an inner class as a helping class is one of the most useful applications of inner classes –If used as a helping class, an inner class should be marked private

© 2006 Pearson Addison-Wesley. All rights reserved13-12 Tip: Inner and Outer Classes Have Access to Each Other's Private Members Within the definition of a method of an inner class: –It is legal to reference a private instance variable of the outer class –It is legal to invoke a private method of the outer class Within the definition of a method of the outer class –It is legal to reference a private instance variable of the inner class on an object of the inner class –It is legal to invoke a (nonstatic) method of the inner class as long as an object of the inner class is used as a calling object Within the definition of the inner or outer classes, the modifiers public and private are equivalent

© 2006 Pearson Addison-Wesley. All rights reserved13-13 Class with an Inner Class

© 2006 Pearson Addison-Wesley. All rights reserved13-14 Class with an Inner Class

© 2006 Pearson Addison-Wesley. All rights reserved13-15 Class with an Inner Class

© 2006 Pearson Addison-Wesley. All rights reserved13-16 The.class File for an Inner Class Compiling any class in Java produces a.class file named ClassName.class Compiling a class with one (or more) inner classes causes both (or more) classes to be compiled, and produces two (or more).class files –Such as ClassName.class and ClassName$InnerClassName.class