• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

anonymous inner class constructor can take argumnts

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possiable that anonymous inner class constructor can take argumnts?
If yes please explain with example
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

As the name tells, an anonymous inner class is a local class without a name.

And since it has no name, it is not possible for it to define a constructor within the class body. They usually subclass the classes that do not take the constructor arguments.

Cheers,
Mamatha
SCJP(1.4)
 
Satyajeet Kadam
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not post questions from that source.

Real SCJP 1.4 question deleted.

[ February 28, 2007: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to say I am a little bit out of this topic the last time but maybe this helps some:

Dan Chisholm:

http://www.danchisholm.net/oct1/topic/section1/anonymous1ans.html
see the answers on question 1 and 2. Especially answer 1 gives info.

(by the way, the questions are on:
http://www.danchisholm.net/oct1/topic/section1/anonymous1.html

regards,

Ronald
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amol,

First need of anonymous class?
A class with no name and that is used to extend a class or implement an interface, of course not both at a time. Use of anonymous class is to provide immediate extender of a class or immediate implementer of an interface.


Now discussion on your question:

What after a new keyword you write name of the class or interface as:
abstract class Drink{
Drink(){}
Drink(int a){
this.a=a;
}
void cool();
private int a;
}


class Coke {

public static void main(String[] args) {
Drink b1 = new Drink(5) {// Line 1
void cool() {
System.out.println(�Overridden cool()�);
}

void hot() {
System.out.println(�hot() method�);
}
};
System.out.println(b1.a);//prints 5
b1.cool(); //OK
b1.hot();//Error, only overridden method can be called, or in other words
//the methods the parent class has knowledge of


}
}






Meaning of Line 1:
Create a reference variable named b1 and then create an anonymous class (unnamed class) that is subclass of the �Drink� class and pass the argument value 5 to the constructor of the Drink class. No instantiation of the Drink class is being done there.
The anonymous class can�t have constructor itself because it has no name and as you know to make a constructor we need the name of a class (constructor name is the name of class without any return type). Bottom line: �b1 refers to an anonymous class that extends the Drink class(that is Parent class of the anonymous class)�

To make the concept more clearer I have made the Drink class abstract, so that you could be sure that there is no instantiation of Drink class at Line 1. Again �b1 is refereing to an anonymous class that is subclass of the Drink class. �

I hope this elaboration helps you!

With regards,
chandrabhatt
 
brevity is the soul of wit - shakepeare. Tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic