Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
1
Chapter 13 Abstract Classes and Interfaces CS165 Colorado State - - PowerPoint PPT Presentation
Chapter 13 Abstract Classes and Interfaces CS165 Colorado State University Original slides by Daniel Liang Modified slides by Wim Bohm, Sudipto Ghosh 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
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
2
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
3
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
4
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
5
Run
GeometricObject Circle Rectangle TestGeometricObject
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
6
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
7
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
8
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
9
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
10 10
Run
LargestNumbers
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
11 11
✦ Defines what operations an object can perform. ✦ You can specify common behavior for objects using
✦ Contains only constants and abstract methods. ✦ Similar to abstract classes in many ways ✦ Operations are defined by the classes that implement the
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
12 12
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
13 13
✦ Treated like a special class in Java. ✦ Each interface is compiled into a separate bytecode file,
✦ Like an abstract class, you cannot create an instance
✦ In most cases you can use an interface more or less the
✦ For example, you can use an interface as a data type for
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
14 14
✦ The Edible interface specifies whether an object is edible. ✦ Classes Chicken and Fruit implement the Edible interface ✦ They use the implements keyword. ✦ For example, (See TestEdible).
Run
TestEdible Edible
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
15 15
public interface T1 {
public static final int K = 1; public abstract void p(); }
Equivalent
public interface T1 { int K = 1; void p(); }
✦ All data fields are public final static. ✦ All methods are public abstract. ✦ For this reason, these modifiers can be omitted. ✦ A constant defined in an interface can be
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
16 16
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
17 17
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
18 18
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
19 19
s instanceof String s instanceof Object s instanceof Comparable d instanceof java.util.Date d instanceof Object d instanceof Comparable n instanceof Integer n instanceof Object n instanceof Comparable
Run
SortComparableObjects
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
20 20
ComparableRectangle
Run
SortRectangles
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
21 21
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
22 22
Suppose that c is an instance of Class2. c is also an instance of Object, Class1, Interface1, Interface1_1, Interface1_2, Interface2_1, and Interface2_2. ✦ All classes share a single root, the Object class, but there is no single root for interfaces. ✦ Like a class, an interface also defines a type.
✦
A variable of an interface type can reference any instance of the class that implements the interface.
✦
If a class implements an interface, this interface plays the same role as a superclass. You can use an interface as a data type and cast a variable of an interface type to its subclass, and vice versa.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
23 23
✦
✦
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
24 24
✦
✦
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
25 25
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
26
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
27
✦ Follow standard Java programming style and naming
✦ Always place the data declaration before the
✦ Always provide a constructor and initialize variables
✦ Choose informative names for classes, data fields, and
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
28
✦ Each class can present two contracts
– one for the users of the class – one for the extenders of the class.
✦ Make the fields private and accessor methods public if they are
✦ Make the fields or method protected if they are intended for
✦ The contract for the extenders encompasses the contract for the
✦ The extended class may increase the visibility of an instance
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
29