Ch 10 Object-Oriented Programming: Polymorphism and Interfaces
differences of abstract and concrete classes
- Abstract superclasses are too general to create real objects, they specify only what is common among subclasses. - Concrete classes provide the specifics that make it reasonable to instantiate objects
three ways to assign superclass ans subclass references
- Assigning a superclass reference to a superclass variable - Assigning a subclass reference to a subclass variable - Assigning a subclass reference to a superclass variable
concrete classes
- Classes that can be used to instantiate objects - Such classes provide implementations of every method they declare (some of the implementations can be inherited).
common interface of the Java API
- Comparable - serialization - Runnable - GUI event-listener interfaces - AutoCloseable
Polymorphism
- Enables you to "program in the general" rather than "program in the specific." - enables you to write programs that process objects that share the same superclass as if they were all objects of the superclass; this can simplify programming.
extensible design and systems
- New classes can be added with little or no modification to the general portions of the program, as long as the new classes are part of the inheritance hierarchy that the program processes generically
Abstract classes
- Sometimes it's useful to declare classes for which you never intend to create objects. - Cannot be used to instantiate objects, they are incomplete - provides a superclass from which other classes can inherit and thus share a common design
final method
- a superclass cannot be overridden in a subclass.
Interfaces
- are particularly useful for assigning common functionality to possibly unrelated classes - Allows objects of unrelated classes to be processed polymorphically, objects of classes that implement the same interface can respond to all of the interface method calls
static binding
- calls to final methods are resolved at compile time - declaration can never change, so all subclasses use the same method implementation
variable
- can be used to invoke only those methods that are members of that variable's type, which the compiler verifies.
final class
- cannot be extended to create a subclass - class where all methods are implicitly final
declaring a method in an interface
- choose a method name that describes the method's purpose in a general manner, because the method may be implemented by many unrelated classes
Comparable
- common interface of java API used to allow objects of a class that implements the interference to be compared to one another - commonly used for ordering objects in a collection such as an array
Runnable
- common interfaces of the Java API - Implemented by any class that represents a task to perform - Contains one method, run, with specifies the behavior of an object when executed
AutoCloseable
- common interfaces of the java API - Implemented by classes that can be used with the try-with-resources statement to help prevent resource leaks
GUI event-listener interfaces
- common interfaces of the java API - specifies one or more methods that must be implemented to respond to user interactions
Serializable
- common interfaces of the java API - used to identify classes whose object can be written to or read from some type of storage (file on disk, database field, etc.) or transmitted from a network.
abstract class
- declare common attributes and behaviors (both abstract and concrete) of the various classes in a hierarchy - typically contains one or more abstract methods that subclasses must override if they are to be concrete.
Runnable
- functional interface to define a task that may be run in parallel with other parts of your program
Comparator
- functional interfaces to define a method that can compare two objects of a given type to determine whether the first object is less than, equal to or greater than the second.
ActionListener
- functional interfaces to define a method that's called when the user clicks a button
Interfaces
- offer a capability requiring that unrelated classes implement a set of common methods - define and standardize the ways in which things such as people and systems can interact with one another
interfaces
- often used when disparate classes (i.e., unrelated classes) need to share common methods and constants - Allows objects of unrelated classes to be processed polymorphically by responding to the same method calls
realization
- used for UML to express relationship between a class and an interface - A class is said to "realize," or implement, the methods of an interface - modeled as a dashed arrow with a hollow arrowhead pointing from the implementing class to the interface
abstract superclasses
Another reference for abstract classes, Used only as superclasses in inheritance hierarchies
functional interface
As of Java SE 8, any interface containing only one abstract method
dynamic binding
At execution time, the type of the object to which the variable refers determines the actual method to use
public default methods
Java SE 8 methods with concrete default implementations that specify how operations are performed when an implementing class does not override the methods
dynamic or late binding
Java decides which class's toString method to call at execution time rather than at compile time
objects of the interface type
Objects of any subclasses of a class that implements an interface
is-a relationship
Once a class implements an interface and all objects of the class are guaranteed to provide the functionality described by the interface
is-a relationship
applies only up the hierarchy from a subclass to its direct (and indirect) superclasses, and not down the hierarchy
interface declaration
begins with the keyword interface and contains only constants and abstract methods - all interface members must be public
superclass reference
can be used to invoke only methods of the superclass
superclass object
cannot be treated as a subclass object - it is not an object of any of its subclasses
default methods
declare common method implementations in interfaces, which gives you more flexibility in designing your classes
downcasting
enables a program to invoke subclass methods that are not in the superclass.
compilation error
error in attempting to invoke a subclass-only method directly on a superclass reference
compilation error
error made attempting to instantiate an object of an abstract class
compilation error
error made when assigning a superclass variable to a subclass variable
compilation error
error made when failure to implement a superclass's abstract methods in a subclass, unless subclass is also declared abstract
compilation error
error resulted in failing to implement any method of an interface in a concrete class that implements the interface - must be declared abstract
compilation error
error where attempting to declare a subclass of a final class
abstract method
is an instance method with keyword abstract in its declaration, as in public abstract void draw(); // abstract method
interface
prior to Java SE 8, was typically used (rather than an abstract class) when there were no implementation details to inherit
getClass method
returns an object Class (from package java.lang), which contains information about the object's type, including its class name
getName
returns the object's class name
ClassCastException
when downcasting a reference, occurs if the referenced object at execution time does not have an is-a relationship with the type specified in the cast operator