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 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

You can use the variables of a class with an object by referring to them by name together with the object name.

Syntax:
objectName.variableName;
Example:
class AClass{ public static void main(String[] args){ Book ABook = new Book(); ABook.numPages = 200; System.out.println("Number of pages in the book: " + ABook.numPages); } }
Output:
Number of pages in the book: 200

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.

Syntax:
objectName.methodName([parameters])
Example:
class AClass{ public static void main(String[] args){ 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"); System.out.println("Book title: " + ABook.getTitle()); System.out.println("Number of pages in the book: " + ABook.getNumPages()); } }
Output:
Book title: Read Me Number of pages in the book: 300

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

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.

The Book class once again:
class Book{ Sting title; int numPages; public void setNumPages(int numOfPages){ numPages = numOfPages; } public int getNumPages(){ return numPages; } public void setTitle(String 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{ }

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

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

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.

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; } }
Try to access private variable directly:
class AClass{ public static void main(String[] args){ //declare a new instance of the Book class Book ABook = new Book(); ABook.setTitle("Greatest book ever"); //try to access the private variable title directly //it will not work and an error will be generated System.out.println(ABook.title); } }
Output:
AClass.java:12: title has private access in Book System.out.println(ABook.title); ^ 1 error

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.

Try to access private variable through public method:
class AClass{ public static void main(String[] args){ //declare a new instance of the Book class Book ABook = new Book(); ABook.setTitle("Greatest book ever"); //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: Greatest book ever
© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information