About Question enthuware.ocpjp.v7.2.1369 :
Moderator: admin
-
- Posts: 33
- Joined: Thu Jul 18, 2013 6:06 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1369 :
Hi, So the only way to use doA(0 declare in the anonymous class is to use it whitin that declaration ?
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1369 :
Not sure what you mean. Can you please put what you mean in code?
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 33
- Joined: Thu Jul 18, 2013 6:06 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1369 :
in your code the method declare at //2 can only be use in the scope of the new inner() declaration at //1 because it's an anonymous class ?
package test;
public class TopClass {
public Inner inner1 = new Inner() //1
{
public void doA(){ // 2
System.out.println("doing A");
}
};
public void doA() { inner1.doA(); }
}
class Inner { int value; }
package test;
public class TopClass {
public Inner inner1 = new Inner() //1
{
public void doA(){ // 2
System.out.println("doing A");
}
};
public void doA() { inner1.doA(); }
}
class Inner { int value; }
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1369 :
Yes, but not just because it is an anonymous inner class. It is also because the declaration of doA() is not present in class Inner. So you cannot call doA() on a variable of type Inner.
It is really same as introducing a new method in a sub class. For example, if you have two classes InputDevice and KeyBoard, where KeyBoard extends InputDevice and KeyBoard has an extra method getNumberOfKeys(), you can't call this method on a variable of class InputDevice. i.e.
InputDevice id = new KeyBoard() ; //This is fine
id.getNumberOfKeys(); //This will not compile.
Of course, within the KeyBoard class, you can call getNumberOfKeys() on 'this'.
HTH,
Paul.
It is really same as introducing a new method in a sub class. For example, if you have two classes InputDevice and KeyBoard, where KeyBoard extends InputDevice and KeyBoard has an extra method getNumberOfKeys(), you can't call this method on a variable of class InputDevice. i.e.
InputDevice id = new KeyBoard() ; //This is fine
id.getNumberOfKeys(); //This will not compile.
Of course, within the KeyBoard class, you can call getNumberOfKeys() on 'this'.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 33
- Joined: Thu Jul 18, 2013 6:06 pm
- Contact:
-
- Posts: 10
- Joined: Fri Sep 26, 2014 8:40 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1369 :
Does this mean that the anonymous inner class inner1 is like extending class Inner with its doA() method ?
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1369 :
Yes, that is correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Fri Sep 26, 2014 8:40 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1369 :
What i understand is anonymous inner class can be created only in a code block (inside method, constructor, or intialization block).
How does compiler allows creating anonymous inner class inside top-level class?
How does compiler allows creating anonymous inner class inside top-level class?
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1369 :
Sorry, I don't understand your question.
If you like our products and services, please help us by posting your review here.
-
- Posts: 97
- Joined: Wed Dec 28, 2016 9:00 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1369 :
Also note guys non-overrided methods inside anonymous class can be called!
It can be done by placing the call inside the overrided method.
I think this should also be added to the explaination if it is worth it!
It can be done by placing the call inside the overrided method.
Code: Select all
class InnerTest1
{
public static void main(String args[])
{
InnerTest1 i1 = new InnerTest1(){
public void callme()
{
m1();
}
void m1(){
System.out.println("m1");
}
};
i1.callme();
}
public void callme()
{
}
}
-
- Posts: 14
- Joined: Fri May 11, 2018 6:59 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1369 :
>>>Observe that there is no way doA() can be accessed.
fyi...
fyi...
Code: Select all
class TopClass
{
public Inner inner1 = new Inner()
{
public Inner doA(){
System.out.println("doing A");
return this;
}
}.doA(); // call doA() here
public void doA() { inner1.doA(); }
}
class Inner
{
int value;
}
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1369 :
Assuming that you are trying to show a counter example, have you even tried to compile your code?
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Fri May 11, 2018 6:59 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1369 :
you are right, it does not compile, i took it from the explanation of question,
which show inappropriate call of a new method of anonymous class.
Here is example, that compiles.
Regards, Paul
which show inappropriate call of a new method of anonymous class.
Here is example, that compiles.
Regards, Paul
Code: Select all
class TopClass
{
public Inner inner1 = new Inner()
{
public Inner doA(){
System.out.println("doing A");
return this;
}
}.doA(); // call doA() here
}
class Inner
{
int value;
}
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1369 :
Yes, this compiles but this is not the doA() that is given in the problem statement.
Your code does show an interesting way to invoke a method of an inner class though.
regards,
Paul.
Your code does show an interesting way to invoke a method of an inner class though.
regards,
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 40 guests