Page 1 of 1

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

Posted: Fri Dec 12, 2014 7:50 pm
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.

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

Posted: Fri Dec 12, 2014 11:21 pm
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.

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

Posted: Wed Dec 24, 2014 11:46 pm
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

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

Posted: Thu Dec 25, 2014 12:49 am
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.

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

Posted: Wed Jan 07, 2015 12:13 am
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.

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

Posted: Wed Jan 07, 2015 4:24 am
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.

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

Posted: Mon Nov 16, 2015 5:36 am
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();).

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

Posted: Mon Nov 16, 2015 9:55 am
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.

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

Posted: Mon Nov 23, 2015 5:38 pm
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?

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

Posted: Mon Nov 23, 2015 7:33 pm
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.

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

Posted: Tue Nov 24, 2015 5:56 pm
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

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

Posted: Sat Dec 05, 2015 6:14 pm
by Russtam
I think it's typo in available options:
void k( ) { i++ }   //(3)
void l( ) { j++ }  //(4)
semicolons are missed

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

Posted: Sat Dec 05, 2015 8:09 pm
by admin
You are right. Fixed.
thank you for your feedback!

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

Posted: Fri Feb 12, 2016 7:05 am
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