answersLogoWhite

0

Anonymous Inner Classes - Type One

Check out the following code:

class Ferrari {

public void drive() {

System.out.println("Ferrari");

}

}

class Car {

Ferrari p = new Ferrari() {

public void drive() {

System.out.println("anonymous Ferrari");

}

};

}

Let's look at what's in the preceding code:

• We define two classes, Ferrari and Car.

• Ferrari has one method, drive().

• Car has one instance variable, declared as type Ferrari. That's it for Car. Car has no methods.

And here's the big thing to get

The Ferrari reference variable refers not to an instance of Ferrari, but to an instance of an anonymous (unnamed) subclass of Ferrari.

Let's look at just the anonymous class code:

2. Ferrari p = new Ferrari() {

3. public void drive() {

4. System.out.println("anonymous Ferrari");

5. }

6. };

Line 2

Line 2 starts out as an instance variable declaration of type Ferrari. But instead of looking like this:

Ferrari p = new Ferrari(); // notice the semicolon at the end

there's a curly brace at the end of line 2, where a semicolon would normally be.

Ferrari p = new Ferrari() { // a curly brace, not a semicolon

You can read line 2 as saying,

Declare a reference variable, p, of type Ferrari. Then declare a new class that has no name, but that is a subclass of Ferrari. And here's the curly brace that opens the class definition...

Line 3

Line 3, then, is actually the first statement within the new class definition. And what is it doing? Overriding the drive() method of the superclass Ferrari. This is the whole point of making an anonymous inner class-to override one or more methods of the superclass!

Line 4

Line 4 is the first statement within the overriding drive() method. Nothing special here.

Line 5

Line 5 is the closing curly brace of the drive() method. Nothing special here either.

Line 6

Here's where you have to pay attention: line 6 includes a curly brace closing off the anonymous class definition (it's the companion brace to the one on line 2), but there's more! Line 6 also has the semicolon that ends the statement started on line 2-the statement where it all began-the statement declaring and initializing the Ferrari reference variable. And what you're left with is a Ferrari reference to a brand-new instance of a brand-new, anonymous (no name) subclass of Ferrari.

Anonymous Inner Classes Type Two

The only difference between type one and type two is that type one creates an anonymous subclass of the specified class type, whereas type two creates an anonymous implementer of the specified interface type. In the previous examples, we defined a new anonymous subclass of type Ferrari as follows:

Ferrari p = new Ferrari() {

But if Ferrari were an interface type instead of a class type, then the new anonymous class would be an implementer of the interface rather than a subclass of the class. Look at the following example:

interface Drivable {

public void drive();

}

class Car {

Drivable c = new Drivable() {

public void drive() {

System.out.println("anonymous Drivable implementer");

}

};

}

The preceding code, like the Ferrari example, still creates an instance of an anonymous inner class, but this time the new class is an implementer of the Drivable interface. And note that this is the only time you will ever see the syntax

new Drivable()

where Drivable is an interface rather than a non-abstract class type. Because think about it, you can't instantiate an interface, yet that's what the code looks like it's doing. But of course it's not instantiating a Drivable object, it's creating an instance of a new, anonymous, implementer of Drivable. You can read this line:

Drivable c = new Drivable() {

as, "Declare a reference variable of type Drivable that, obviously, will refer to an object from a class that implements the Drivable interface. But, oh yes, we don't yet have a class that implements Drivable, so we're going to make one right here, right now. We don't need a name for the class, but it will be a class that implements Drivable, and this curly brace starts the definition of the new implementing class."

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

What ananomus classes in java?

An Anonymous class in Java is one that does not have a name. It is usually created inside a class


How garbage collector works on anonymous class in java?

about the garbage collector it is in java and it is mainly responsible for dynamic memory manegement


What is an anonymous class?

An anonymous class is a local class without a name. An anonymous class is defined and instantiated in a single succinct expression using the new operator. While a local class definition is a statement in a block of Java code, an anonymous class definition is an expression, which means that it can be included as part of a larger expression, such as a method call. When a local class is used only once, consider using anonymous class syntax, which places the definition and use of the class in exactly the same place.New Syntax for Anonymous ClassesWe've already seen examples of the syntax for defining and instantiating an anonymous class. We can express that syntax more formally as: new class-name ( [ argument-list ] ) { class-body }or: new interface-name () { class-body }


What is the meaning of anonymous classes for events in java?

Anonymous classes, basically, have no name. It combines the class declaration and the creation of an instance of the class in one step. If an event handler is not shared by multiple components there is no need to declare a class to handle the event. The handler can be implemented with the use of an anonymous inner class. i.e. : button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "You clicked button1."); } });


Why you start java program by class?

without class non of the folder can run so the java program should start in class we can use the class without object in java


When java uses compiler?

What i know is java we will use compiler when it want to get class file(file with .class extension) from java file(file with .java extension).


What is anonymous object in java?

Objects without their declarations known as Anonymous Objects. these are known as use & throw objects because these are died after one time use.


Can you use java class in webpage?

Yes.


What is a connection class in java?

to connect java and data base...use connection stament


Extension file for Java?

Java source files have the .java extension, compiled Java class files have the .class extension.


How many classes should be included in each java file?

You can only have one non-inner public classes per java file and that class name must match the filename. The java file can also have any number of inner classes and anonymous classes.


Is it possible to have file name as java eg javajava?

Yes you can name a file (class if you use eclipse) java or even have java in the name.