WC - Anatomy of a Class (Lesson)

APCompSci_LessonTopBanner.png

Anatomy of a Class

Introduction

View the video below:

 

Wait. Why are we singing a preschool song? It is now time to learn about the anatomy of a class. Like the anatomy of the body (head, shoulders, knees, and toes), a Java class has an anatomy of its own. Sing with me now!

Heading

Fields

Constructors

Methods

These are the parts of a Java class. Listen to the song again only this time substitute the parts of a class in for the parts of the body.

Basic Class Design

We have been eating cookies for a long time now, so it is time to learn about the cookie cutters. Remember, in Java the class is like the cookie cutter and the cookie is the object. Now let’s write a Java class.

We will write Java classes that contain the same basic information in the following order:

  • class heading
  • instance variables (also known as fields)
  • constructors
  • methods
  • toString method

Recall from our previous lessons that the instance variables are the attributes of the object. The constructors will assign values to the instance variables. The methods in a class represent the actions of the object. The toString method is simply a method which will return a String that states what the object is – what values are stored in the instance variables.

Consider the Robot class below by completing the Hotspot activity:

Notice that this class does not contain the main method. The main method will be placed in another tester or runner class. The runner class will only contain the main method and that will be where objects are created.

In the Robot class, the constructor

public Robot ()
{ } 

The toString method is a method in the Robot class. (Not every class will define its own toString method. It will be inherited from the Object class if it is not defined.) Can you guess what this one does?

Duck saying "Notice that the constructor and the class have the same names." 

Once a class has been created, a runner or tester class will be written to access the class and instantiate the objects:

Click below to begin the public class Hotspot Activity.

  

Let’s look at a Triangle Class. When you think of a triangle consider the attributes and the methods. What does a triangle look like or have (instance variables), and what can it do (methods)?

A triangle object would have a base and height (instance variables) and it can compute its area (method) using the base and height.

View the video below to see how to write a Triangle class.

 

Data encapsulation is a technique in which the implementation details of a class are kept hidden from a user. When designing a class, programmers make decisions about what data to make accessible and modifiable from an external class. Data can be either accessible or modifiable, or it can be both or neither.

The keywords public and private affect the access of classes, data, constructors, and methods. The instance variables are private which means they are accessible in the class. The keyword private restricts access to the declaring class, while the keyword public allows access from classes outside the declaring class. The instance variables are encapsulated by using the private access modifier. Accessor and mutator methods can be used to allow client code to use and modify data that is otherwise private.

Notice that the word public appears before method names. Methods can be public or private. We will keep our methods public which means they can be accessed both inside and outside of the class. Classes and constructors are also designated as public.

Book Icon Click on "Runestone Academy" below to open the required reading that is listed.

Runestone Academy: AP CSA – Java Review Links to an external site.:

READ: 2.7 – Parts of a Java Class

Book Icon Click on "Introduction to Computer Science Using Java" below to open the required reading that is listed.

Introduction to Computer Science Using Java Links to an external site.:

READ:

  • Chapter 46: Defining your own Classes
  • Chapter 47: Class Design Example: Cone
  • Chapter 50: Encapsulation and Visibility Modifiers

Complete the reviews and quiz for practice.

Practice-it Self Check

Practice Icon Practice-It! Practice

APCompSci_LessonBottomBanner.pngIMAGES CREATED BY GAVS