4.1.2.3 - Classes, objects and encapsulation (Java)

4.1.2.3 - Classes, objects and encapsulation (Java)

Learn how a Java class defines object state and behaviour, how constructors create usable instances, and how access controls protect that state. Distinguish instance methods from static and abstract methods before the next lesson develops relationships between classes. This Java route is incomplete: runnable support pending; the source questions are written practice with no Run.

Classes, fields and methods

A class defines the attributes and behaviours common to objects of that type. An object is a particular instance with its own state. In Java, attributes are stored in fields, while methods express operations. The class definition is not itself one object.

Java
class Counter {
    private int value;

    Counter(int initialValue) {
        value = initialValue;
    }

    public void increment() {
        value++;
    }

    public int getValue() {
        return value;
    }
}

A Counter's characteristic is its value; its behaviours include incrementing and reporting that value. Counter left = new Counter(2); Counter right = new Counter(8); creates two objects. left.increment(); makes left's value 3 while right's stays 8. The method source is shared by the class, but a non-static method acts on the particular object receiving the call. Within such a method, this refers to that current object.

Grouping related state and operations makes an entity's responsibilities visible. Callers can request increment() rather than knowing how the class stores its count. This supports reuse and localised change: changing a class's internal representation need not change callers if its public operations still meet the same contract. Object-oriented design is useful when many entities need independent, persistent state; it still requires good decomposition and testing.

Encapsulation

Bundling related state and behaviour within a class and controlling access to its implementation through a defined public interface.

A private field alone is not enough for a good design. If a public operation allows any impossible state, callers can still violate the intended invariant. The later access section shows how an operation can enforce a meaningful rule.

Instantiation and constructors

Instantiation creates an object from a class, and a constructor initialises it. In Counter first = new Counter(4);, new Counter(4) creates the object and invokes the matching constructor; the resulting reference is assigned to first, a reference variable of type Counter. The constructor has the class's name and no return type, including no void.

If a class declares no constructor, Java supplies an implicit no-argument constructor, subject to superclass construction rules. Once a class declares a constructor, a no-argument one is not automatically supplied as well. Our Counter declares Counter(int), so new Counter() is not a valid call unless that additional constructor is written.

Reference assignment does not copy an object:

Java
Counter first = new Counter(4);
Counter alias = first;
Counter separate = new Counter(4);
alias.increment();

Now first and alias refer to the same Counter whose value is 5. separate refers to a different Counter whose value remains 4. Equal field values do not imply identical objects. A null reference refers to no object; invoking an instance method through it would fail. The variable and the referenced object are distinct entities.

A constructor parameter can have the same name as a field. this.value = value; assigns the parameter's value to the current object's field. Without this, value = value; would merely assign the parameter to itself in that constructor. Trace what each name denotes before assuming the object was initialised.

Encapsulation and access

Access specifiers determine which code may use a member, helping a class enforce its own rules. public exposes a member to callers wherever its declaring type is accessible. private restricts use to the declaring class's access context. protected permits access in the same package and through subclass access under Java's rules; it does not give every unrelated class access. Across packages, a subclass cannot use a protected instance member through an arbitrary superclass object.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account