1 Anonymous Classes A local class is a class that is defined in a block. This could be a method body, a constructor, a local block, a static initializer.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
Object-Oriented PHP (1)
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Inner Classes Overview  Java Inner Classes: A Definition.  Overview of Nested Classes.  Top level Nested Classes.  Non-static Inner Classes.  Non-static.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Abstract classes and Interfaces. Abstract classes.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
Interfaces and Inner Classes
Nested Classes CompSci 230 S Software Construction.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Object Oriented Programming in Java Habib Rostami Lecture 10.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
CompSci 230 S Software Construction
Inheritance-Basics.
Final and Abstract Classes
Inheritance and Polymorphism
CompSci 230 S Programming Techniques
Review Session.
UML Class Diagram: class Rectangle
Nested class.
Chapter 9 Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Overloading and Overriding
ITEC324 Principle of CS III
Packages and Interfaces
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
ITEC324 Principle of CS III
Chapter 6 Inheritance.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

1 Anonymous Classes A local class is a class that is defined in a block. This could be a method body, a constructor, a local block, a static initializer or an instance intializer. Such a local class is only visible within the context of the block. I.e. the name of the class is only valid in the context of the block in which it is defined. A local class cannot be specified with the keyword static. However, if the context is statc(i.e. a static method or a static initializer) then the local class is implicitly static. Otherwise, the local class is non-static Like non-static inner classes, an instance of a non-static class is passed a hidden reference designating an instance of its enclosing class in its constructors, and this gives non-static local classes much of the same capability as non-static inner classes l A local class can access members defined within the class. l A local class can access final local variables, final method p arameters. Some restrictions which apply to local classes are: 1- Local classes cannot have static members, as the cannot provide class-specific services. 2- Local classes cannot have any accessibility. This restriction applies to local variables, and is also enforced for local classes

2 Anonymous Classes Local Classes Example (Illustrates Access Rules of Local Classes) class SuperB { protected double x; protected static int n; } class SuperC { protected double y; protected static int m; } class TopLevelA extends SuperC { // Top-level Class private double z; private static int p; void nonStaticMethod(final int i) { // Non-static Method final int j = 10; int k; class NonStaticLocalD extends SuperB { // Non-static local class // static double d; // (1) Not OK. Only non-static members allowed. int ii = i; // (2) final from enclosing method. int jj = j; // (3) final from enclosing method. // double kk = k; // (4) Not OK. Only finals from enclosing method. double zz = z; // (5) non-static from enclosing class. int pp = p; // (6) static from enclosing class. double yy = y; // (7) inherited by enclosing class. int mm = m; // (8) static from enclosing class. double xx = x; // (9) non-static inherited from superclass int nn = n; // (10) static from superclass }

3 Anonymous Classes Local Class Example static void staticMethod(final int i) { // Static Method final int j = 10; int k; class StaticLocalE extends SuperB { // Static local class // static double d; // (11) Not OK. Only non-static members allowed. int ii = i; // (12) final from enclosing method. int jj = j; // (13) final from enclosing method. // double kk = k; // (14) Not OK. Non-final from enclosing method. // double zz = z; // (15) Not OK. Non-static member. int pp = p; // (16) static from enclosing class. // double yy = y; // (17) Not OK. Non-static member. int mm = m; // (18) static from enclosing class. double xx = x; // (19) non-static inherited from superclass int nn = n; // (20) static from superclass }

4 Anonymous Classes Local Classes A local class can access final local variables, final method parameters an final catch-block parameters in the socpe of the local context. Such final variables are also read-only in the local class. This situation is shown at(2)and (3), where the final paramter I and the final local variable j of the method nonStaticMethod() in the non- static local class NonStaticLocalID are accessed. This also applies to static local classes, as shown at(12) and (13. Access to non-final local variables is not permitted from local classes, as shown at(4) and(14). A non-static local class can access memebers defined in the enclosing class. This situation is shown at(5) and(6), where the instance variable z and static variable p defined in the enclosing class TopLevelA are accessed, respectively. The special form of the this construct can be used for explicit refeencing of the members defined in the enclosing class: double zz=TopLevelA.this.z; Howerver, aa static local class cn only directly acces static members defined in the enclosing class, as shown at(16), but non-static members, as shown at (15)

5 Anonymous Classes l Classes are usually first defined and then instantiated using the new operator. Anonymous classes combine the process of definition and instantiation into a single step. Anonymous classes are defined at the location they are instantiated, using additional syntax with the new operator. As these do not have a name, an instance of the class can only be created together with the definition l Anonymous class is the act of declaring an inner class without naming it l An anonymous class is declared as part of a new expression and must either be a subclass or implement an interface new ( ) { } new interfaceName( ) { classBody} className is the name of the superclass of the anonymous class, while interfaceName is the name of the interface to which the anonymous class must conform. The argumentList is a parameter list that is used to call a matching constructor of the named superclass. Anonymous class cannot define constructors (as it does not have name). No extends clause is used in the construct

6 Anonymous Classes Anonymous Classes (Cont’d) l Class is anonymous if it does not have a name, In a program, something that is only used once doesn’t usually need a name. For example, you can replace Rectangle rect =new Rectangle(100,100,100,100); g2.fill(rect); With g2.fill(new Rectangle (100,100,100,100)); l If the rectangle is not used elsewhere in the same method. The object new Rectangle(100,100,100,100)is an anonymous object.

7 Anonymous Classes Anonymous Inner Classes public class Parcel { public Contents cont() { return new Contents() { private int i = 11; public int value() { return i; } // End of method value() }; // Semicolon required in this case } // End of method cont() public static void main(String[] args) { Parcel p = new Parcel(); Contents c = p.cont(); } // End of method main() } // End of class Parcel Anonymous Inner Class Look at the code shown below: The method cont() of the class Parcel returns a reference to an anonymous class defined within the same method and called Contents. This syntax means: ”create an object of an anonymous class that’s inherited from Contents”. The reference returned by the new expression is automatically upcast to contents reference.

8 Anonymous Classes Anonymous Class Example interface IDrawable { // (1) void draw(); } class Shape implements IDrawable { // (2) public void draw() { System.out.println("Drawing a Shape."); } } class Painter { // (3) Top-level Class public Shape createShape() { // (4) Non-static Method return new Shape(){ // (5) Extends superclass public void draw() { System.out.println("Drawing a new Shape."); } }; } public static IDrawable createIDrawable() { // (7) Static Method return new IDrawable(){ // (8) Implements interface public void draw() { System.out.println("Drawing a new IDrawable."); } }; }

9 Anonymous Classes Anonymous Class Example (Cont’d) public class Anon { public static void main(String args[]) { // (9) IDrawable[] drawables = { // (10) new Painter().createShape(), // (11) non-static anonymous class Painter.createIDrawable(), // (12) static anonymous class new Painter().createIDrawable() // (13) static anonymous class }; for (int i = 0; i < drawables.length; i++) // (14) drawables[i].draw(); System.out.println("Anonymous Class Names:"); System.out.println(drawables[0].getClass()); // (15) System.out.println(drawables[1].getClass()); // (16) } }

10 Anonymous Classes The instance method createShape() at (4) deifines a non-static anonymous class at(5), which extends the superclass Shape. The anonymous class overrides the inherited method draw(). As references to an anonymous class cannot be declared, the functionality of the class is only available through superclass references, Usually it makes sense to either override methods from the superclass or implement abstract methods from the superclass. An anonymous class implementing an interface, the static method createIDrawable() at (7) deifnes a static anonymous class at(8), which implements the interface IDrawable by providing an implementation of the method draw(. The class Client creates on instance at (11) of the non-static anonymous class defined at(5), and two instances at(12) and (13) respectively of the static anonymous class defined at(8). The program output shows the polymorphic behavior and the runtime types of the objects.

11 Anonymous Classes Anonymous Class Example 2 public class EggApplet extends Applet { public EggApplet() { egg = new Ellipse2D.Double(0, 0, EGG_WIDTH, EGG_HEIGHT); // add mouse click listener MouseClickListener listener = new MouseClickListener(); addMouseListener(listener); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.draw(egg); } private Ellipse2D.Double egg; private static final double EGG_WIDTH = 30; private static final double EGG_HEIGHT = 50; private class MouseClickListener extends MouseAdapter { public void mouseClicked(MouseEvent event) { int mouseX = event.getX(); int mouseY = event.getY(); // Now move the ellipse to (mouseX, mouseY) egg.setFrame(mouseX - EGG_WIDTH / 2, mouseY - EGG_HEIGHT / 2, EGG_WIDTH, EGG_HEIGHT); repaint(); }

12 Anonymous Classes Anonymous Class Example 2 (Cont’d) public class Myapplet extends Applet { public MyApplet() { addMouseListener(new MouseClickListener()); } private class MouseClickListener extends MouseAdapter { public void mouseClicked(MouseEvent event) { // mouse click action goes here } } } public class Myapplet extends Applet { public MyApplet() { addMouseListener(new MouseClickListener() { // name of superclass public void mouseClicked(MouseEvent event) { // methods of anonymous subclass // mouse click action goes here } }); } }