Java classes and objects (part 1)
Java is an object-oriented language. Other object-oriented languages include PHP, Javascript, and C++. An object-oriented language is a language which supports the creation of data types which go beyond the simple primitive data types. These particular data types are called objects. These particular data types have much more power and functionality than primitive data types. An object in the context of object-oriented programming is a representation of a real world thing.
This lesson focuses on:
- Creating a class
- Creating variables for a class
- Creating methods for a class
- Instantiating a class
- Creating a class constructor
Creating a class
A class is a blueprint for an object. As stated above in the introduction to this lesson, an object is a representation of a real world thing. An object can represent a car, a table, a book or any other real world concept. Before an object can be implemented, however, 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{
}
A class is created with the class keyword, followed by a single space, the name of the class to be created, an opening curly brace, and a closing curly brace. All the specifics and logistics of the class go in between these curly braces.
Example of a class:
class Book{
}
The above code creates a class named Book.
Classes can be declared with certain keywords called access modifiers which dictate the level of access on a class.
Class access modifiers:-
public
The keyword public when placed in front of a class name specifies that a class can be accessed by any other class.
Example:
public class AClass{ } -
protected
The keyword protected when placed in front of a class name specifies that a class can only be accessed within the same package.
Example:
protected class AClass{ } -
private
The keyword private when placed in front of a class name specifies that a class can be accessed only within itself. It cannot be subclassed (extended) or instantiated.
Example:
private class AClass{ }
Creating variables for a class
Class variables are known as member variables, because they store important information in regards to the class.
Example:
class Book{
String title;
int numPages;
}
The above code uses the Book class and creates two member variables for it. The first is title - a string variable which will store the title of the book. The second is numPages - a numeric variable which will store the number of pages in the book.
Variables can be declared with certain keywords called access modifiers which dictate the level of access on a variable.
Variable access modifiers:-
public
The keyword public when placed in front of a variable specifies that the variable can be accessed directly by any other class.
Example:
public int aNumber;
-
protected
The keyword protected when placed in front of a variable specifies that the variable can be accessed by the class it is defined in, a subclass of the class it is defined in, or from classes within the same package.
NOTE: A package is a collection of related classes.
Example:
protected int aNumber;
-
private
The keyword private when placed in front of a variable specifies that the variable can be accessed only by the class it is declared in.
Example:
private int aNumber;
Creating methods for a class
Class methods are defined inside a class definition, they 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.
Example:
class Book{
String title;
int numPages;
//this is a mutator method
public void setNumPages(int numOfPages){
numPages = numOfPages;
}
//this is an accessor method
public int getNumPages(){
return numPages;
}
//this is a mutator method
public void setTitle(String theTitle){
title = theTitle;
}
//this is an accessor method
public String getTitle(){
return title;
}
}
In the above example, four methods are created:
-
setNumPages(int numOfPages)
This method is used to set the number of pages in the book, this is done through its parameter numofPages. The method sets the variable numPages to whatever value you supply to it through its parameter numOfPages.
-
getNumPages()
This method is used to return the number of pages in the book, this is done through the use of the keyword return followed by the variable numPages - the variable that stores the value of how many pages there are in the book.
-
setTitle(String theTitle)
This method is used to set the title of the book, this is done through its parameter theTitle. The method sets the variable title to whatever value you supply to it through its parameter theTitle.
-
getTitle()
This method is used to return the title of the book, this is done through the use of the keyword return followed by the variable title - the variable that stores the title of the book.
Methods can be declared with certain keywords called access modifiers which dictate the level of access on a method.
Method access modifiers:
-
public
The keyword public when placed in front of a method specifies that the method can be accessed directly by any other class.
Example:
public void printNumber(){ } -
protected
The keyword protected when placed in front of a method specifies that the method can be accessed by the class it is defined in, a subclass of the class it is defined in, or from classes within the same package.
Example:
protected void printNumber(){ } -
private
The keyword private when placed in front of a method specifies that the method can be accessed only by the class it is declared in.
Example:
private void printNumber(){ }
Another keyword used with methods is the void keyword. The void keyword is used to specify that a method will not return a value. To specify that a method does return a value, instead of the void keyword, the appropriate keyword corresponding to the data type that the method will return should be used. For example, a method that will return an integer should have the keyword int in front of it. Generally, mutator methods do not return a value, and accessor methods do return a value.
The example from above contains two methods that do not return a value and two methods that do return a value.
The example from above reiterated:
class Book{
String title;
int numPages;
//does not return a value
//use the keyword void
public void setNumPages(int numOfPages){
numPages = numOfPages;
}
//returns an int value
//use the keyword int
public int getNumPages(){
return numPages;
}
//does not return a value
//use the keyword void
public void setTitle(String theTitle){
title = theTitle;
}
//returns a String value
//use the name of the class (String)
public String getTitle(){
return title;
}
}
Instantiating a class
So you have created your class with its variables and methods, and now what to do with it? Now you can create objects based on how you designed your class. The process of creating an object is called instantiation.
Syntax:
nameOfClass nameOfObject = new nameOfClass();
Example:
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 automatically have a certain value when it is instantiated? For example, if you wanted all instances of the Book class to automatically have 200 pages? This is what class costructors are for. Class constructors are used to set objects to have certain values when they are initialized. The method used for class constructors 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]){
}
Example:
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 when creating classes. The purpose of it is to refer to the current class.




