Download presentation
Presentation is loading. Please wait.
1
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises
2
Unit 08 & 092 Introduction to Nested Classes So far, our classes and interfaces were defined at top-level. These top-level classes and interfaces are grouped into packages. Classes in the same package are accessible to each other. With access modifiers, classes can have inter-package access. Newer versions of Java enable defining classes and interfaces inside other classes. Benefits of nested classes will be discussed at the end after they become familiar.
3
Unit 08 & 093 Introduction to Nested Classes (cont’d) There are four categories of nested classes in Java: 1.Static member classes and interfaces. 2.Inner classes, defined inside a class (also called Non-Static Member Classes) 3.Local classes ( defined inside a block of Java code) 4.Anonymous classes ( defined inside a block of Java code) Only the last three categories will be covered in this course.
4
Unit 08 & 094 Nested Classes at a Glance interface AnInterface { void f(); } class AClass{ class InnerClass { //... } public void myMethod1(){ class LocalClass { //... } public AnInterface myMethod2(){ return new AnInterface(){ // Anonymous public void f(){} }; }
5
Unit 08 & 095 Inner Classes A Inner class is analogous to an instance field or instance method. Like other instance members, an inner class can have any access modifier. Every instance of a inner class is linked with an instance of the containing class. A nested class produces a separate byte code file. –If a nested class called Inside is declared in an outer class called Outside, two byte code files are produced: Outside.class Outside$Inside.class Inner classes cannot have static fields or static methods Interfaces cannot be defined as inner classes. Why?
6
Unit 08 & 096 Example 1: Inner Classes class Outer{ private int i = 10; private String s = "hello"; private class Inner{ //private instance variables of the enclosing class object can be accessed public void test(){ i += 10; System.out.println(i); System.out.println(s); } //creates a Inner class object which has access to the private state of //the object it is called for. public void test(){ Inner n = new Inner(); n.test(); } public static void main(String[] args){ Outer obj = new Outer(); obj.test(); }
7
Unit 08 & 097 Using this Keyword in Inner Classes The this reference is applicable inside inner classes. From within an instance of an inner class we can refer to two objects, the inner object, and the outer object associated with it. How do we distinguish the two objects? The next example demonstrates how this can be done. –To refer to the outer class object, use the notation OuterClass.this
8
Unit 08 & 098 Example 2: Keyword this in Inner Classes 1 class InnerClassesAndThis { 2 String x = "ICS 201"; 3 class B { 4 int x = 5; 5 void f(int x){ 6 System.out.println(x); 7 System.out.println(this.x); 8 System.out.println(InnerClassesAndThis.this.x); 9 } 10 } 11 public static void main(String [] s){ 12 InnerClassesAndThis outer = new InnerClassesAndThis (); 13 B b = outer.new B(); 14 b.f(7); 15 } 16 }
9
Unit 08 & 099 Local Classes When a class name is used only within a block it can be defined locally. A local class is defined within a block of Java code. Like a local variable a local class is visible only within its enclosing block. Local classes share many of the features of inner classes. An instance of a local class is associated with an instance of its enclosing class. Local classes are completely hidden in their containing block. Interfaces cannot be defined locally. Why?
10
Unit 08 & 0910 Example 3: Local Classes class LocalClassExample{ 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 l = new Local(); l.test(); } public static void main(String[] args){ LocalClassExample obj = new LocalClassExample(); obj.method(); }
11
Unit 08 & 0911 Example 4: Local Classes interface Thing{ public void test(); } class LocalClassExample2{ private String name = "KFUPM"; public Thing f(){ int j = 20; final int k = 30; class Local implements Thing{ public void test(){ //System.out.println(j); //Error as j is not final System.out.println(k); //OK k is final //the enclosing object instance variables can be accessed. System.out.println(name); } Local l = new Local(); return l; } public static void main(String[] args){ LocalClassExample2 obj1 = new LocalClassExample2(); Thing obj2 = obj1.f(); obj2.test(); }
12
Unit 08 & 0912 Anonymous Classes When a local class is used only once, it can be defined anonymously. An anonymous class is a local class without a name. An anonymous class is defined and instantiated in a single expression. An anonymous class has no constructors. Construction parameters can be given through the superclass constructor. When an anonymous class implements an interface, it cannot have any construction parameters. Why? Interfaces cannot be defined anonymously. Why?
13
Unit 08 & 0913 Example 5: Anonymous Classes interface Thing{ public void test(); } class LocalClassExample2{ private String name = "KFUPM"; public Thing f(){ int j = 20; final int k = 30; return new Thing() { // equivalent to Example 4 but using anonymous class public void test(){ //System.out.println(j); //Error as j is not final System.out.println(k); //OK k is final //the enclosing object instance variables can be accessed. System.out.println(name); } }; } public static void main(String[] args){ LocalClassExample2 obj1 = new LocalClassExample2(); Thing obj2 = obj1.f(); obj2.test(); }
14
Unit 08 & 0914 Example 6: Anonymous Classes interface Thing{ public void test(); } class AnonymousClassExample{ private String name = "KFUPM"; private Thing method1() { Thing obj = new Thing(){ public void test(){ //instance variables of enclosing object can be accessed. System.out.println(name); } }; return obj; } private Thing method2(){ Thing obj = new Thing(){ public void test(){ System.out.println(name+name+name); } }; return obj; } public static void main(String[] args){ AnonymousClassExample obj = new AnonymousClassExample(); Thing t; t = obj.method1(); t.test(); t = obj.method2(); t.test(); }
15
Unit 08 & 0915 Benefits of Nested Classes 1.Object-orientation: Separation of definition from functionality. 2.Code organization: Namespace control and access control. Inner classes can be hidden from other classes in the same package. 3.Multiple inheritance: Inner classes of a class can each inherit from a different class.
16
Unit 08 & 0916 Benefits of Nested Classes (Cont’d) 4. Code Simplification: 1.Inner classes access outer class variables automatically ( even if they are declared private). 2.In certain situations this makes the implementation of the classes easier because they can share information easily. Eg., in the following code, method f method can access both x and y directly: public class A { int y; public class B { int x; void f () {….} }
17
Unit 08 & 0917 Review Exercises 1. Use appropriate examples to explain how nested classes provide addition support for object orientation, code organization and multiple implementation inheritance. 2. Can an interface be defined inside an inner class? 3. Write a complete program to illustrate how to access object of an outer class from an object of the inner class. 4. Mention two similarities and two differences between inner classes and anonymous classes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.