Java classes and objects (part 2)
This is part 2 of the lesson on classes and objects.
If you are not familiar with the basics of using classes and objects, it is strongly recommended that you read part 1 of this lesson first!
Click here to see part 1 of this lesson
This lesson focuses on:
- Referencing the variables of a class with an object
- Using the methods of a class with an object
- Inheritance
- Polymorphism
- Encapsulation
Referencing the variables of a class with an object
You can reference the variables of a class with an object by referring to them by name.
Syntax:
objectName.variableName;
Example:
class AClass{
public static void main(String[] args){
//create a new instance of the Book class
//name ABook
Book ABook = new Book();
//set the numPages variable of the ABook object
//to 200
ABook.numPages = 200;
//print the value of the numPages variable
System.out.println("Number of pages in the book: "
+ ABook.numPages);
}
}
Output:
Number of pages in the book: 200
In the above example, an instance of the Book class named ABook is created. The numPages variable of the ABook object is set to 200, and then the value of this variable is printed.
NOTE: You can refer directly to a class variable as in the above example, only if the variable is set as public! However, you can still access class variables even if they are not set as public. How to do this is covered later in this lesson with the topic of "encapsulation"
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.
Syntax:
objectName.methodName([parameters])
Example:
class AClass{
public static void main(String[] args){
//create a new instance of the Book class
//name ABook
Book ABook = new Book();
//set the number of pages in the book
//to 300
ABook.setNumPages(300);
//set the title of the book to "Read Me"
ABook.setTitle("Read Me");
//print the title of the book
System.out.println("Book title: " + ABook.getTitle());
//print the number of pages in the book
System.out.println("Number of pages in the book: "
+ ABook.getNumPages());
}
}
Output:
Book title: Read Me
Number of pages in the book: 300
In the above example, an instance of the Book class named ABook is created. The setNumPages() method is called and it is used to set the number of pages in the book to 300. The setTitle() method is then called and it is used to set the title of the book to "Read Me". The getTitle() method is then used to return the title of the book and this information is printed. The getNumPages() method is then used to return the number of pages in the book and this information is also printed.
The setNumPages() and setTitle() methods have a return type of void so they do not return any value, and instead they are used to perform some actions. The getTitle() and getNumPages() methods are used to return values (getTitle() returns a String value, getNumPages() returns an int value). Because these methods are used to return values, you can use them as values themselves, and thats why they are referred to within the System.out.println() statements.
Inheritance
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 lesson.
Here it is again:
class Book{
Sting title;
int numPages;
public void setNumPages(int numOfPages){
numPages = numOfPages;
}
public int getNumPages(){
return numPages;
}
public void setTitle(theTitle){
title = theTitle;
}
public String getTitle(){
return title;
}
}
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.
The SoftCoverBook class:
class SoftCoverBook extends Book{
public boolean isSoftCover(){
return true;
}
}
In the above example, the SoftCoverBook class extends the Book class. By virtue of this inheritance, the SoftCoverBook class has 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. There is however, a new method declared in the SoftCoverBook class, and that method is the isSoftCover() method. This method is set to return true denoting that an object declared as a SoftCoverBook does indeed represent a soft cover book.
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
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 class Object, the SoftCoverBook class is also automatically an instance of it.
Encapsulation
Encapsulation means data hiding. Through the use of encapsulation, you can declare class variables as private so that other classes cannot access them directly, however you can declare public methods within the same class that access these variables, and you can then access private class variables by proxy through these public methods.
Example:
class Book{
//declare a private class variable
private String title;
//declare a public method
//to set the title of the book
public void setTitle(String theTitle){
title = theTitle;
}
//declare a public method
//to get the title of the book
public String getTitle(){
return title;
}
}
The above example declares a class with a private class variable. If you try to access it directly from another class, you will not be able to. However, you can access it by proxy through the public methods setTitle() and getTitle(). The setTitle() method is used to set the title of the book with its parameter theTitle. The method sets the variable title to whatever value you supply to it through its parameter theTitle. The getTitle() 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.
Using the Book class when the variable title is declared as private:
class AClass{
public static void main(String[] args){
//declare a new instance of the Book class
Book ABook = new Book();
//try to access the private variable title
//it will not work and an error will be generated
//System.out.println(ABook.title);
//try to set the title of the book
//through the public setTitle() method
ABook.setTitle("A book to read");
//try to access the private variable title through
//the public getTitle() method and it will be printed
System.out.println("Book title: " + ABook.getTitle());
}
}
Output:
Book title: A book to read




