Java object-oriented programming (part 2)
Java supports the creation of advanced data types called objects.
If you are not familiar with the basics of using classes and objects, it is strongly recommended that you read Java obect-oriented programming part 1 first.
This tutorial focuses on:
- Using class variables
- Using class methods
Using class variables
You can use the variables of a class with an object by referring to them by name together with the object name.
NOTE: You can refer directly to a class variable like in the above example only if the variable is set as public! You can still access class variables even if they are not set as public through encapsulation (covered further down on this page).
Using the methods of a class with an object
Just as with the variables of a class, you can use the methods of a class with an object by referring to them by name together with the object name.
NOTE: Because getTitle() and getNumPages() are used to return values, you can use them as values themselves. This is why they are used inside of the System.out.println() statements.
Inheritance is the process by which a class gets the properties and methods of another class. The idea behind inheritance is that it is not absolutely necessary to always build a class from scratch. Instead, you can take an existing class, add a few new features to it, and have a new class based on the already existing class. Inheritance is achieved through the use of the extends keyword. Recall the Book class with all its variables and methods from the first part of this tutorial.
This class is designed for objects which represent books, but what if you wanted to have a class whose objects represent soft cover books only? This is where inheritance comes into the picture. Instead of creating a whole new class for soft cover books, we will create a class for soft cover books which extends the Book class.
Now the SoftCoverBook class inherits all the variables and methods of the Book class, and they do not have to be declared again. Rather, they are automatically part of the SoftCoverBook class.
A class that extends another class is said to be its subclass and a class that is extended is said to be the superclass of that class. In the above example, SoftCoverBook is a subclass and Book is its superclass.
NOTE: All classes in Java inheirt from the class Object whether this is explicitly declared or not. The Object class is the base class in Java.
Polymorphism (meaning many forms) means that an instance of a class is also an instance of its superclass. For example, if you create an instance of the SoftCoverBook class, it is automatically an instance of the Book class because the SoftCoverBook class is a subclass of the Book class. Furthermore, since all classes in Java inherit from the Object class, every instance of the SoftCoverBook class as well as the Book class are automatically instances of the Object class.
Encapsulation basically means data hiding. Through the use of encapsulation, you can declare class variables as private so that objects of the class (as well as other classes) cannot access them directly. You can then declare public methods within the same class that access these variables, and then objects of the class (as well as other classes) can access the private class variables by proxy through these public methods.
As you can see, an error is generated when trying to access a private variable directly. But if we try to access it through a public method, it will work.