Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.1. Objects

4.1. Objects
Last updated: 23 January 2013.

This tutorial discusses classes, objects, nested classes and object responsibility in Java.


4.1.1. Classes and objects

Objects are representations of real-world items. They have a status given by their fields (variables) and a behaviour given by their methods. The fields can be either simple primitive types or other objects. Objects are created from a class which is their code representation. Before being able to use an object (modify its status, call its methods), it has to be created (instantiated) in memory. The Java keyword new creates a new instance of a class. For example, let's take a look at the class Aircraft shown below:

public class Aircraft {

   
float length;
   
float height;
   
int range;
   
double weight;
   
boolean airborne;


   
public boolean land(){
         
if(airborne == true){
               
airborne = false;
               
return true;
         
}
         
return false;
   
}


   
public boolean takeOff(){
         
if(airborne == false){
               
airborne = true;
               
return true;
         
}
         
return false;
   
}

}

The class Aircraft has integer fields indicating its length, height, range, weight and a boolean indicating whether it is airborne or not. The class Aircraft also has two methods returning booleans: land and takeOff. The line of code below creates a new instance of the class Aircraft:

Aircraft aircraft = new Aircraft();

Each created instance has its own copies of the instance variables and instance methods. The instance variables can be accessed via their names. For example, the variable range can be accessed this way:

aircraft.range

Likewise, instance methods are accessed via their names. The example shown below is a call to the method land:

aircraft.land();


4.1.2. Nested classes

A nested class is a class that is declared within another enclosing class. As an example, I can declare a class named Engine that contains all of the variables and methods of the aircraft's engine. Instances of nested classes can be created within their enclosing class with the keyword new. For example, an instance of the class Engine is created in the method startEngine:

public class Aircraft {

   
float length;
   
float height;
   
int range;
   
double weight;
   
boolean airborne;


   
class Engine {
         
// ...
          // The variables and methods of the aircraft's engine go here
          // ...
   
}

   
public boolean land(){
         
if(airborne == true){
               
airborne = false;
               
return true;
         
}
         
return false;
   
}


   
public void startEngine(){
         
Engine engine = new Engine();
         
// do something with engine
          // ...
   
}


   
public boolean takeOff(){
         
if(airborne == false){
               
airborne = true;
               
return true;
         
}
         
return false;
   
}

}


4.1.3. Object responsibility

Since objects are representations of real-world items, they must be designed to recreate as closely as possible the behaviours of real-world items. An aircraft that is waiting for its turn to land cannot decide when to land. The authorization must be granted by the control tour. Consequently, if you have to write a method called getLandingAuthorization whose purpose is to grant landing authorizations, it makes sense to write it within a new class named ControlTour rather than writing it within the class Aircraft.

public class ControlTour {

   
public boolean getLandingAuthorization(){

         
// the code to grant landing authorizations goes here

   
}

}

A given class should only have one set of purposes. The responsibility of an object is its set of purposes and it is good programming practice to always have a limited and coherent set of purposes in each object.


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.1. Objects
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.2. Constructors

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7