1 © Luxoft Training 2012 Inner and anonymous classes
2 © Luxoft Training 2012 Inner class A class defined within another class is referred to as nested or inner class. As a matter of fact, a class can be declared in any block, including blocks that are part of a method. What is so peculiar about nested classes is their visibility and accessibility. Note! Classes nested in the method are called local.
3 © Luxoft Training 2012 Example of inner class
4 © Luxoft Training 2012 Inner class The full name of the inner class from the example is OuterOne.InnerOne. Upon compilation, a separate file named OuterOne$InnerOne.class is created. A inner class can only be instantiated through an outer class instance. Note! All attributes of the outer class are available to the nested class, because there is a reference to outer class in the nested class (well be covered later).
5 © Luxoft Training 2012 Inner class Inner class can only be instantiated through an outer class instance. public static void main(String[] args) { OuterOne.InnerOne i = new OuterOne().new InnerOne(); i.innerMethod(); OuterOne outer = new OuterOne(); OuterOne.InnerOne inner = outer.new InnerOne(); } Inner class can be public, private or protected.
6 © Luxoft Training 2012 Static inner classes Inner class can be declared as static. A static nested class cannot use the this keyword to access outer object attributes. Yet, it can request static variables and outer class methods.
7 © Luxoft Training 2012 Inner classes Anything declared within a method is not a class member. Local objects cannot have access modifiers and be declared as static. Anonymous classes can be created. An anonymous class can only access outer objects if they are declared as final.
8 © Luxoft Training 2012 Inner classes
9 © Luxoft Training 2012 Anonymous classes You can declare an inner class within the body of a method without naming it. Anonymous class can be declared as extension to another class or as an interface implementation. Example: Closing of user window by clicking the Close button. The response is programmed as a separate class: class CloseActionListener implements ActionListener
10 © Luxoft Training 2012 Anonymous classes
11 © Luxoft Training 2012 Anonymous classes Note! Inheritance and implementation cannot be used at a time.
12 © Luxoft Training 2012 Anonymous classes Anonymous classes are practical when you dont want to use trivial names for classes. The class code contains several lines. When compiling an anonymous class, a separate class named EnclosingClassName$n is created, where n is an anonymous class order number in the outer class.
13 © Luxoft Training 2012 Anonymous classes A constructor cannot be defined for an anonymous class. The superclass constructor can be called.
14 © Luxoft Training 2012 Anonymous classes
15 © Luxoft Training 2012 Using of inner and anonymous classes public static void main (String[] args) { List pets = new ArrayList<>(); RoboDog d = new RoboDog("Robik"); pets.add(d); // create inner class class SpecialRoboDog extends RoboDog { public void beFriendly() { System.out.println( "I'm very special for you!"); } pets.add(new SpecialRoboDog()); // add anonymous class pets.add(new RoboDog() { public void beFriendly() { System.out.println( "I'm more friendly than everyone else!"); } }); class RoboDog extends Robot implements Pet { private String name; public RoboDog() { this("Noname Robot"); } public RoboDog(String name) { this.name = name; Dog.pets.add(this); } public String getName() { return name; } public void beFriendly() { System.out.println("I'm friendly!"); }
16 © Luxoft Training 2012 Using of inner and anonymous classes // create anonymous class inherited from Cat pets.add(new Cat("Tiger Tigra") { public void beFriendly() { System.out.println( "I'm not friendly! "+ "I'm a Tiger!"); } public String getName() { return ""; } }); // adding Pet interface implementation pets.add(new Pet() { public void beFriendly() { } public String getName() { return "I'm a Pet"; } }); // ask all pets to be friendly for (Pet p: pets) { p.beFriendly(); } class Cat implements Pet { public String name; public Cat(String name) { this.name = name; } public String getName() { return name; } public void beFriendly() { System.out.println("I'm friendly!"); } interface Pet { public String getName(); void beFriendly(); }