About Question enthuware.ocajp.i.v7.2.1075 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
jlanpheer
Posts: 1
Joined: Tue Dec 09, 2014 8:15 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1075 :

Post by jlanpheer »

Can anybody explain why option (e) is not correct?

option (e) is:
abstract void f();

I don't understand why the BigBang class would have to be declared abstract if the abstract method f is declared in the superclass. The rest i understand.
thanx,
jim.

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

Re: About Question enthuware.ocajp.i.v7.2.1075 :

Post by admin »

If base class has an abstract method, the subclass is supposed to provide an implementation unless subclass is also abstract. If you think about it, it makes sense. If the base class has an abstract method, what will happen if you instantiate an object of the subclass and call that method on the subclass instance?
-Paul.
If you like our products and services, please help us by posting your review here.

chandu1987
Posts: 4
Joined: Wed Dec 24, 2014 11:41 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1075 :

Post by chandu1987 »

Code: Select all

//void k(){ i++; } //(3)
Why this is not wrong? Can you explain? I thought non static methods cannot access static methods/fields

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

Re: About Question enthuware.ocajp.i.v7.2.1075 :

Post by admin »

Actually, you got it reverse. static methods cannot access non-static methods/field. non-static ie. instance methods can access static methods and fields.

Also, please make sure you understand what the above really means. You will see the above statement in many places but it is very misleading. A static method does not have the implicit variable 'this' and that is why it cannot directly access an instance fields. But it can certainly access instance fields through a valid reference. For example:

Code: Select all

public class X{

   public int i; //instance field

   public static void m(){
      System.out.println(i); //this is invalid because i means this.i but m() is static and it doesn't have 'this'
      X x = new X();
      System.out.println(x.i); //this is valid 
   } 
}
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

zhengye1
Posts: 17
Joined: Wed Jan 07, 2015 12:06 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1075 :

Post by zhengye1 »

I think the reason for option "abstract void f();" is wrong.

The original reason in the test said .
If this line is inserted, then class BigBang will have to be declared abstract.
But if you using Eclipse to write the code, and you declare BigBang to be an abstract class like

Code: Select all

abstract final class BigBand extends Bang
Eclipse complain "The class BigBang can be either abstract or final, not both".

In my opinion, the reason why option "abstract void f();" wrong is if we comment out this line, since BigBang is subclass of Bang, it has to provide the method body for f() in BigBang class.

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

Re: About Question enthuware.ocajp.i.v7.2.1075 :

Post by admin »

Yes, abstract and final cannot go together. So when the explanation says that you have to make the class abstract, it also implies that it cannot be final.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Mushfiq Mammadov
Posts: 32
Joined: Fri Oct 16, 2015 4:33 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1075 :

Post by Mushfiq Mammadov »

I read the above posts and I also think that It will be good to write "If this line is inserted, then class BigBang will have to implement abstract method" instead of "If this line is inserted, then class BigBang will have to be declared abstract". The code will not still compile if we remove final and add abstract keyword to class BigBang, because we create an instance of BigBang in main method (Bang mc = new BigBang();).

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

Re: About Question enthuware.ocajp.i.v7.2.1075 :

Post by admin »

If you write, "... have to implement abstract method" then it can be argued that it is not required because the class can be declared abstract and if you write, "...class BigBang will have to be declared abstract", then it can be argued that the class can also implement the abstract method. So either way, some people will not be happy. So I am adding both the options to the explanation :)

thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.

meghajor
Posts: 7
Joined: Fri Nov 20, 2015 7:03 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1075 :

Post by meghajor »

Hi Paul

If abstract void f(); cannot be inserted why is class Bang declared as abstract? Abstract class needs atleast one abstract method right?

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

Re: About Question enthuware.ocajp.i.v7.2.1075 :

Post by admin »

meghajor wrote:Hi Paul

If abstract void f(); cannot be inserted why is class Bang declared as abstract? Abstract class needs atleast one abstract method right?
No, that is not correct. You can declare any class as abstract. No need to have any abstract method.
I would suggest you to go through a book before attempting the mock exams.
-Paul.
If you like our products and services, please help us by posting your review here.

meghajor
Posts: 7
Joined: Fri Nov 20, 2015 7:03 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1075 :

Post by meghajor »

Yes I got confused. My book says even if a single method is declared abstract then the whole class must be declared abstract.
Not the other way round

Russtam
Posts: 9
Joined: Fri Dec 04, 2015 11:27 am
Location: Saint-Petersburg
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1075 :

Post by Russtam »

I think it's typo in available options:
void k( ) { i++ }   //(3)
void l( ) { j++ }  //(4)
semicolons are missed

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

Re: About Question enthuware.ocajp.i.v7.2.1075 :

Post by admin »

You are right. Fixed.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1075 :

Post by NickWoodward »

good question this one!

got it right, but I disagree with it being labelled 'very easy'!

//(0) - you have to know that f() needs to be implemented in the subclass

//(1) - that final cannot be overridden

//(2) - that there'd be no default constructor

//(4) - that j is not inherited

just my opinion, but that's a fair amount of good knowledge in one question.

Nick

Post Reply

Who is online

Users browsing this forum: No registered users and 25 guests