1 © Luxoft Training 2012 Inner and anonymous classes.

Презентация:



Advertisements
Похожие презентации
Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
Advertisements

2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface.
© Luxoft Training 2013 Using Reflection API in Java.
A class is just a collection of variables--often of different types--combined with a set of related functions. The variables in the class are referred.
© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.
Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
Unit II Constructor Cont… Destructor Default constructor.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Polymorphism.
Object-Oriented Programming Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
1 © Luxoft Training 2012 Java basics Module 2. 2 © Luxoft Training 2012 Running Java application.
1/27 Chapter 9: Template Functions And Template Classes.
2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
© Luxoft Training 2013 Java Collections API. © Luxoft Training 2013 Collections hierarchy.
1/30 Chapter 8: Dynamic Binding And Abstract classes.
Initialization & Cleanup Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
Loader Design Options Linkage Editors Dynamic Linking Bootstrap Loaders.
© 2006 Avaya Inc. All rights reserved. Programmable Buttons and features.
PAT312, Section 21, December 2006 S21-1 Copyright 2007 MSC.Software Corporation SECTION 21 GROUPS.
Michael Marchenko. In mathematics, a sequence is an ordered list of objects (or events). Like a set, it contains members (also called elements, or terms),
My favourite website Katya Kupko Form 10 A. My favorite site is
Транксрипт:

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(); }