Session 3 |
Introduction into object-oriented programming
Before we begin, here is a small introduction to object-oriented programming.
Most of our focus so far has been on procedural programming - breaking a complex task into smaller subtasks (methods). A method works as a separate part of a program and it can be called from anywhere in the program. When a method is called, execution of the program moves to the beginning of the called method. After the execution of the method is done, the program will continue from where the method was called. This is the oldest style of programming and even in a language like Java we still use procedural techniques.
But Java has also a different approach to programming that we call object-oriented programming. In object-oriented programming, just like in procedural programming, we attempt to divide a program into smaller parts - objects with its state (properties) and behavior (methods).
Object-oriented programming involves a particular view of programming that has its own terminology. Let's explore that terminology.
Objects
By now we know how to create primitive variables and assign values (e.g. int x = 5;
). If we want to operate on a series of ordered data of the same type, we use arrays (e.g. int[] a={1,2,3}
). What should we do if we want to group specific data of different types of data? For example, a pet shop sells dogs. One of the dogs is a 3-year-old brown labrador Liza. We cannot create an array liza = {"Liza", "labrador", "small", 3, "brown"} because the values in the array of different data types. In such cases we can use objects.
When we create our own object, we can group the data of different data types under a brand new data type and store the data in a single variable. For example:
Dog liza = new Dog("Liza","labrador","small",3,"brown");
In this example, a brand new data type is Dog and various data about labrador Liza are stored in variable liza.
In order to create a new object (like liza), we have to create:
- object class - a class without the main method in which a new data type, object state and behavior are defined in general;
- client class - a class with the main method where specific object(s) called instance is created.
1. Object class called Dog.java
public class Dog { // The name of the class is also the name of a new data type // Object state (properties) String name; String breed; int age; String size; String color; String accent = "bow-wow"; // Constructor public Dog (String dogName, String dogBreed, int dogAge, String dogSize, String dogColor) { this.name = dogName; this.breed = dogBreed; this.age = dogAge; this.size = dogSize; this.color = dogColor; } // Object behavior (methods) public String woofing() { return ("The dog "+name+" makes sound like "+accent); } }
2. Client class called Execute.java
public class Execute{ // main method public static void main(String[] args){ // Create an instance of the object class for labrador Liza // and assign values to the instance variables Dog liza = new Dog("Liza","labrador","small",3,"brown"); System.out.println(liza.woofing()); } }
Used resources: MOOC "Object-Oriented Programming with Java: Chapter 21" by Arto Hellas, Matti Luukkainen.
Session 3 |