About Question enthuware.ocajp.i.v7.2.1075 :
Moderators: Site Manager, fjwalraven
-
- Posts: 1
- Joined: Tue Dec 09, 2014 8:15 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1075 :
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.
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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1075 :
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.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Wed Dec 24, 2014 11:41 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1075 :
Code: Select all
//void k(){ i++; } //(3)
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1075 :
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:
HTH,
Paul.
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
}
}
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 17
- Joined: Wed Jan 07, 2015 12:06 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1075 :
I think the reason for option "abstract void f();" is wrong.
The original reason in the test said .
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.
The original reason in the test said .
But if you using Eclipse to write the code, and you declare BigBang to be an abstract class likeIf this line is inserted, then class BigBang will have to be declared abstract.
Code: Select all
abstract final class BigBand extends Bang
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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1075 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Fri Oct 16, 2015 4:33 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1075 :
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();).
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1075 :
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.
thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Fri Nov 20, 2015 7:03 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1075 :
Hi Paul
If abstract void f(); cannot be inserted why is class Bang declared as abstract? Abstract class needs atleast one abstract method right?
If abstract void f(); cannot be inserted why is class Bang declared as abstract? Abstract class needs atleast one abstract method right?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1075 :
No, that is not correct. You can declare any class as abstract. No need to have any abstract method.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?
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.
-
- Posts: 7
- Joined: Fri Nov 20, 2015 7:03 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1075 :
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
Not the other way round
-
- Posts: 9
- Joined: Fri Dec 04, 2015 11:27 am
- Location: Saint-Petersburg
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1075 :
I think it's typo in available options:
semicolons are missedvoid k( ) { i++ } //(3)
void l( ) { j++ } //(4)
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1075 :
You are right. Fixed.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Mon Mar 30, 2015 6:00 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1075 :
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
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
Who is online
Users browsing this forum: No registered users and 25 guests