Object-Orientation - Abstract Classes (in Java)
Abstraction can provide stumbling blocks for people of strange intelligence.
–Gustave Flaubert, in his unfinished satirical work, Bouvard et Pécuchet
Overview
Concept
An abstract class is a Java class that has been declared as abstract.
public abstract class Foo {
/* some stuff here */
}
Similarity to concrete classes
Code-wise, an abstract class can encapsulate all the same properties and methods as a concrete class.
-
private, protected, or public properties and methods
-
static or non-static properties and methods
-
constants and non-constants
-
etc.
Difference from concrete classes
The only two differences between an abstract class and a concrete class are…
- abstract classes can contain abstract methods - i.e. method signatures without their corresponding implementation.
public abstract class Foo {
pubic abstract int getRandom(int lowerBound, int upperBound);
}
- abstract classes can never be instantiated
Foo f = new Foo(); // a good view of a compilation error!
Legacy
Since abstract classes can contain abstract methods and because an abstract class cannot be instantiated…
- any non-abstract child class must implement any abstract methods declared in an abstract parent or other ancestor class.
public abstract class Foo {
// an abstract method, for example
pubic abstract int getRandom(int lowerBound, int upperBound);
}
public class Bar extends Foo {
// this class *must* contain a concrete implementation of the abstract method
public int getRandom(int lowerBound, int upperBound) {
return rand = ( (int) Math.random() * (upperBound - lowerBound) ) + lowerBound;
}
}
Frequently asked questions
There are three questions that commonly arise in relation to abstract classes:
-
Why can you not instantiate an abstract class?
-
When would you use an abstract class over an interface, and vice-versa?
Cannot Instantiate
Justification
An abstract class cannot be instantiated because any object made from it would potentially be missing the implementation of one or more methods.
- For example, given the following abstract class…
public abstract class Foo {
// an abstract method, for example
pubic abstract int getRandom(int lowerBound, int upperBound);
}
- Imagine instantiating and calling this method… what would happen?
Foo f = new Foo();
int num = f.getRandom(1, 10); // not allowed - this method has no concrete implementation!
Picking an Abstraction
Deciding between abstract classes and interfaces
Given both abstract classes and interfaces have the ability to encapsulate abstract methods, how do you pick between the two?
-
It’s easy:
-
Interfaces are used to enforce a common set of behavioral capabilities on otherwise-disparate classes with very little code in common.
-
Abstract classes are used to enforce a common set of behavioral capabilities on classes that share a significant amount of code in common.
Deciding between abstract and concrete classes
Given that abstract classes can share all the same sorts of code as a concrete class, and more, how do you pick between the two?
-
It’s easy:
-
Use an abstract class if it makes no conceptual sense for the class to ever be instantiated.
-
Use a concrete class if it does make sense.
-
For example, dogs and cats share a lot of internals in common - they are both mammals. Imagine writing code representing them… you might create a Mammal class that encapsulates what Dog and Cat have in common and have Mammal be inherited by each.
-
Would it make sense to be able to instantiate a Mammal object? Or might it be better to create an abstract Mammal class that contains a lot of code shared in common by two concrete child classes, Dog and Cat.
Mixing Abstractions
Choices
When dealing with inheritance, polymorphism, interfaces, and abstract classes, we often study them in isolation. However, in reality, a single class might take advantage of all of these.
Imagine, for example, the following scenarios:
-
a single class can use class-based inheritance (whether from an abstract class or a concrete class) as well as implement an interface
-
a single class might implement multiple interfaces
-
a single class might implement an interface that inherits from another interface
-
a single class might inherit from an abstract class that implements multiple interfaces
Conclusions
Abstract classes can be useful, when implemented with tact. You now have an understanding of the various ways in which this might be the case.
- Thank you. Bye.