Accompanying code files:
- Lab 1 - Elevens (Activity 2)
Jump to Homework
- a superclass that represents an abstract concept, and so should not be instantiated
- e.g. a platform game like centipede, has different characters such as the player, the centipede, spiders, fleas, and scorpions.
- these all share certain features (e.g. location, the way they are displayed etc.), and can thus be declared as subclasses of an abstract class, called Sprite
- instances of the player, centipede, spiders, fleas, and scorpions will be created by the game, but there will be no instances of Sprite
- an abstract class may contain abstract methods, which are methods with no implementation code, just a header
- the reasoning behind having abstract methods is that there is no good default code for them
- this means that every subclass will have to override the abstract methods
- thus, the abstract methods are placeholders
- any class that contains any abstract methods MUST be declared an abstract class
public abstract class AbstractClass { // use the abstract keyword to declare a class abstract
// data fields
// abstract methods with varied implementation for each subclass
public abstract int intMethod();
public abstract String stringMethod();
// method that should definitely be in each subclass with the same implementation
public void mutator() {
// implementation code
}
}
public class SubClass extends AbstractClass {
// data fields
// abstract methods replaced
public int intMethod() {
// implementation code, notice no use of super
}
public String stringMethod() {
// implementation code, notice no use of super
}
public void mutator() {
super.mutator(); // not an abstract method
}
}
- a collection of related methods, either abstract (headers only), or default (implementation provided in the interface). NOTE: default methods are new in Java 8 and will not be tested on the AP exam
- non-default methods include abstract and public methods i.e. no need to explicitly include these keywords
- therefore, they provide a framework of behaviour for any class
- the classes that implement the same interface may represent completely different objects
- e.g. some classes might implement a FlyingObject interface, with the methods
fly
andisFlying
, such as Bird, Airplane, Missile, Bee, and Witch, but they will all have completely different implementations of it - a class that implements an interface can define any number of methods, and should have implementations for all non-default (abstract) methods in the interface, UNLESS it is an abstract class
- a class that extends a superclass can also directly implement an interface
- a class can have one direct superclass, but it can implement any number of interfaces
- as with abstract classes, no instances can be created for an interface
public interface FlyingObject {
void fly(); // method that simulates flight of an object
boolean isFlying(); // true if object is in flight, false otherwise
}
public class Bird implements FlyingObject {
}
public class Owl extends Bird implements FlyingObject { // extends must be BEFORE implements
}
public class Owl extends Bird implements FlyingObject, Carnivore {
}
- use an abstract class for an object that is application specific but incomplete without its subclasses
- consider using an interface when its methods are suitable for the program but could be equally applicable in other programs
- an interface doesn't typically have implementations, but and abstract class does
- an interface cannot contain instance variables, but an abstract class can
- it is not possible to create an instance of either an interface object or abstract class object
Assignments:
- all odd-numbered questions for the Classes and Objects MCQ handout I gave you (I will be marking these as assessment)
- (optional) the rest of the questions for the Classes and Objects handout (just for extra practice, I will post the answers in this issue)
Prep for Next Class:
- N/A