Update – 4 April 2025. A new version about Swift can be found in What is a Class in Swift.
A class in Java can be thought of as a blueprint. If using Karel the Robot as an example, each time we “extend Karel” we are getting a copy of Karel the robot to utilise. The blueprint specifies exactly what Karel can do out of the box. In terms of Karel, the blueprint is held at Stanford and we are not able to modify him. Every person who takes the iTunes U CS106A course takes a copy of Karel each time the program runs. The reason we cannot modify the blueprint is that changes we make would likely break everyone elses programs.
So what we do is become a subclass of Karel… ie, we “extend Karel” which means that our programs make an exact copy. When I use the word subclass, this is the opposite of superclass which is what Karel is to our own implementation. This doesn’t mean that ours is on a lower level… in fact, it could be said that we have more power being a sub-class because we get a copy of Karel and can enhance him with a turnRight() private method. So, our copy of Karel can do more than the blueprint specifies.
How to Create a Karel Subclass
Creating a class of Karel is quite simple. Our code needs a few things for it to work though. The first important line is the import which lets the program know where the blueprint is stored. In the case of Karel, this is held at Stanford at the location on line 1 of the code:
[Read more…]