About Question enthuware.ocpjp.v7.2.1369 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Wisevolk
Posts: 33
Joined: Thu Jul 18, 2013 6:06 pm
Contact:

About Question enthuware.ocpjp.v7.2.1369 :

Post by Wisevolk »

Hi, So the only way to use doA(0 declare in the anonymous class is to use it whitin that declaration ?

admin
Site Admin
Posts: 8996
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1369 :

Post by admin »

Not sure what you mean. Can you please put what you mean in code?
-Paul.
If you like our products and services, please help us by posting your review here.

Wisevolk
Posts: 33
Joined: Thu Jul 18, 2013 6:06 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1369 :

Post by Wisevolk »

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; }

admin
Site Admin
Posts: 8996
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1369 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

Wisevolk
Posts: 33
Joined: Thu Jul 18, 2013 6:06 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1369 :

Post by Wisevolk »

Thnks

vijayanand
Posts: 10
Joined: Fri Sep 26, 2014 8:40 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1369 :

Post by vijayanand »

Does this mean that the anonymous inner class inner1 is like extending class Inner with its doA() method ?

admin
Site Admin
Posts: 8996
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1369 :

Post by admin »

Yes, that is correct.
If you like our products and services, please help us by posting your review here.

vijayanand
Posts: 10
Joined: Fri Sep 26, 2014 8:40 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1369 :

Post by vijayanand »

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?

admin
Site Admin
Posts: 8996
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1369 :

Post by admin »

Sorry, I don't understand your question.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1369 :

Post by jagoneye »

Also note guys non-overrided methods inside anonymous class can be called!
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()
   {
   }
}
I think this should also be added to the explaination if it is worth it! :)

Harvey Manfrenjensen
Posts: 14
Joined: Fri May 11, 2018 6:59 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1369 :

Post by Harvey Manfrenjensen »

>>>Observe that there is no way doA() can be accessed.
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;
}

admin
Site Admin
Posts: 8996
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1369 :

Post by admin »

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.

Harvey Manfrenjensen
Posts: 14
Joined: Fri May 11, 2018 6:59 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1369 :

Post by Harvey Manfrenjensen »

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

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;
}

admin
Site Admin
Posts: 8996
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1369 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest