 Here's a UML definition for a circle class that's part of a graphics program. It has a color, a boolean that tells whether it's filled or not, and a radius. It should also have a center x and y coordinate, but to save space I've omitted those. And here's the definition for a rectangle class. It also has a color, a boolean, fulfilled, but this time it has a width and height. Look at all the fields and methods that they have in common. What if we were to add triangles, pentagons, and even more geometric objects? That would be a lot of unnecessarily repetitive code. We can avoid that repetition by using subclasses. We can create a class that contains all the common fields and methods and call it geometric object. This general class is a superclass, also called a base class or parent class. The circle and rectangle classes now contain only the items that make them circles and rectangles. They are subclasses or child classes, and they will inherit all the accessible fields and methods from their parent class. How do we implement this in Java? First, here's geometric object. It has its fields, its constructors, the getters and setters, and a two-string method. Here's the circle class, where we use the keyword extends to specify that a circle is a subclass of a geometric object. The only thing that we're adding to the fields is the radius, the thing that makes a circle a circle. The color and filled fields are inherited from the superclass geometric object. We have the constructors, the getter and setter for the radius, and the area and circumference methods, and finally our two-string method. Let's take a closer look at the constructor. It uses setColor and setFilled to access the fields in the parent class. Why didn't we use the fields directly? Since they're inherited, why didn't we just say this.color equals white and this.filled equals false? The reason we can't do that is because here in geometric object they've been declared private, and that means that nobody outside the class that declared them can access them, not even a subclass. Let's compile and you'll see what I mean, and we get errors because color and filled have private access in geometric object. That's why we had to use setColor and setFilled, because those are public in the superclass geometric object, and now it compiles successfully. That's also why we have to use getColor and isFilled in the two-string method. Let's take a look at the rectangle class. It again extends geometric object, so a rectangle is a subclass of a geometric object. We only need to specify the width and height, the fields that make a rectangle a rectangle, and we have our constructors, the getters and setters, the area and perimeter methods, and two-string. Here's a program to test these classes. We'll create a circle and we'll create a rectangle. We'll print them, which will invoke the two-string method. For the circle, we'll get its area and circumference, and for the rectangle, we'll get its area and its perimeter. Let's compile and run, and there's our output. So far so good, but there's still some duplication of code. Here in the constructor for circle, we're calling setColor white and setFilled false, but that's the same code that's handled by the noArgument constructor for geometric object. And here we're setting color to color and setting fill to filled, but that's the same code that's happening in the two-argument constructor for geometric object. Java lets us call the superclass constructor by using the keyword super. In the noArgument constructor, we'll tell Java to call the noArgument superclass constructor, and here in the multipleArgument constructor, we're going to tell Java to call the superclass constructor with the specified color and filled. When you use super, it must be the very first non-comment line in your constructor. Let's recompile that to make sure it works, recompile test, and run it, and it also works fine. We can do the same thing here in rectangle. We'll replace these two statements with a call to the noArgument superclass constructor and these two statements with a call to the multipleArgument constructor for the superclass geometric object. In summary, you can create subclasses by extending the superclass, and you can call the superclass constructor by using the super keyword.