2 Creating Classes

 

After reading lesson 3, you will be able to:

  • Define a new class
  • Add fields to represent the class attributes
  • Add methods to the class to represent class behaviors
  • Create objects based on the class definition

Java is an object-oriented programming language, but in order to create objects we have to start by creating a class. A class in Java is a blueprint or template for an object. So, what does that mean? Java programs create objects that model items in the real world. These items can represent physical items such as a car or more abstract objects such as a bank account. Before we can create a class, we must identify the attribute(s) and behavior(s) of our objects. I find it helpful to think of the attributes as the adjectives used to describe the object and the behaviors are the verbs or actions that we can perform on the object. For example, a car has the following attributes:

  • Make
  • Model
  • Year
  • MPG
  • Color

And the behaviors include:

  • Move forward
  • Move backward
  • Stop (or brake)
  • Turn left/right
  • Honk horn

2.1   Defining a Class

 
 

2.2   Add Class Behaviors using Methods

 
 
 
 

2.3   Create objects based on the class definition

 
 

2.4   Summary

 
 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest