By Tony Bevis on Saturday, 07 October 2023
Category: Java

Java programming course: 2.1 Object-oriented programming

This is Section 2 of the Java programming course.

Object-oriented programming

Java is an object-oriented programming language, providing you with a means of modelling your programs in a way that more closely models the real-world as compared to procedural languages.

In this section you will learn:


Object-oriented concepts and terminology

In the previous section you learned that that a class is defined as a template or blueprint to model something, and an object is an instance (i.e., a particular example) of a class.

You also learned that when you write a class you specify its instance variables to store its attributes and its methods and constructors in order to provide its functionality (i.e., behaviours).

This section will develop these concepts and introduce some additional terminology.

Each object consists of both state and protocol:

Encapsulation describes the concept that each individual object consists of both its state and its protocol.

It is a good idea to prevent an object's state from being modified in nonsensical ways; for example, you might have an instance variable that stores an animal's age, and it would make no sense for this to contain a negative value. To prevent this kind of thing from happening you can define the visibility of each instance variable, and it is recommended that in most cases you declare instance variables to be private. This keyword means that only the class in which they are declared has direct access to the variable.

But what if you need other classes to be able to update an animal's age, but still need to prevent nonsensical values? To accomplish this, a method can be defined that contains Java code; it can verify that the age value is sensible before updating the instance variable value. It is common for many methods to be declared public, meaning that any other object can invoke it.

You can think of methods as providing a protective layer around the variables; the only way other parts of the application can get at the variables' data is through its methods.

Attributes

The attributes (also known as properties, or sometimes fields) of a class are defined by declaring instance variables. These are defined within the class block but outside of any constructors or methods.

A good rule-of-thumb is to declare instance variables to be private in order accomplish data hiding.

All instance variables should be given meaningful names and follow the convention of camel-case but starting with a small letter. You should avoid abbreviations (unless they are in common use).

Methods

​ When an object sends a message to another object, it causes a method to be invoked:

Methods names should also adhere to camel-case and start with a small letter. The first word in a compound word method name (or the only word in a single word method name) should normally be a verb. The name is always followed by an opening and closing bracket to contain its required arguments, but the brackets may be empty if no arguments are needed.

In the above example method sayHello() the pair of brackets are empty. This signifies that no arguments (also known as parameters) are being sent along with the message. An argument is a value that the method might use to do something useful, for example to say hello to a particular person:

Note how you need to specify the type of argument being sent (String above). You can send multiple arguments separated by commas:


The above example method returns a String reference. You can return any recognised class or primitive type, or if you don't need to return anything just specify the keyword void

Inside the sum() method above, the + symbol is being used for its usual arithmetical purpose of adding the two values contained in the argument variables firstNumber and secondNumber. The sum of these is then stored in a local method int variable called total. The value of total is then returned to the caller. You could use this method as follows:

The term member is sometimes used to refer to either a variable or a method. Therefore, a class's members are all of its attributes and methods.

In the next lesson you will learn how to create objects.

Lesson 2.2 Creating objects

Related Posts

Leave Comments