Win a copy of liveProject: Build an ML Recommender System this week in the Artificial Intelligence and Machine Learning forum!

Clemonte Johnstone

Ranch Hand
+ Follow
since Jan 05, 2021
Cows and Likes
Cows
Total received
2
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
6
Given in last 30 days
3
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Clemonte Johnstone

Ok Line n1 is clear. However, I do not understand why Line n3 throws ArrayIndexOutOfBoundsException
Dear All,

The source of this question comes from the Online Udemy Course "Pass the Oracle Certified Associate(OCA): Java SE 8 Programmer I EXAM"
Instructor: Udayan Khattry

Please could someone kindly explain why and what the value of arr = new String[?][?] is?
Also, what happens after that line of code is executed. I am also confused on why an array is declared in the outer for loop.





Thanks in advance for any help that you can give.
Dear All,

The source of this question comes from the Online Udemy Course "Pass the Oracle Certified Associate(OCA): Java SE 8 Programmer I EXAM"
Instructor: Udayan Khattry


Please could someone kindly explain how on earth this program prints -1 when executed?



Thanks in advance for any help that you can give.

Mike Simmons wrote:

Clemonte Johnstone wrote:1) Why throws Exception is not allowed if supplemented below?


Because when you override a method, the new method may not add any new checked exception types.  

In your example, throwing ArrayIndexOutOfBoundsException is OK, because (a) it's unchecked, and (b) it's a subtype of IndexOutOfBoundsException.  You're not throwing a new checked exception; you're saying you won't throw any IndexOutOfBoundsException other than the more specific subtype ArrayIndexOutOfBoundsException.  That's allowed, even if these were checked exceptions (which they are not).

But throws Exception is definitely a new type of checked exception.  It includes the unchecked IndexOutOfBoundsException and ArrayIndexOutOfBoundsException, but also may include many other things like IOException.  It could be anything.  Definitely not allowed by the super method declaration.

Clemonte Johnstone wrote:2) Can we say that we are overriding the method declaration in the Subclass?


Yes.  You are overriding it, providing a method with the same signature.  You are not implementing it, since it's still abstract, no implementation, but you are overriding it.  This can be done to modify the method in a few allowed ways, including (a) making the method more accessible (not less), (b) changing the return type to a more specific subtype, if applicable, (c) changing the throws clause to a more restrictive (not less) list of checked types (plus any unchecked types you like), and (d) adding a new JavaDoc comment (which hopefully modifies but does not violate the contract of the super method -- that's up to you though, the compiler doesn't care)




Mike here is another example from Udayyan Khattry, please could you kindly explain about the throws exception type declarations. I have noted that main throws exception, and Sub1 is throwing IOException which is a sub-type, and that the main is throwing RuntimeException which is again a sub-type.

class Super1 {
   Super1() throws RuntimeException {
       System.out.print("1 ");
   }
}

class Sub1 extends Super1 {
   Sub1() throws IOException {
       System.out.print("2 ");
   }
}

public class _13_Question_Constructors_And_Throws_Exception_Declarations {
   public static void main(String[] args) throws Exception {
       new Sub1();
   }
}
Hi, is anyone able to help please in relation to the above?
Dear All,
Please could someone explain to me:

1) Why throws Exception is not allowed if supplemented below?
2) Can we say that we are overriding the method declaration in the Subclass?





The source of this question comes from the Online Udemy Course "Pass the Oracle Certified Associate(OCA): Java SE 8 Programmer I EXAM"
Instructor: Udayan Khattry
Can we also not say that X is an instance of X1?

Stephan van Hulst wrote:Then I'm afraid I can't help you further.



This is polymorphism right?


Stephan van Hulst wrote:

Clemonte Johnstone wrote:Its polymorphism as well right as X1 is instantiating X?


Can you elaborate on your question? I don't know what you mean.



I have nothing more to add. What I have stated should be enough information.

Paul Clapham wrote:

Stephan van Hulst wrote:What error message are you getting?



Come on. If your goal is to become a Java programmer, you should know by now that the error messages are there to tell you something about the problem.



Its polymorphism as well right as X1 is instantiating X?

Paul Clapham wrote:

Stephan van Hulst wrote:What error message are you getting?



Come on. If your goal is to become a Java programmer, you should know by now that the error messages are there to tell you something about the problem.



Apologies the code compiles. It was because of a duplicate class in my test suite. Its because I work very hard and have overlooked this yesterday. Sorry.

Clemonte Johnstone wrote:

Stephan van Hulst wrote:What error message are you getting? To me, that code looks valid and it should print "X3".


The code does not execute. It gives a compilation error on line 17



Its polymorphism as well right as X1 is instantiating X?

Stephan van Hulst wrote:What error message are you getting? To me, that code looks valid and it should print "X3".


The code does not execute. It gives a compilation error on line 17
Dear all,

Please could someone kindly explain why generates a compilation error? I do not think it is because it is NOT overriding the methods of X3. The IDE is not giving me any helpful info to determine the root cause. Thanks in advance for any help that you can give.

The source of this question comes from the Online Udemy Course "Pass the Oracle Certified Associate(OCA): Java SE 8 Programmer I EXAM"
Instructor: Udayan Khattry