Basics of OOP in Java - ilya-khadykin/notes-outdated GitHub Wiki

Characteristics of object-oriented language:

  1. Everything is an object.
  2. A program is a bunch of objects telling each other what to do by sending messages.
  3. Each object has its own memory made up of other objects.
  4. Every object has a type (class type).
  5. All objects of a particular type can receive the same messages.

An object has state, behavior

What Is an Object?

An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life.

Objects are key to understanding object-oriented technology. Look around right now and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle.

Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.

Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.

A bicycle modeled as a software object:

By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6.

Bundling code into individual software objects provides a number of benefits, including:

  • Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.
  • Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.
  • Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.
  • Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.

What Is a Class?

A class is a blueprint or prototype from which objects are created.

In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.

The following Bicycle class is one possible implementation of a bicycle:

class Bicycle {

    int cadence = 0;
    int speed = 0;
    int gear = 1;

    void changeCadence(int newValue) {
         cadence = newValue;
    }

    void changeGear(int newValue) {
         gear = newValue;
    }

    void speedUp(int increment) {
         speed = speed + increment;   
    }

    void applyBrakes(int decrement) {
         speed = speed - decrement;
    }

    void printStates() {
         System.out.println("cadence:" +
             cadence + " speed:" + 
             speed + " gear:" + gear);
    }
}

The fields cadence, speed, and gear represent the object's state, and the methods (changeCadence, changeGear, speedUp etc.) define its interaction with the outside world.

You may have noticed that the Bicycle class does not contain a main method. That's because it's not a complete application; it's just the blueprint for bicycles that might be used in an application. The responsibility of creating and using new Bicycle objects belongs to some other class in your application.

Here's a BicycleDemo class that creates two separate Bicycle objects and invokes their methods:

class BicycleDemo {
    public static void main(String[] args) {

        // Create two different 
        // Bicycle objects
        Bicycle bike1 = new Bicycle();
        Bicycle bike2 = new Bicycle();

        // Invoke methods on 
        // those objects
        bike1.changeCadence(50);
        bike1.speedUp(10);
        bike1.changeGear(2);
        bike1.printStates();

        bike2.changeCadence(50);
        bike2.speedUp(10);
        bike2.changeGear(2);
        bike2.changeCadence(40);
        bike2.speedUp(10);
        bike2.changeGear(3);
        bike2.printStates();
    }
}

The output of this test prints the ending pedal cadence, speed, and gear for the two bicycles:

cadence:50 speed:10 gear:2
cadence:40 speed:20 gear:3

What Is Inheritance?

Inheritance provides a powerful and natural mechanism for organizing and structuring your software. Classes can inherit state and behavior from their superclasses.

Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear). Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.

Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses:

The syntax for creating a subclass is simple. At the beginning of your class declaration, use the extends keyword, followed by the name of the class to inherit from:

class MountainBike extends Bicycle {

    // new fields and methods defining 
    // a mountain bike would go here

}

This gives MountainBike all the same fields and methods as Bicycle, yet allows its code to focus exclusively on the features that make it unique. This makes code for your subclasses easy to read. However, you must take care to properly document the state and behavior that each superclass defines, since that code will not appear in the source file of each subclass.

What Is an Interface?

An interface is a contract between a class and the outside world. When a class implements an interface, it promises to provide the behavior published by that interface.

As you've already learned, objects define their interaction with the outside world through the methods that they expose. Methods form the object's interface with the outside world; the buttons on the front of your television set, for example, are the interface between you and the electrical wiring on the other side of its plastic casing. You press the "power" button to turn the television on and off.

In its most common form, an interface is a group of related methods with empty bodies. A bicycle's behavior, if specified as an interface, might appear as follows:

interface Bicycle {

    //  wheel revolutions per minute
    void changeCadence(int newValue);

    void changeGear(int newValue);

    void speedUp(int increment);

    void applyBrakes(int decrement);
}

To implement this interface, the name of your class would change (to a particular brand of bicycle, for example, such as ACMEBicycle), and you'd use the implements keyword in the class declaration:

class ACMEBicycle implements Bicycle {

    int cadence = 0;
    int speed = 0;
    int gear = 1;

   // The compiler will now require that methods
   // changeCadence, changeGear, speedUp, and applyBrakes
   // all be implemented. Compilation will fail if those
   // methods are missing from this class.

    void changeCadence(int newValue) {
         cadence = newValue;
    }

    void changeGear(int newValue) {
         gear = newValue;
    }

    void speedUp(int increment) {
         speed = speed + increment;   
    }

    void applyBrakes(int decrement) {
         speed = speed - decrement;
    }

    void printStates() {
         System.out.println("cadence:" +
             cadence + " speed:" + 
             speed + " gear:" + gear);
    }
}

Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

What Is a Package?

A package is a namespace for organizing classes and interfaces in a logical manner. Placing your code into packages makes large software projects easier to manage.

A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. You might keep HTML pages in one folder, images in another, and scripts or applications in yet another. Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.

The Java platform provides an enormous class library (a set of packages) suitable for use in your own applications. This library is known as the "Application Programming Interface", or "API" for short. Its packages represent the tasks most commonly associated with general-purpose programming. For example, a String object contains state and behavior for character strings; a File object allows a programmer to easily create, delete, inspect, compare, or modify a file on the filesystem; a Socket object allows for the creation and use of network sockets; various GUI objects control buttons and checkboxes and anything else related to graphical user interfaces. There are literally thousands of classes to choose from. This allows you, the programmer, to focus on the design of your particular application, rather than the infrastructure required to make it work.

The Java Platform API Specification contains the complete listing for all packages, interfaces, classes, fields, and methods supplied by the Java SE platform. Load the page in your browser and bookmark it. As a programmer, it will become your single most important piece of reference documentation.

Key points (1)

  • Object-oriented programming lets you extend a program without having to touch previously-tested, working code.
  • All Java code is defined in a class.
  • A class describes how to make an object of that class type. A class is like a blueprint.
  • An object can take care of itself; you don’t have to know or care how the object does it.
  • An object knows things (instance variables) and does things (instance methods).
  • Things an object knows about itself are called instance variables. They represent the state of an object.
  • Things an object does are called methods. They represent the behavior of an object.
  • When you create a class, you may also want to create a separate test class which you’ll use to create objects of your new class type.
  • A class can inherit instance variables and methods from a more abstract superclass.
  • At runtime, a Java program is nothing more than objects ‘talking’ to other objects.

public static void main(String[] args) { }

The two uses of main:

  1. to test your real class
  2. to launch/start your Java application

The Heap

Each time an object is created in Java, it goes into an area of memory known as The Heap. All objects—no matter when, where, or how they’re created – live on the heap. But it’s not just any old memory heap; the Java heap is actually called the Garbage-Collectible Heap.

When the JVM can ‘see’ that an object can never be used again, that object becomes eligible for garbage collection. And if you’re running low on memory, the Garbage Collector will run, throw out the unreachable objects, and free up the space, so that the space can be reused

Encapsulation

It's an idea of restricting direct manipulation of class variables. Every interaction with a class is done using methods.

Getters and Setters

By forcing everybody to call a setter method, we can protect the class instance variable from unacceptable value

class ElectricGuitar {
  String brand;
  boolean rockStarUsesIt;

  String getBrand() {
      return brand;
  }

  void setBrand(String aBrand) {
      brand = aBrand;
  }

  boolean getRockstarUsesIt() {
      return rockStarUsesIt;
  }

  void setRockStarUsesIt(boolean yesOrNo) {
      rockStarUsesIt = yesOrNo;
  }
}

Encapsulation (using Getter and Setters) allows you to validate your data in one place

See the example: (2)

/*
 *  Date.java
 */
package date1;

public class Date {
  private int day;     // restricting access to an instance variable
  private int month;   // restricting access to an instance variable
  private int year;    // restricting access to an instance variable

  @Override
  public String toString() {
      return this.month + "/" + this.day + "/" + this.year;
  }

  // getters
  public int getDay() {
      return day;
  }

  public int getMonth() {
      return month;
  }

  public int getYear() {
      return year;
  }

  public boolean isLeapYear(int year) {
      return (((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0);
  }

  public int daysInMonth(int month, int year) {
    int rv;
    switch(month) {
        case 9: // Thirty days hath September
        case 4: // April
        case 6: // June
        case 11: // and November
            rv = 30;
            break;
        case 2:
            if (isLeapYear(year)) {
                rv = 29;
            } else {
                rv = 28;
            }
            break;
        default:
            rv = 31;
            break;
    }
    return rv;
  }

  public void nextDay() {
      int dayCount = daysInMonth(this.month, this.year);
      this.day++;
      if (this.day > dayCount) {
          this.day = 1;
          this.month++;
          if (this.month > 12) {
              this.month = 1;
              this.year++;
          }
      }
  }

  // Constructor
  public Date(int m, ind d, int, y) {
      this.day = d;
      this.month = m;
      this.year = y;
  }
}
/*
 *  Date1.java
 */
package date1;

public class Date1 {
  
  public static void main(String[] args) {
      Date meetingDate = new Date(2, 29, 2012);

      System.out.println("Meeting will be on "
                + meetingDate.getMonth() + "/"
                + meetingDate.getDay() + "/"
                + meetingDate.getYear()
      );

      System.out.println("Again meeting will be on"
                + meetingDate.toString()
      );

      System.out.println("Yet again meeting will be on"
                + meetingDate
      );

  }
}

Inheritance

Java supports single inheritance: you can extend only one specialized (super) class (but that class can extend something else)

Method Overriding

Method name, returned type and a sequence of arguments should be identical

  @Override
  public void pet() {
      System.out.println("Super class method was overwritten by its subclass version")
  }

Dynamic invocation or late binding

We can invoke derivative class methods from a superclass, see example below.

/**
 * Zoo.java
 */

package zoo;

import zoo.animals.Lion;
import zoo.animals.Sheep;
import zoo.animals.Zebra;
import zoo.animals.Animal;

public class Zoo {
  
//  private Sheep sheep = new Sheep();
//  private Lion lion = new Lion();
//  private Zebra zebra = new Zebra();

  private Animal [] animals = {
     new Sheep(), new Lion(), new Zebra()
  };

  public void visitAnimal(Animal animal) {
      System.out.println("Try to feed the animal");
      animal.feed();
      System.out.println("Try to pet the animal");
      animal.pet();
  }

  public void visit() {
      System.out.println("You visit the zoo...");
//      System.out.println("You go to see the sheep, you try to feed it");
//      sheep.feed();
//      System.out.println("You try to pet the sheep");
//      sheep.pet();

//      System.out.println("You go to see the lion, you try to feed it");
//      lion.feed();
//      System.out.println("You try to pet the lion");
//      lion.pet();

//      System.out.println("You go to see the zebra, you try to feed it");
//      zebra.feed();
//      System.out.println("You try to pet the zebra");
//      zebra.pet();

      for (int i = 0; i < animals.length; i++) {
          visitAnimal(animals[i]);
      }
  }

  public static void main(String[] args) {
      Zoo z = new Zoo();
      z.visit();
  }
}
/**
 * Animal.java
 */
package zoo.animals;

public class Animal {
  public void feed() {
      System.out.println("Animal eats...");
  }

  public void pet() {
      System.out.println("Animal gets petted...");
  }
}

/**
 * Zebra.java
 */
package zoo.animals;

public class Zebra extends Animal {
  @Override
  public void feed() {
      System.out.println("Zebra eats grass, looking around anxiously...");
  }
  @Override
  public void pet() {
      System.out.println("Zebra looks at you strangely and runs away...");
  }
}
/**
* Lion.java
*/
package zoo.animals;

public class Lion extends Animal {
 @Override
 public void feed() {
     System.out.println("Lion eats meat and roars to warn you not to try to take its kill");
 }
 @Override
 public void pet() {
     System.out.println("Lion roars as you approach and you decide no to try it...");
 }
}
/**
* Sheep.java
*/
package zoo.animals;

public class Sheep extends Animal {
@Override
public void feed() {
    System.out.println("Sheep eats grass, munching happily...");
}
@Override
public void pet() {
    System.out.println("Sheep pushes against your leg and head-butts "
               + "your hand for more attention...");
}
}

Abstract class

Abstract is like a concept rather that a physical entity. For that reason there is no point of creating it directly. For example:

  public abstract class Animal {
      public abstract void feed();

      public abstract void pet();

Keyword abstract protects us of creating an instance of that class. Abstract class can contain any number of real methods or variables, not only abstract ones.

Interface

Interface is an abstract class which has only abstract methods. You can extend one class to many interfaces.

  public interface Animal {
      public abstract void feed();

      public abstract void pet():
  }

  public class Lion implements Animal {
      @Override
      public void feed() {
        // your code
      }

      @Override
      public void feed() {
        // your code
      }
  }

References

  1. Head First Java, 2nd Edition
  2. Java Programming Basics by Simon Roberts
  3. Lesson: Object-Oriented Programming Concepts