Java Anonymous inner class

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Object-oriented Programming Concepts
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Topic 10 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.”
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 8: Inner Classes. You're an OO programmer, so you know that for reuse and flexibility/extensibility you need to keep your classes specialized.
CSS446 Spring 2014 Nan Wang.  To be able to declare and use interface types  To appreciate how interfaces can be used to decouple classes  To learn.
Inheritance in the Java programming language J. W. Rider.
1 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.” CS Computer Science II.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
Object Oriented Programming in Java Habib Rostami Lecture 10.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Lecture 5:Interfaces and Abstract Classes
Advanced Programming in Java
Sections Inheritance and Abstract Classes
NESTED CLASSES REFLECTION PROXIES.
Interface, Subclass, and Abstract Class Review
Inheritance and Polymorphism
Leftover Patterns Chain of Responsibility
Generics, Lambdas, Reflections
Interfaces and Inheritance
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Nested class.
Object Oriented Programming
Designing for Inheritance
Abstract Classes.
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Chapter 19 Generics Dr. Clincy - Lecture.
Interface.
Extending Classes.
Overloading and Overriding
Conditional Statements
Week 6 Object-Oriented Programming (2): Polymorphism
Interfaces.
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
Generics.
Abstract Classes An abstract class is a kind of ghost class. It can pass along methods and variables but it can’t ever be instantiated itself. We can.
Abstract Classes Page
Functional interface.
Java Inheritance.
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
Defining Classes and Methods
CS2013 Lecture 7 John Hurley Cal State LA.
Chapter 15 Event-Driven Programming and Animations Part 1
CMPE212 – Reminders Assignment 2 due next Friday.
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Abstract Data Types Abstraction is to distill a system to its most fundamental parts. Applying the abstraction paradigm to the design of data structures.
Presentation transcript:

Java Anonymous inner class A class with no name is an anonymous inner class in java. enables you to make your code more concise. declares and instantiates a class at the same time. local classes except that they do not have a name. Use them if you need to use a local class only once.

If you have to override method of class or interface. Can be created by two ways: Class (may be abstract or concrete). Interface

A class is created and its name is decided by the compiler which extends the Person class and provides the implementation of the eat() method. An object of Anonymous class is created that is referred by p reference variable of Person type.

A class is created and its name is decided by the compiler which implements the Eatable interface and provides the implementation of the eat() method. An object of Anonymous class is created that is referred by p reference variable of Eatable type.

Interfaces as Callbacks A callback is a situation where you'd like to pass a reference to some behavior and have another object invoke it later.

interface TextUpdateable { receiveText( String text ); } class TickerTape implements TextUpdateable { TextSource source; init() { source = new TextSource( this ); ... public receiveText( String text ) { scrollText( text ): class TextSource { TextUpdateable receiver; TextSource( TextUpdateable r ) { receiver = r; private sendText( String s ) { receiver.receiveText( s );

TextSource object will send any new text data TextSource stores a reference to a TickerTape object, but then we could never use our TextSource to send data to other kind of objects. A more elegant solution is to have TextSource store a reference to an interface type, TextUpdateable. TextSource really cares about is finding the right method to invoke to send text. TickerTape object simply passes a reference to itself as the callback for text updates, and the source invokes its receiveText() method as necessary.