Chapter 13 Interfaces and Inner Classes Slides prepared

  • Slides: 14
Download presentation
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University

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

Static Inner Classes • A normal inner class has a connection between its objects

Static Inner Classes • A normal inner class has a connection between its objects and the outer class object that created the inner class object – This allows an inner class definition to reference an instance variable, or invoke a method of the outer class • There are certain situations, however, when an inner class must be static – If an object of the inner class is created within a static method of the outer class – If the inner class must have static members © 2006 Pearson Addison-Wesley. All rights reserved 2

Static Inner Classes • Since a static inner class has no connection to an

Static Inner Classes • Since a static inner class has no connection to an object of the outer class, within an inner class method – Instance variables of the outer class cannot be referenced – Nonstatic methods of the outer class cannot be invoked • To invoke a static method or to name a static variable of a static inner class within the outer class, preface each with the name of the inner class and a dot © 2006 Pearson Addison-Wesley. All rights reserved 3

Public Inner Classes • If an inner class is marked public, then it can

Public Inner Classes • If an inner class is marked public, then it can be used outside of the outer class • In the case of a nonstatic inner class, it must be created using an object of the outer class Bank. Account account = new Bank. Account(); Bank. Account. Money amount = account. new Money("41. 99"); – Note that the prefix account. must come before new – The new object amount can now invoke methods from the inner class, but only from the inner class © 2006 Pearson Addison-Wesley. All rights reserved 4

Public Inner Classes • In the case of a static inner class, the procedure

Public Inner Classes • In the case of a static inner class, the procedure is similar to, but simpler than, that for nonstatic inner classes Outer. Class. Inner. Class inner. Object = new Outer. Class. Inner. Class(); – Note that all of the following are acceptable inner. Object. nonstatic. Method(); inner. Object. static. Method(); Outer. Class. Inner. Class. static. Method(); © 2006 Pearson Addison-Wesley. All rights reserved 5

Tip: Referring to a Method of the Outer Class • If a method is

Tip: Referring to a Method of the Outer Class • If a method is invoked in an inner class – If the inner class has no such method, then it is assumed to be an invocation of the method of that name in the outer class – If both the inner and outer class have a method with the same name, then it is assumed to be an invocation of the method in the inner class – If both the inner and outer class have a method with the same name, and the intent is to invoke the method in the outer class, then the following invocation must be used: Outer. Class. Name. this. method. Name() – using this inside an inner class refers to the object of the inner class © 2006 Pearson Addison-Wesley. All rights reserved 6

Nesting Inner Classes • It is legal to nest inner classes within inner classes

Nesting Inner Classes • It is legal to nest inner classes within inner classes – The rules are the same as before, but the names get longer – Given class A, which has public inner class B, which has public inner class C, then the following is valid: A a. Object = new A(); A. B b. Object = a. Object. new B(); A. B. C c. Object = b. Object. new C(); © 2006 Pearson Addison-Wesley. All rights reserved 7

Inner Classes and Inheritance • Given an Outer. Class that has an Inner. Class

Inner Classes and Inheritance • Given an Outer. Class that has an Inner. Class – Any Derived. Class of Outer. Class will automatically have Inner. Class as an inner class – In this case, the Derived. Class cannot override the Inner. Class • An outer class can be a derived class • An inner class can be a derived class also © 2006 Pearson Addison-Wesley. All rights reserved 8

Local Classes • A local class is defined within a block of Java code.

Local Classes • A local class is defined within a block of Java code. • Local classes are completely hidden in their containing block. • When a class name is used only within a block it can be defined locally. • A local class can access instance variables of the outer class and only the final local K O O B e variables of the enclosing block. h nt © 2006 Pearson Addison-Wesley. All rights reserved ti o N 9

Local Classes: Example class Local. Class. Example{ private String name = "KFUPM"; public void

Local Classes: Example class Local. Class. Example{ private String name = "KFUPM"; public void method ( ) { int j = 20; final int k = 30; class Local { public void test ( ) { //System. out. println(j); //Error as j is not final System. out. println(k); //OK k is final //Like an inner class, instance variables of //the enclosing object can be accessed. System. out. println ( name ) ; } } Local loc = new Local ( ) ; loc. test ( ) ; } public static void main ( String [ ] args ) { Local. Class. Example obj = new Local. Class. Example ( ); obj. method ( ) ; } } © 2006 Pearson Addison-Wesley. All rights reserved K O O t in o N B e h t 10

Anonymous Classes • It is a local class without a name • If only

Anonymous Classes • It is a local class without a name • If only one object has to be created from a class, and there is no need to name the class, then an anonymous class definition can be used – The class definition is embedded inside the expression with the new operator • Anonymous class has no constructors • It is either derived from a class, or implements an interface. Like: – An. Interface i = new An. Interface ( ) { // methods defs. … } – ASuperclass c = new ASuperclass(…) { // methods defs. … } © 2006 Pearson Addison-Wesley. All rights reserved 11

Anonymous Classes © 2006 Pearson Addison-Wesley. All rights reserved 12

Anonymous Classes © 2006 Pearson Addison-Wesley. All rights reserved 12

Anonymous Classes © 2006 Pearson Addison-Wesley. All rights reserved 13

Anonymous Classes © 2006 Pearson Addison-Wesley. All rights reserved 13

Anonymous Classes © 2006 Pearson Addison-Wesley. All rights reserved 14

Anonymous Classes © 2006 Pearson Addison-Wesley. All rights reserved 14