Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.

Similar presentations


Presentation on theme: "Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local."— Presentation transcript:

1 Advanced Java class Nested Classes & Interfaces

2 Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local named anonymous

3 Some Definitions Nested = a class or interface definition is somewhere inside another one Top-level class or interface = an instance of a type does not need to be instantiated with a reference to any enclosing instance Inner class = an instance of a class does need to be instantiated with an enclosing instance Inner Member class = defined inside another class, but not inside any methods. Inner Local class = defined inside a method Named Inner Local class = has a class name Anonymous Inner Local class = does not have a class name

4 Top-level Classes & Interfaces Declared inside a class (or interface) definition Must be declared as static The class (or interface) definition is not a member of an instantiation of the outer class (or interface). Shows a close functional relationship between the types. Class referencing syntax: OuterClass.InnerClass You can nest classes in interfaces, and vice versa.

5 Here Property is a Top-Level Nested Class public class PropertyValueFinder { final String GET = "get"; public void printProperties(Object o) {... } private Property propertyGottenBy(Method m) {...} private boolean isGetter(Method m) {...} static public class Property {...} } in another class, far, far away... PropertyValueFinder.Property p = new PropertyValueFinder.Property("hi", String.class); No enclosing instance of PropertyValueFinder is needed to make an instance of Property

6 Scope of Top-level Nested Classes Enclosed classes share private access with other enclosed classes in the same enclosing class, and with the enclosing class. –(Note: the book is wrong on this point, as shown by the compilation and running of the following example.)

7 public class TopLevelNested { private int myIntTLN = 3; void test() { new Other1().printOther2(); new Other2().printOther1(); } private void test2() {System.out.println("test 2");} static class Other1 { private int myIntO1 = 1; static void printOther2(){ System.out.println(new Other2().myIntO2); new TopLevelNested().test2(); } private static class Other2 { private int myIntO2 = 2; private static void printOther1(){ System.out.println(new Other1().myIntO1); System.out.println(new TopLevelNested().myIntTLN); }

8 Inner Classes Member –Defined in class definition, not static Local (private, not static) –Defined in method definition –May be named or anonymous

9 Using Inner Classes Reasons to use: –Nice for classes that implement 1-method interfaces –Adapter classes (see ex. page. 129) Presenting some aspect(s) of the enclosing class in terms of an interface that it does not implement Can not be static. The class definition is a member of the outer class. (And therefore accessible via reflection.) Inner object shares scope of outer object. These are classes, not interfaces.

10 Here Property is a Member Inner Class public class PropertyValueFinder { final String GET = "get"; public void printProperties(Object o) {... } private Property propertyGottenBy(Method m) {...} private boolean isGetter(Method m) {...} public class Property {...} } in another class, far, far away... PropertyValueFinder pvf = new PropertyValueFinder(); PropertyValueFinder.Property p = pvf.new Property("hi", String.class); An enclosing instance of PropertyValueFinder is needed to make an instance of Property

11 Scope of Inner Classes Scope: –in the inner class, “this” refers to both the instance of the inner class and its enclosing class instance –this means that the inner object can refer directly to fields and methods of the outer object –See example next page

12 public class Outer { private String myString = "outer"; private String outerString ="outer outer !"; void test() { System.out.print("test "); System.out.println(myString); new Inner().testInner(); } private void test2() { System.out.print("test2 "); System.out.println(myString); } class Inner { private String myString = "inner"; private void testInner(){ System.out.print("testInner "); System.out.println(myString); System.out.println(outerString); test2(); }

13 Inner classes extend other classes Inheritance vs. Enclosing Scope –If not fully specified, order is: inner, super, enclosing (book says won’t compile, but I don’t find this true) –If all of these exist, you should specify to: inner: this.X super: super.X enclosing: EnclosingClassName.this.X

14 Enclosing Class has outer most scope public class ScopeConflict { String s = "outer"; class Inner extends SuperClass { //String s = "inner"; void foo(){ System.out.println(s); } class SuperClass { //String s = "super"; } in another class: ScopeConflict sc = new ScopeConflict(); ScopeConflict.Inner inner = sc.new Inner(); inner.foo(); output: outer

15 Super class has scope between Enclosing and Inner class public class ScopeConflict { String s = "outer"; class Inner extends SuperClass { //String s = "inner"; void foo(){ System.out.println(s); } class SuperClass { String s = "super"; } in another class: ScopeConflict sc = new ScopeConflict(); ScopeConflict.Inner inner = sc.new Inner(); inner.foo(); output: super

16 Inner class has innermost scope public class ScopeConflict { String s = "outer"; class Inner extends SuperClass { String s = "inner"; void foo(){ System.out.println(s); } class SuperClass { String s = "super"; } in another class: ScopeConflict sc = new ScopeConflict(); ScopeConflict.Inner inner = sc.new Inner(); inner.foo(); output: inner

17 Specifying which scope you want: public class ScopeConflict { String s = "outer"; class Inner extends SuperClass { String s = "inner"; void foo(){ System.out.println(this.s); System.out.println(super.s); System.out.println(ScopeConflict.this.s); } class SuperClass { String s = "super"; } output: inner (from this.s) super (from super.s) outer (from ScopeConflict.this.s)

18 Member Inner Class as Superclasses public class Computer { int model; Computer(int i) { model = i; } public class HardDrive { int size; public HardDrive(int i) { size = i; } public HardDrive() { size = 40; } class SCSI extends Computer.HardDrive{ SCSI (Computer c) { c.super(80); }

19 Local Inner Classes Defined inside a method in an enclosing class Has access not only to the scope of the enclosing object(s), but also to the scope of the enclosing method and any code blocks that are final. Two types –Local Inner Named Classes –Local Inner Anonymous Classes

20 Local Inner Classes In a instance method –Can use all members of the enclosing instance In a class method –Can only use class members (because it’s in a static context).

21 Local Inner Classes Why must the local enclosing fields be final to be in scope? Because the objects (not the classes) can be used outside of the scope they were created in, so they must have a copy of the local fields from their original scope. These local copies would be wrong if the fields changed, so the fields must be final to protect the integrity of the local inner class object.

22 Named Local Inner classes public class Outer { public void outerMethod() { class InnerClass {...} } See example 2-30 on page 129 –accesses all of these things from different scopes: private instance variable of enclosing object [equationInput] arguments of the instance method the local inner class was defined in [input1 & input2] private variable local to method [localVar[2]] private instance of local inner class [normalField]

23 Anonymous Local Inner Classes syntax: –Type objectName = new [SuperType()] {...} (If you don’t provide a SuperType, defaults to Object, as usual.) This syntax both defines the class and instantiates it. No name, and no constructors. To initialize instance variables, use a initialization statement: { // initializations go here }

24 Group Task None for now! Will be combined with other tasks, relating to Threads, Events, GUI, etc.


Download ppt "Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local."

Similar presentations


Ads by Google