-
Notifications
You must be signed in to change notification settings - Fork 90
/
ch02a.html
104 lines (60 loc) · 4.1 KB
/
ch02a.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Study guide for the Oracle Certified Professional, Java SE 8 Programmer Exam ">
<title>Java 8 Programmer II Study Guide: Exam 1Z0-809</title>
<link href="css/code.css" rel="stylesheet" type="text/css" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="js/common-sections.js"></script>
</head>
<body>
<div class="header">
<div class="title-container">
<div class="chapter-title">
<h1><i class="chapter">Chapter TWO</i><br />
Inheritance and Polymorphism</h1>
<p><br /></p>
<h3 style="text-align: center;"><i>Exam Objectives</i></h3>
<p style="text-align: center;"><i>Implement inheritance including visibility modifiers and composition.<br />
Implement polymorphism.<br />
Develop code that uses abstract classes and methods.</i></p>
</div>
</div>
</div>
<div class="container">
<div class="column">
<h2>Answers</h2>
<p><b>1. The correct answer is B.</b><br /> The method:</p>
<p><code class="java hljs"><span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">equals</span><span class="hljs-params">(Question_1_3 q)</span></span></code></p>
<p>Doesn't override the equals method from <code>java.lang.Object</code> (look at the access modifier). For that reason, the <code>if</code> condition fails. Note that if the method call were:</p>
<p><code class="java hljs"><span class="hljs-keyword">if</span>(q1.equals(q2)) { ...</code></p>
<p>We will be calling the method with the <code>protected</code> modifier, making the condition true.</p>
<p><br /></p>
<p><b>2. The correct answer is D.</b><br /> The proper signatures of the methods are:</p>
<ul>
<li><code>public int hashCode()</code></li>
<li><code>public boolean equals(Object obj)</code></li>
<li><code>public String toString()</code></li>
</ul>
<p>So the only correct answer is option D.</p>
<p><br /></p>
<p><b>3. The correct answer is A.</b><br /> You could say that the overload is ambiguous, and compilation fails, since <code>null</code> can be assigned to any type, but in fact, there's a rule that says the in overloading the most <b>SPECIFIC</b> one is chosen. Since <code>Integer</code> is more specific than <code>Object</code>, the former is selected.</p>
<p><br /></p>
<p><b>4. The correct answer is B.</b><br /> Static methods are hidden not overridden. Since the main method is in the subclass, the subclass method is called.</p>
<p>The call to <code>Question_2_4.print()</code> yields the same result:</p>
<p><code>Subclass</code></p>
<p>The call to <code>SuperClass.print()</code> yields:</p>
<p><code>Superclass</code></p>
<p><br /></p>
<p><b>5. The correct answer is A.</b><br /> Static methods are allowed in <code>abstract</code> classes, the only restriction being that they cannot be <code>abstract</code>. All methods that are accessible are inherited by subclasses. If an inherited method is <code>static</code>, the only difference is that the method can be hidden instead of overridden.</p>
<p><br /></p>
<p><b>6. The correct answer is B.</b><br /> Even when the variable is cast to <code>SuperClass3</code>, Java chooses to execute the method of the subclass thanks to polymorphism. If we want to execute the method of the superclass, we have to instantiate an object from that class.</p>
<p><br /></p>
</div>
</div>
<footer></footer>
</body>
</html>