Jump to content

Java Programming/Classes

From Wikiversity

Programs in Java are composed of Classes. A class is an object with a specified set of fields, methods, static functions organized for modular use.

Access

[edit | edit source]

Objects in Java have four access levels:

  • The default is that a class is visible only within a package.
  • A public class or field is visible globally.
  • A protected class or field is visible only to the current class and its subclasses.
  • A private class or field is only accessible to that class.

Creating objects

[edit | edit source]

An object within Java is an instance of a class. To create an object, you use the new operator:

{
  Employee em = new Employee();
}

An object declaration, as shown above, is a reference to an existing object. If the reference changes, it doesn't directly change or destroy the object it is pointing to.

Constructors

[edit | edit source]

A constructor is a function that executes on creation of a given class. It is responsible for initializing variables to the starting state, if required.

public class Employee
{
    /* A default constructor */
    public Employee()
    {
    }

    /* Constructor with parameters */
    public Employee(String name)
    {
    }
}

Constructors are usually declared as public. If they are protected or private, they cannot be directly created outside the class unless you provide a factory method, or allow developers to use a different constructor.

Inheriting from classes

[edit | edit source]

Java classes can inherit features from a parent class by using the extends keyword. A class can only inherit from one parent class. As a classical example, you may see the class Employee:

public class Employee
{
}

In another file:

public class Manager extends Employee
{
}

If a child class uses a constructor, it can choose any constructor in the parent class by using the super keyword. This must be done within the first statement of the constructor.

Child classes have full access to the parent's public and protected fields and methods. Anything private is exclusive to the parent class and cannot be removed directly.

Abstract classes

[edit | edit source]

Discussed later are abstract classes:

public abstract class Employee
{
}

Abstract classes are declared similarly to regular classes, but unlike normal classes, cannot be instantiated directly. They are only created through subclasses that implement all abstract methods.

Multiple classes in a file

[edit | edit source]

Under normal java programming, classes are normally located within a file of their own. For example, the Employee class should be found in Employee.java, and the manager class should be found in Manager.java. In rare cases, you may have a reason to include both classes into a single file.

There are two ways to do so. The first involves listing the classes one after another:

public class Employee
{
}

class Manager extends Employee
{
}

In this style, only the first class file may be declared public. All following classes must use default access (where only classes in the same package may access it.)

Nested classes have one class contained entirely in another, and may either be static or inner. A static class is not associated with the outer class instance, while an inner class maintains this association.

public class Outer
{
  private class Inner
  {
  }
}

In this context, the inner class is declared private, but it can still be accessed by the Outer class. Ouside the Outer class, it is not accessible.


Project: Introduction to Programming in Java
Previous: Boolean variables — Java Programming/Classes — Next: Inheritance