11slide Accessible
11slide Accessible
Data Structures
Twelfth Edition
Chapter 11
Inheritance and
Polymorphism
Circle
-width: double
Rectangle Circle
-radius: double
+Circle() -height: double
+Circle(radius: double) +Rectangle()
+Circle(radius: double, color: String,
filled: boolean)
+Rectangle(width: double, height: double)
+Rectangle(width: double, height: double Rectangle
+getRadius(): double color: String, filled: boolean)
+setRadius(radius: double): void +getWidth(): double
+getArea(): double +setWidth(width: double): void
+getPerimeter(): double
+getDiameter(): double
+getHeight(): double
+setHeight(height: double): void TestCircleRectangle
+printCircle(): void +getArea(): double
+getPerimeter(): double
class B { class B {
public void p(double i) { public void p(double i) {
System.out.println(i * 2); System.out.println(i * 2);
} }
} }
PolymorphismDemo
DynamicBindingDemo
Copyright © 2020 Pearson Education, Inc. All Rights Reserved
Polymorphism, Dynamic Binding and
Generic Programming (2 of 2)
An object of a subtype can be used wherever its supertype
value is required. This feature is known as polymorphism.
Cn Cn-1 ..... C2 C1
Student b = o;
A compile error would occur. Why does the statement Object o = new
Student() work and the statement Student b = o doesn’t? This is
because a Student object is always an instance of Object, but an 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 know it. To
tell the compiler that o is a Student object, use an explicit 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:
Apple x = (Apple)fruit;
Orange x = (Orange)fruit;
CastingDemo
java.util.ArrayList<E>
+ArrayList() Creates an empty list.
+add(o: E) : void Appends a new element o at the end of this list.
+add(index: int, o: E) : void Adds a new element o at the specified index in this list.
+clear(): void Removes all the elements from this list.
+contains(o: Object): boolean Returns true if this list contains the element o.
+get(index: int) : E Returns the element from this list at the specified index.
+indexOf(o: Object) : int Returns the index of the first matching element in this list.
+isEmpty(): boolean Returns true if this list contains no elements.
+lastIndexOf(o: Object) : int Returns the index of the last matching element in this list.
+remove(o: Object): boolean Removes the element o from this list.
+size(): int Returns the number of elements in this list.
+remove(index: int) : boolean Removes the element at the specified index.
+set(index: int, o: E) : E Sets the element at the specified index.
TestArrayList
list.add("London");
Inserting a new element Blank
list.add(index,
"London");
Removing an element Blank
list.remove(index);
Removing an element Blank list.remove(Object);
list.clear();
DistinctNumbers
Copyright © 2020 Pearson Education, Inc. All Rights Reserved
Array Lists from/to Arrays
Creating an ArrayList from an array of objects:
System.out.pritnln(java.util.Collections.max(
new ArrayList<String>(Arrays.asList(array)));
System.out.pritnln(java.util.Collections.min(
new ArrayList<String>(Arrays.asList(array)));
MyStack
MyStack
-list: ArrayList A list to store elements.
+isEmpty(): boolean Returns true if this stack is empty.
+getSize(): int Returns the number of elements in this stack.
+peek(): Object Returns the top element in this stack.
+pop(): Object Returns and removes the top element in this stack.
+push(o: Object): void Adds a new element to the top of this stack.
+search(o: Object): int Returns the position of the first element in the stack from
the top that matches the specified element.
Visibility increases
ü
sign
üsign
üsign
protected
üsign
ü
sign
ü
sign Blank
default
ü ü
sign sign Blank Blank
private
ü
sign Blank Blank Blank
package p1;
public class C1 { public class C2 {
public int x; C1 o = new C1();
protected int y; can access o.x;
int z; can access o.y;
private int u; can access o.z;
cannot access o.u;
protected void m() {
} can invoke o.m();
} }
package p2;