Chapter 12 Flashcards
Abstract classes
-Classes that are not intended for creating objects but serve only as superclasses
-they are restricted and cannot be used to create objects
-may contain abstract methods
When to use abstract classes?
-you want to share code amont several closely related classes
-You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
-You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
Abstract method
-A method declared w/o an implementation (without braces, and followed by a semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);
-the definition consists of a method header w/o a method body. It is marked with the keyword abstract.
T/F: An abstract method can be executed
F- it has not body, so can never be executed
What purposes do declaring a class abstract serve?
- No instances can be created of abstract classes
- Only abstract classes can have abstract methods
- Abstract classes with abstract methods force subclasses to override and implement those methods declared abstract
What happens if a subclass does not provide an implementation for an inherited abstract method?
-it is itself abstract and no instances may be created- for it to be concrete it must provide implementations for all inherited abstract methods.
Java Interface
a specification of a type (in the form of a type name and a set of methods). It often does not provide an implementation for most of its methods.
What are the most significant features of interfaces?
- keyword interface is used instead of class in the header of the declaration.
- No constructors
- No instance fields
- Only fields that are constant class fields (static and final) with public visibility are allowed in an interface. The public, static, and final keywords may be omitted, therefore; they are assumed automatically.
- Abstract methods do not have to include the keyword abstract in their header.
A class is said to implement an interface if it includes an ___ in its class header
-implements clause
ie.
public class Fox extends Animal implements Drawable
{
}
Default methods in interfaces
-Methods that can have a body
- provides additional functionality to a given type without breaking down the implementing classes.
-it should be clear that the functionality possible in a default method is strictly limited, since there is no state that can be examined or manipulated directly by them.
2 Main purposes of using inheritance
- Inherit code (code inheritance)- useful for code reuse
- Inherit type (subtyping)- for polymorphism and specialization
Describe what we inherit from concrete classes, interfaces and abstract classes
- inherit from concrete- both implementation and type
- inherit (implement) from interfaces- we separate the 2, inherit the type but usually no implementation
- inherit from abstract- inherit type and partial implementation
Java allows any class to extend at most __ other class/es
one
-however, it allows a class to implement any number of interfaces (in addition to possibly extending one class)
How would we define class Hunter to implement Actor and Drawable as interfaces
public class Hunter implements Actor, Drawable
{
}
-it inherits the methods of the interfaces as abstract methods
-now it must provide method definitions for both of them by overriding the methods, or the class itself must be declared abstract.
T/F: Where a class implements multiple interfaces and two or more of the interfaces have a default method with the same signature then the implementing class must override that method—even if the alternative versions of the method are identical.
T