Home‎ > ‎ICT Programming‎ > ‎Java‎ > ‎

Classes

What Is a Class?

A class is a blueprint or prototype from which objects are created. This section defines a class that models the state and behavior of a real-world object. It intentionally focuses on the basics, showing how even a simple class can cleanly model state and behavior.

Review this material: Java Tutorials - Classes and Objects

Defining objects and classes


A class declaration typically contains a set of attributes (sometimes called instance vari- ables) and functions (called methods in Java). So far a Java class declaration is very simi- lar to one in Simula. Attributes are declared almost as usual:

class Turtle {
private boolean penDown; protected int x, y;

          // Declare some more stuff
      }

The private and protected keywords require some explanation. The private declara- tion means that those attributes cannot be accessed outside of the class. In general, attributes should be kept private to prevent other classes from accessing them directly.

There are two other related keywords: public and protected. The public keyword is used to declare that something can be accessed from other classes. The protected key- word specifies that something can be accessed from within the class and all its subclasses, but not from the outside. 


Comments