Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
1 rights reserved.
2
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
7 rights reserved.
8
Constructor Chaining animation
animation animation
Trace Execution Trace Execution
public class Faculty extends Employee { public class Faculty extends Employee {
public static void main(String[] args) { public static void main(String[] args) {
new Faculty(); 2. Invoke Faculty new Faculty();
} constructor }
animation animation
Trace Execution Trace Execution
public class Faculty extends Employee { public class Faculty extends Employee {
public static void main(String[] args) { public static void main(String[] args) {
new Faculty(); new Faculty();
} }
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
19 rights reserved.
20
Calling Superclass Methods Overriding Methods in the Superclass
You could rewrite the printCircle() method in the Circle class as A subclass inherits methods from a superclass. Sometimes it is
follows: necessary for the subclass to modify the implementation of a method
defined in the superclass. This is referred to as method overriding.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
21 rights reserved.
22
NOTE NOTE
An instance method can be overridden only Like an instance method, a static method
if it is accessible. Thus a private method can be inherited. However, a static method
cannot be overridden, because it is not cannot be overridden. If a static method
accessible outside its own class. If a method defined in the superclass is redefined in a
defined in a subclass is private in its subclass, the method defined in the
superclass, the two methods are completely superclass is hidden.
unrelated.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
23 rights reserved.
24
Overriding vs. Overloading The Object Class and Its Methods
public class Test { public class T est {
publ ic stat ic void main( String[ ] args) {
A a = new A();
a. p(10);
publi c stati c void main(St ring[] args) {
A a = new A();
a.p (10);
Every class in Java is descended from the
}
a. p(10.0) ;
}
a.p (10.0);
java.lang.Object class. If no inheritance is
} }
}
Sy stem.ou t.print ln(i * 2);
}
Sys tem.out .printl n(i * 2 ); superclass of the class is Object.
} }
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
25 rights reserved.
26
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
27 rights reserved.
28
Polymorphism, Dynamic Binding and Generic Programming
public class PolymorphismDemo {
Dynamic Binding
public static void main(String[] args) {
m(new GraduateStudent()); Method m takes a parameter Dynamic binding works as follows: Suppose an object o is an
m(new Student()); of the Object type. You can
m(new Person());
m(new Object()); invoke it with any object.
instance of classes C1, C2, ..., Cn-1, and Cn, where C1 is a subclass
}
of C2, C2 is a subclass of C3, ..., and Cn-1 is a subclass of Cn. That
public static void m(Object x) { An object of a subtype can be used wherever its is, Cn is the most general class, and C1 is the most specific class.
System.out.println(x.toString());
supertype value is required. This feature is
}
}
known as polymorphism.
In Java, Cn is the Object class. If o invokes a method p, the JVM
class GraduateStudent extends Student { searches the implementation for the method p in C1, C2, ..., Cn-1
and Cn, in this order, until it is found. Once an implementation is
}
class Student extends Person { When the method m(Object x) is executed, the
public String toString() {
return "Student"; argument x’s toString method is invoked. x found, the search stops and the first-found implementation is
}
} may be an instance of GraduateStudent, invoked.
Student, Person, or Object. Classes
class Person extends Object {
public String toString() {
GraduateStudent, Student, Person, and Object
return "Person"; have their own implementation of the toString
}
}
method. Which implementation is used will be Cn Cn-1 ..... C2 C1
determined dynamically by the Java Virtual
Machine at runtime. This capability is known Since o is an instance of C1, o is also an
DynamicBindingDemo Run as dynamic binding. Object instance of C2, C3, …, Cn-1, and Cn
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
29 rights reserved.
30
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
31 rights reserved.
32
Casting Objects Why Casting Is Necessary?
You have already used the casting operator to convert variables of Suppose you want to assign the object reference o to a variable of the
one primitive type to another. Casting can also be used to convert an Student type using the following statement:
object of one class type to another within an inheritance hierarchy. In
Student b = o;
the preceding section, the statement
m(new Student());
A compile error would occur. Why does the statement Object o =
new Student() work and the statement Student b = o doesn’t? This is
assigns the object new Student() to a parameter of the Object type. because a Student object is always an instance of Object, but an
This statement is equivalent to: Object is not necessarily an instance of Student. Even though you can
see that o is really a Student object, the compiler is not so clever to
Object o = new Student(); // Implicit casting know it. To tell the compiler that o is a Student object, use an explicit
m(o); casting. The syntax is similar to the one used for casting among
primitive data types. Enclose the target object type in parentheses and
place it before the object to be cast, as follows:
The statement Object o = new Student(), known as
implicit casting, is legal because an instance of
Student is automatically an instance of Object. Student b = (Student)o; // Explicit casting
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
33 rights reserved.
34
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
35 rights reserved.
36
TIP Example: Demonstrating
To help understand casting, you may also Polymorphism and Casting
consider the analogy of fruit, apple, and
orange with the Fruit class as the superclass This example creates two geometric objects: a
for Apple and Orange. An apple is a fruit, so circle, and a rectangle, invokes the
you can always safely assign an instance of displayGeometricObject method to display the
Apple to a variable for Fruit. However, a objects. The displayGeometricObject displays
fruit is not necessarily an apple, so you have the area and diameter if the object is a circle, and
to use explicit casting to assign an instance displays area if the object is a rectangle.
of Fruit to a variable of Apple.
CastingDemo Run
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
37 rights reserved.
38
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
41 rights reserved.
42
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
43 rights reserved.
44
max and min in an Array List Shuffling an Array List
String[] array = {"red", "green", "blue"}; Integer[] array = {3, 5, 95, 4, 15, 34, 3, 6, 5};
System.out.println(java.util.Collections.max( ArrayList<Integer> list = new
new ArrayList<String>(Arrays.asList(array))); ArrayList<>(Arrays.asList(array));
java.util.Collections.shuffle(list);
System.out.println(list);
String[] array = {"red", "green", "blue"};
System.out.println(java.util.Collections.min(
new ArrayList<String>(Arrays.asList(array)));
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
45 rights reserved.
46
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
47 rights reserved.
48
Visibility Modifiers A Subclass Cannot Weaken the Accessibility
package p1;
public class C1 { public class C2 { A subclass may override a protected
public int x; C1 o = new C1();
protected int y;
int z;
can access o.x;
can access o.y; method in its superclass and change its
private int u; can access o.z;
package p2;
method defined in the superclass. For
public class C3
extends C1 {
can access x;
public class C4
extends C1 {
can access x;
public class C5 {
C1 o = new C1(); example, if a method is defined as public
can access o.x;
can access y;
can access z;
cannot access u;
can access y;
cannot access z;
cannot access o.y;
cannot access o.z; in the superclass, it must be defined as
cannot access u; cannot access o.u;
can invoke m(); can invoke m(); cannot invoke o.m();
public in the subclass.
} } }
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
49 rights reserved.
50
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
51 rights reserved.
52