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:

  • Some basic object-oriented concepts and terminology
  • How to define and use attributes and methods
  • What objects are and how to create and reference them
  • How to deal with errors which may occur
  • About the provided documentation for Java
  • About primitive data types 

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:

  • An object's state is all of the data that it stores. This corresponds to the values of all of its instance variables taken as a whole.
  • An object's protocol is all the things it can do. This corresponds to its methods taken as a whole, which you can think of as its behaviour.

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.

Encapsulation and data hiding

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).

// Example instance variables
private String name;
private String placeOfBirth;
private String copyOfHtmlPage;
 

Methods

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

// Example method defined in a particular class returns a String
public String sayHello() {
    return “Hello”;
}

// Somewhere else we send a message to the above...
String s = sayHello(); // if sending from same class
String s = anObject.sayHello(); // if sending from a different class

// The variable named s should now contain “Hello”;
 

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:

// Modified version of method that has a method argument
public String sayHello(String name) {
	return “Hello ” + name;
}

// Somewhere else we send a message to the above...
String s = sayHello(“Fred”);

// The variable s should now contain “Hello Fred”;
 

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

// Modified version of method that has 3 method arguments
public String sayHello(String firstName, String lastName,
								  int age) {
	return “Hello ” + firstName + “ “ + lastName +
		  “, you are“ + age + “ years old”;
}

// Somewhere else we send a message to the above...
String s = sayHello(“Fred”, “Bloggs”, “50”);

// The variable s should now contain “Hello Fred Bloggs, you are 50 years old”;
 
  • Each argument type is declared on the method signature (String, String, int).
  • It is permissible to write each Java statement over more than one line, as in the two places above.
  • The + symbol is used to concatenate String objects into larger String objects.
  • Spaces are included in the strings to aid formatting.
  • The method call must specify all of the arguments in the correct order (two String objects followed by an int primitive value in the above example).

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

// Return a sum
public int sum(int firstNumber, int secondNumber) {
	int total = firstNumber + secondNumber;
	return total;
}

// Nothing to return
public void outputWord(String word) {
	System.out.println(word);
}

 

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:

// Add two numbers
int value = sum(3, 6);

// At this stage, value will contain 9
 

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


Print
×
Stay Informed

When you subscribe, we will send you an e-mail whenever there are new updates on the site.

Related Posts

 

Comments

No comments made yet. Be the first to submit a comment
Sunday, 19 October 2025

Captcha Image