About Question enthuware.ocajp.i.v7.2.1231 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1231 :
Explanation says :
The getValue(int i) method of class A in option c, is different than the method defined in the interface because their parameters are different. So this class does not actually implement the method of the interface and so it is declared abstract.
I'm assuming that getValue(int i) is in class B for the third option.
My question is when an abstract class implements an Interface, can it not implement the method in the interface..
ie in this question would it have been wrong if the third option was
abstract class B implements I1
{
String getValue() { return "something"; }
}
The getValue(int i) method of class A in option c, is different than the method defined in the interface because their parameters are different. So this class does not actually implement the method of the interface and so it is declared abstract.
I'm assuming that getValue(int i) is in class B for the third option.
My question is when an abstract class implements an Interface, can it not implement the method in the interface..
ie in this question would it have been wrong if the third option was
abstract class B implements I1
{
String getValue() { return "something"; }
}
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1231 :
A class can be marked abstract even if it does not have any abstract method. If your code would be valid even if it implements all the methods of an interface.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Sat Nov 01, 2014 4:46 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1231 :
Regarding this partial explanation in option c, "...So this class does not actually implement the method of the interface and so it is declared abstract."
I can't figure out why the class has to be declared abstract. Why couldn't it be a non-abstract class? The method in option c is not an abstract method so why make the class abstract?
Thanks.
I can't figure out why the class has to be declared abstract. Why couldn't it be a non-abstract class? The method in option c is not an abstract method so why make the class abstract?
Thanks.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1231 :
If a class says that it implements an interface but doesn't actually define all the interface methods, then it has to be declared abstract.
HTH,
Paul.
HTH,
Paul.
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.1231 :
is it worth pointing out that option A wouldn't be correct even if it implemented the interface - because of default access in the methods?
the same could be said for option C (class B) - getValue(int i) isn't just incorrect because of the arguments, again it's got default access.
obviously the answers are correct, just thought that it might be worth pointing out (if i'm correct that is!)
nick
the same could be said for option C (class B) - getValue(int i) isn't just incorrect because of the arguments, again it's got default access.
obviously the answers are correct, just thought that it might be worth pointing out (if i'm correct that is!)
nick
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1231 :
Yes, that is a good point as well.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Tue Nov 19, 2019 2:01 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1231 :
I see option C is not correct b/c of the parameter difference, but had that been the same and it truly implements `getValue`; isn't it also not correct b/c it does not implement `setValue`? Therefore it would still need to be abstract for that reason as well?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1231 :
Option 3 is this:
and is a correct option.
Code: Select all
abstract class B implements I1{
int getValue(int i) { return 0; }
}
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Tue Nov 19, 2019 2:01 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1231 :
sorry, I typed that wrong. I meant to say Option 3 needs to be abstract (b/c it doesn't implement all methods from the interface), not that it is incorrect.
Is that correct?
Is that correct?
Who is online
Users browsing this forum: Google [Bot] and 37 guests