About Question com.enthuware.ets.scjp.v6.2.791 and enthuware.ocajp.i.v7.2.1167 :
Moderator: admin
-
- Posts: 3
- Joined: Wed Aug 13, 2014 5:35 pm
- Contact:
About Question com.enthuware.ets.scjp.v6.2.791 and enthuware.ocajp.i.v7.2.1167 :
Why is B's m() not an override of A's m()? or am I looking at this question wrong?
-
- Posts: 3
- Joined: Wed Aug 13, 2014 5:35 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.791 :
the modifier is less restrictive.
//in file A.java
public class A
{
protected void m() throws SomeException{}
}
//in file B.java
public class B extends A
{
public void m(){ }
}
//in file A.java
public class A
{
protected void m() throws SomeException{}
}
//in file B.java
public class B extends A
{
public void m(){ }
}
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.791 :
It is a valid override. The explanation doesn't say that it is not a valid override either. So I am not sure what is the problem?
-Paul.
-Paul.
-
- Posts: 24
- Joined: Wed Sep 28, 2022 9:41 am
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.791 :
Ok.
As I could see. We have 2 correct answers:
A a = new B();
a.m();
A a = new B();
( ( B) a ).m();
Am I right?
As I could see. We have 2 correct answers:
A a = new B();
a.m();
A a = new B();
( ( B) a ).m();
Am I right?
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.791 and enthuware.ocajp.i.v7.2.1167 :
Did you read the explanation provided for this option? It explains exactly why this option is not correct.A a = new B();
a.m();
A's m() declares 'throws SomeException', which is a checked exception, while the main() method doesn't. So a.m() must be wrapped in a try/catch block.
-
- Posts: 2
- Joined: Mon Jan 06, 2025 3:24 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.791 and enthuware.ocajp.i.v7.2.1167 :
Wouldn't this solution not compile?
class B would need to import A via a package since they are in separate files, and TestClass.java would need to import the package(s) containing class A and B to use them.
None of these files import any packages explicitly.
If I'm misunderstanding the question, or if missing such imports accross files is expected to be omitted, please let me know. It does mention "Given that SomeException is a checked exception ...", perhaps does that mean we assume it will compile?
//in file A.java public class A{ protected void m() throws SomeException{} }
//in file B.java public class B extends A{ public void m(){ } }
//in file TestClass.java public class TestClass{ public static void main(String[] args){
//insert code here. //1
} }
class B would need to import A via a package since they are in separate files, and TestClass.java would need to import the package(s) containing class A and B to use them.
None of these files import any packages explicitly.
If I'm misunderstanding the question, or if missing such imports accross files is expected to be omitted, please let me know. It does mention "Given that SomeException is a checked exception ...", perhaps does that mean we assume it will compile?
//in file A.java public class A{ protected void m() throws SomeException{} }
//in file B.java public class B extends A{ public void m(){ } }
//in file TestClass.java public class TestClass{ public static void main(String[] args){
//insert code here. //1
} }
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.791 and enthuware.ocajp.i.v7.2.1167 :
>class B would need to import A via a package since they are in separate files, and TestClass.java would need to import the package(s) containing class A and B to use them.
No, why do you think so? None of the given files have any package statement. That means all of the classes defined in those files are in the same "unnamed" package and so, there is no need for any of them to import each other.
An import is not based on files but is based on packages. A file needs to have an import statement only if a class in that file wants to access a class defined in another package.
No, why do you think so? None of the given files have any package statement. That means all of the classes defined in those files are in the same "unnamed" package and so, there is no need for any of them to import each other.
An import is not based on files but is based on packages. A file needs to have an import statement only if a class in that file wants to access a class defined in another package.
-
- Posts: 2
- Joined: Mon Jan 06, 2025 3:24 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.791 and enthuware.ocajp.i.v7.2.1167 :
Ok great, that makes much more sense, thank you!
Who is online
Users browsing this forum: No registered users and 7 guests