An abstract class is a way of grouping characteristics that other classes share, but that cannot in themselves represet an actual object, i.e. they cannot be instantiated.
An analogy of this might be an Animal. There are lots of different types of animals out there and they share some basic similarities, e.g. they have a height, weight, etc., but there is no such thing as a generic animal. You would have to decide exactly which type of animal you want (cheetah, gorilla, duck, etc.) before you could define its precise characteristics. The Animal class is therefore abstract and could be defined as follows:
public abstract class Animal { ... }
An abstract class can contain abstract methods. Abstract methods have no code, just the method signature. You make an abstract method when there is no sensible default code that all the subclasses could use. Continuing with the above analogy, the Animal class might have the following abstract method:
public abstract void communicate();
A concrete class (not abstract) can extend an abstract class ...
public class Dog extends Animal { ... }
... in which case it must implement a concrete version of the communicate method:
public void communicate() {
if (isFrightened) {
bark();
}
else {
wagTail();
}
}
Things to find out before you finish this module:
Cities
Farm