scjp

Protected member behavior once it was inherited.

流过昼夜 提交于 2019-12-21 13:29:07
问题 I've got some doubts regarding protected identifier. In the first chapter of Sun Certified Java Programmer Study Guide by K.Sierra I found the following information: "Once the subclass-outside-the-package inherits the protected member, that member (as inherited by the subclass) becomes private to any code outside the subclass, with the exception of subclasses of the subclass." I provided sample code which reflects the above statement and it is absolutely clear to me. // Parent class package

Understanding join()

故事扮演 提交于 2019-12-21 05:27:06
问题 Suppose a thread A is running. I have another thread, B , who's not. B has been started, is on runnable state. What happens if I call: B.join() ? Will it suspend the execution of A or will it wait for A's run() method to complete? 回答1: join() will make the currently executing thread to wait for the the thread it is called on to die. So - If A is running, and you call B.join(), A will stop executing until B ends/dies. 回答2: Join waits till the thread is dead. If you call it on a dead thread, it

SCJP - override method with exception handling raises a compiler error

橙三吉。 提交于 2019-12-20 04:12:15
问题 In the SCJP book by Kathey Sierra, an excerpt is as follows: If a method is overridden but you use a polymorphic (supertype) reference to refer to the subtype object with the overriding method, the compiler assumes you’re calling the supertype version of the method. If the supertype version declares a checked exception, but the overriding subtype method does not, the compiler still thinks you are calling a method that declares an exception (more in Chapter 5). Let’s take a look at an example:

SCJP - override method with exception handling raises a compiler error

旧城冷巷雨未停 提交于 2019-12-20 04:12:11
问题 In the SCJP book by Kathey Sierra, an excerpt is as follows: If a method is overridden but you use a polymorphic (supertype) reference to refer to the subtype object with the overriding method, the compiler assumes you’re calling the supertype version of the method. If the supertype version declares a checked exception, but the overriding subtype method does not, the compiler still thinks you are calling a method that declares an exception (more in Chapter 5). Let’s take a look at an example:

Var-arg of object arrays vs. object array — trying to understand a SCJP self test question

限于喜欢 提交于 2019-12-19 03:18:54
问题 I'm having trouble understanding this question, and the explanation of the answer for an SCJP 1.6 self test question. Here is the problem: class A { } class B extends A { } public class ComingThru { static String s = "-"; public static void main(String[] args) { A[] aa = new A[2]; B[] ba = new B[2]; sifter(aa); sifter(ba); sifter(7); System.out.println(s); } static void sifter(A[]... a2) { s += "1"; } static void sifter(B[]... b1) { s += "2"; } static void sifter(B[] b1) { s += "3"; } static

Which run first? default values for instance variables or Super Constructors?

我的梦境 提交于 2019-12-18 06:57:26
问题 According to the SCJP6 (Page 507) i found that instance variables are assigned default values before the superclass constructors complete, i tried an example in Debugg mode but i saw that the super contractor runs before instance variables get their default values, could any one explain that to me ? Example i used in case someone want to try it: package courseExercise; class test { test() { System.out.println("Super Constructor run"); } } public class Init extends test { private Integer i = 6

Hidden fields though inheritance

家住魔仙堡 提交于 2019-12-18 05:49:08
问题 In the following code example: class Parent { int x =5; public Integer aMethod(){ System.out.print("Parent.aMthod "); return x; } } class Child extends Parent { int x =6; public Integer aMethod(){ System.out.print("Child.aMthod "); return x; } } class ZiggyTest2{ public static void main(String[] args){ Parent p = new Child(); Child c = new Child(); System.out.println(p.x + " " + c.x); System.out.println(p.aMethod() + " \n"); System.out.println(c.aMethod() + " \n"); } } And the output: 5 6

Getting confused with == and = in “if” statement

孤街浪徒 提交于 2019-12-18 05:46:12
问题 I know that we cant use assignment operator in if statements in java as we use in any other few languages. that is int a; if(a = 1) { } will give a compilation error. but the following code works fine, how? boolean b; if(b = true) { } EDIT : Is this the exception to rule that assignment cant be used in if statement. 回答1: Because the "result" of an assignment is the value assigned... so it's still a boolean expression in the second case. if expressions require the condition to be a boolean

Use of Serializable other than Writing& Reading object to/from File

一世执手 提交于 2019-12-18 04:29:17
问题 In Which Cases it is a good coding practice to use implements serializable other than Writing & Reading object to/from file.In a project i went through code. A class using implements serializable even if in that class/project no any Writing/Reading objects to/from file? 回答1: If the object leaves the JVM it was created in, the class should implement Serializable. Serialization is a method by which an object can be represented as a sequence of bytes that includes the object's data as well as

Slight confusion regarding overriding where variables are concerned

只愿长相守 提交于 2019-12-17 06:12:17
问题 I'm preparing for the SCJP (recently rebranded as OCPJP by Oracle) and one particular question that I got wrong on a mock exam has confused me, the answer description doesn't explain things clear enough. This is the question : class A { int x = 5; } class B extends A { int x = 6; } public class CovariantTest { public A getObject() { return new A(); } public static void main(String[]args) { CovariantTest c1 = new SubCovariantTest(); System.out.println(c1.getObject().x); } } class