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
- Creating variables for a class
- Creating methods for a class
- Access modifiers
- Instantiating an object
- Creating a class constructor
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).
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.
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.
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:
- public - Specifies that a class, variable, or method can be accessed by any other class
- protected - Specifies that a class, variable, or method can be accessed only by classes in the same package (a package is a group of related classes and interfaces)
- private - Class: can be access only within itself, cannot be subclassed (extended) or instantiated.Variables & methods: can be accessed only by the class they are declared in
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.
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.
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.