java featured image

8.1 Java | Class & Object Relationship

Object-Oriented programming is designed to help programmers, like you, to develop large scale software programs and GUIs.

So we can solve many programming obstacles using arrays, loops, methods, and selections. But just knowing these programming skills are not enough to develop Graphical User Interfaces (GUIs) and big software programs. But knowing how to use Object Oriented Programming will give you a step towards being able to develop such complexities.

Tabs: File Edit View Favorite Tools Help Buttons: Add Extract Test Copy Move Delete Info

How to Define Classes for Objects

The Attributes and actions of an object is defined by the Class.

Object Oriented Programming (OOP) uses objects. An object is akin to a thing, like a mouse, a keycap, a bird, dept, a bank, a whale, etc. Anything. Anyobject. With a similar mindset, objects in programming are noted to have unique behaviors, states, and identities.

The properties/attributes of an object are represented by data fields with their current values. For example, triangles have the data fields height and width; which are the properties that characterizes a triangle. Circles have the data fields radius, where radius is a property that characterizes a circle.

*Note that an object’s state refers to the collective set of properties and values stored in the object. [Source]

Actions or Behaviors of an object is defined by a method. Invoking a method on an object means to ask the object do something. For example, for circle objects you can design methods with names like fetchArea() and fetchPerimeter(). So that when a circle object invokes the fetchArea() method, the area of the circle object is returned. And when a circle object invokes the fetchPerimeter() method, the perimeter of the circle object is returned. Continuing the example, you can design a setRadius(radius) method that changes a circle object’s radius.

Class Object Relationship

The same types of objects are designed (or defined) from a common class. You can think of a class like you think of blueprints, templates, recipes, cookie cutters, molds, etc.

Motlle_crespellines represent the class and object relationship

Classes define (or design) an object’s data fields and methods. Know that an object is known as an instance of class. Such that object = instance. To wrap up, the relationship of class to object/instance is analogous to a chocolate chip cookie recipe to the cookies themselves. As you can make as many chocolate chip cookies you want from a single recipe,  you can also make as many objects/instances you want from a single class.

Class Template to Class Objects
You can make as many objects you want from a single class… Take out the highlighted different colors, and this diagram becomes a UML illustration.

Classes in Java use variables to design data fields. Classes also use methods to design actions. Know that classes have a method of special type, called a constructor, which are invoked (told to do something) to make new objects. A constructor has the ability to perform any action, but constructors are primarily made to perform initializations; like initializing an object’s data fields.

Example

Let’s see how classes are designed for circle objects:

Note that in the above example, the Circle class does not have a main method like other normal classes. This means that the Circle class cannot be run. The Circle class is instead just a definition or design for circle objects.

UML

UML stands for Unified Modeling Language. UML is a notation or way of making class diagrams which help the programmer see the relationships between classes and their objects. Note that for class diagrams, the data field is denoted as:

And the constructor is denoted as:

And finally the method is denoted as:

Java UML Diagram with Explanation
A UML Diagram

◄◄◄BACK | NEXT►►►

What's Your Opinion?