Skip to content

Classes

trekitch edited this page Apr 3, 2017 · 13 revisions

A class is basically a blueprint of any Java program. It contains all of the elements to make a program work. A class has attributes as well. Attributes are characteristics that define an object. Attributes are the properties of the object.

What is an object?

An object is a concrete instance of a class. When you create and instance of a class this is called instantiation. You have done this before when you create an instance of a variable. Think of dogs as an example. Let's say you have a dog class. This class is the blueprint for other dogs. This class has attributes such as name, size, age, owners. These attributes will be the same for each object of the class but they may be different between each object. For example, one dog may have the following attributes:

  • Name: Spot
  • Age: 1
  • Size: 4 pounds
  • Owner: Tom

Another dog may have the following attributes.

  • Name: Dolly
  • Age: 3
  • Size: 15 pounds
  • Owner: Mary

These are two completely different objects but the have the same attributes can come from the same class. A class can have an unlimited number of objects.

Creating a Class

When you create a class it has three main parts. It has an access specifier (optional), the keyword class, and a name for your class. An access modifier is how the class will be access in the program. The most common access modifier is public. Public means that any object in the program can access the class. Public classes can also be extended. Extended classes can be used as the basis for other classes. There is also a private access modifier. A private access modifier makes it so that no other methods in other classes can access the class. Below is an example of how you may set up an employee class.

public class Employee {
 private int empNum;
}

test

Test.java

Clone this wiki locally