Introduction: Objects: the programmers model of the world.

  • Object-oriented programming (OOP) involves programming using objects.

  • An object represents an entity in the real world that can be distinctly identified. For example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects. An object has a unique identity, state, and behavior.

Video

  • Exercise: Stop at 0:35 - Find the differences (attributes) of dogs

  • Exercise: Stop at 1:15 - Find the common behaviours (methods) of dogs

Real world object example

  • Provide the students with the example of real balls, like Table Tennis Balls, Handball, Soccer Ball, Tennis Ball, Plastic Ball

  • Students should write down on the whiteboard the differences between these balls (attributes) and the common behaviours (methods)

  • Demonstration: Create a Ball class with the attributes and methods written down on the board. Example: Ball.java. This example is with main-method in the Ball class.

  • Class is a mold (shape, form), the object created are defined by that form.

    • Class can be seen as kind of template.

  • Objects have members: Methods and fields.

    • Methods are used to operate on the fields. Setting, getting, computations.

    • Fields hold the value or state of an object.

  • An object has its own, non shared, copies of fields.

  • Demonstration: Create a separate class which creates an instance of Balls containing a main method. Example: TestBall.java.

In Java all classes inherit from the class Object. Inheritance passes traits down the hierarchy. Every class, except Object has at least one super class (which is Object), so Java (the compiler, the JVM) knows that certain methods will always be available, like toString(). simpleclass.

Reference variable.

  • Name of a memory location referencing an object.

  • e.g. in String s="abd"; s is a reference variable of type String with the value "abd";

  • All types in Java, with the exception of the primitive types, are reference types.

  • You can imagine the reference as a life line to an object. If an object is no longer attached to at least one such life line, it is considered garbage, and dealt with by the garbage collector.

this.

  • When a method accesses a field of an object, it is always the field of a that object, this object. e.g. this.radius is the long name for the field radius of a circle object.

Let the objects work.

To use a method of an object, use the object reference name followed by a dot and the method (and parameters).

  • E.g. ball.bounce() is the method bounce() called on an object referred as ball.

  • inside the method, the object can be referred to as this, because this is a implicit formal parameter to all object methods.

  • Because of this, the methods are shared between all objects of the same type, each time with a different implicit actual parameter this.

  • this is itself a reference to the current object.

  • References are, just as primitive types, passed by value.

    • The difference is that any modification to an object is visible after the method call, because only the reference is passed by value, not all data contained by the object.

Visibility, accessors

Demo with circle class, playing with setters and getters and the encapsulate flag in the Netbeans getter wizard.

  • A member can have one of four types of visibility:

    • public, for the whole world to see

    • private, accessible only by the object and other objects of the same class.

    • protected, to be seen by package members and sibling classes. (We do not use this for now)

    • default or package private, to be seen only by package members. (We do not use this for now)

  • Choose visibility wisely. Public or protected only when needed, prefer private or package private. (For now we use private and public only!!!)

  • No need to exposed (getters) for all members. It is okay for an object to have private members.

  • Note that private DOES allow access from objects of the exact same type. Private is meant mainly to keep the implementation flexible, less to protect anything.

Nomenclature

  • Objects are instances of classes.

  • The type is a reference type.

  • All types in Java, with the exception of the primitive types, are reference types.

  • An object can have members, either methods or fields. Each object has its own copies of the fields, and shares the methods.

  • A class can have static members, such as static methods and static fields (e.g. public static void main(…​)) and static fields. These static members are shared with all objects.

Concept Description

member

Field or method

field

Part of an object, connected to an instance

method

Works with instance, can reference this

static field

Part of the class, shared by all instances

static method

Part of class shared by all instance. Has NO this.

final

Can be set or initialized only once, either at declaration time or in constructor. Also does not change during use.

static final

Used to create constant values. Often combined with public. Conventionally written UPPER_CASE.

More examples

  • TV

  • naming conventions for getters (get and is for boolean) and setters.

    • Field name with first letter capitalized and prefixed with get or is.

  • Separate application from normal objects.

  • It is okay to have many main methods. The way you start the application decides which …​static…​ main(..) is actually run.

  • Although less conventional, a class can call the main method of another class.

static methods

  • It is quite normal for a class to NOT have static methods.

  • Static methods are bound to the class, not object, and are NOT inherited.

  • Static methods are available without having instances of the class.

  • Static methods are available globally (having public visibility), package wide (having default visibility) or instance accessibility (having private visibility).

  • static methods have NO access to instance fields or methods. They have no this.

static fields.

  • Example: MyCircle.java has a static field.

  • A static field is shared by all instances and stored in the class-object (one location).

  • static final fields are the way to declare constants in Java.

Constructors

  • A constructor is a special kind of method.

    • Its name is the same (including capitalization) as the class it is defined in.

    • No return type, the type is implicitly that of the class.

    • Multiple constructors with same name but different arguments, overloading are allowed.

    • A constructor can use other already defined constructors, avoiding duplication of code.

      • this(args…​) calls other constructor with args as actual parameters, is only allowed in constructors and must be the first statement in the using constructor.

  • There is always at least one constructor, even if you do not declare one yourselves. It is called the default constructor and is generated by the compiler if you do not, unless:

    • you define a constructor, then the default constructor generation is suppressed, so if you still want a parameter-less constructor, you must add that yourselves.

    • show with Circle example, extended as Disc. (With weight).

Create a simple Java Program by Pieter van den Hombergh