Java Java intro
  1. Java intro
  2. Java basics
Java core
  1. Java variables
  2. Java conditionals
  3. Java loops
  4. Java arrays
  5. Java strings
Object-oriented Java
  1. Java OOP 1
  2. Java OOP 2
  3. Java packages
  4. Java interfaces
Java interactive
  1. Java user input
  2. Java exceptions
  3. Java events
Graphical Java
  1. Java GUI
  2. Java GUI layout
  3. Java graphics
Java extras
  1. Java applets
  2. Java sounds
  3. Java random numbers
Java wrap-up
  1. Java summary

Java object-oriented programming (part 1)

You can have simple data types that store some bit of information, but what if you wanted a much more advanced data type that represents an actual thing as opposed to just some piece of data? Where you can set attributes for it and things it does? You can do exactly this with object-oriented programming.

Java, being an object-oriented language, supports the creation of these advanced data types (objects). Objects consist of a set of variables (representing information about the object) and functions (representing what the object can do and things you can do with the object).

This tutorial focuses on:

Creating a class

Remember that an object represents an actual thing, so an object can represent a car, a table, a book or any other real world concept. But before an object can be implemented, a blueprint has to be designed for it, and that blueprint is the class of an object. A class is a blueprint for an object stating the various properties (variables) of the object as well as what it can do (methods).

Syntax for creating a class:
class className{ Specifics of the class go here }
class Book{ }

The above code creates a class named Book. We are going to use this class as a blueprint for a book.

Creating variables for a class

Class variables are known as member variables, because they store important information in regards to the class. Variables represent information about an object.

class Book{ String title; int numPages; }

The title variable - a string variable which will store the title of the book.
The numPages variable - a numeric variable which will store the number of pages in the book.

Creating methods for a class

Methods are class functions which can be used to either retrieve data or manipulate data. Class methods which are used to retrieve data are known as accessor methods. Class methods which are used to manipulate data are known as mutator methods.

Methods for our Book class:
class Book{ String title; int numPages; //sets the number of pages in the book public void setNumPages(int numOfPages){ numPages = numOfPages; } //gets the number of pages //the keyword 'return' is used to return a value public int getNumPages(){ return numPages; } //sets the title of the book public void setTitle(String theTitle){ title = theTitle; } //gets the title of the book public String getTitle(){ return title; } }

NOTE: Methods that do not return a value have the keyword void in front of them. Methods that do return a value have the keyword representing the data type of the value they return in front of them. You can see this in action in the above example.

Access modifiers

Classes, class variables, and class methods can be declared with certain keywords called access modifiers with dictate the level of access on them.

Access modifiers:

Access modifiers should be placed in front of the class, variable, or method during declaration

Instantiating an object

So you have created your class with its variables and methods, and now what to do with it? Now you can create objects! The process of creating an object is called instantiation.

nameOfClass nameOfObject = new nameOfClass();
Book YellowPages = new Book();

The above example creates an object named YellowPages which is an instance of the Book class.

Creating a class constructor

What if you want an object to have certain values when it is instantiated? For example, if you wanted an object of the Book class to have 200 pages right away when you create it? This is what class costructors are for. The method used for a class constructor takes the same name as the class for which it will be a constructor.

NOTE: A class constructor should be declared within the class that it is a constructor for.

Syntax for a class constructor:
name_Of_Class_To_Create_A_Constructor_For([parameters]){ }
class Book{ String title; int numPages; public Book(int numPages){ this.numPages = numPages; } }

In the above example, the Book class contains a constructor with one parameter. The value given to this parameter will be how many pages the book has.

Did you notice a new keyword in the above example? The keyword this is a special keyword used with classes. The purpose of it is to refer to the current class.

Now we can instantiate a new Book object and automatically set how many pages it has based on the class constructor.

Book object with 340 pages:
Book YellowPages = new Book(340);
© Copyright 2013-2014
Terms of use | Privacy policy | Copyright information