View on GitHub

reading-notes

OOP

Object-Oriented Programming Concepts

What Is an Object?

It’s an instance of the class.

Benefits of Bundling code into individual software objects:

  1. Modularity: The source code for an object can be written and maintained independently of the source code for other objects.
  2. Information-hiding.
  3. Code re-use.
  4. Pluggability and debugging.

What Is a Class?

A class is a blueprint for object are created.

Example
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);
    }
}

What Is Inheritance?

Inheritance is to allow object acquires all the properties and behaviors of a parent object

Example
class MountainBike extends Bicycle {

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

}

What Is an Interface?

An interface is a contract between a class and the outside world.

interface Bicycle {

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

    void changeGear(int newValue);

    void speedUp(int increment);

    void applyBrakes(int decrement);
}

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);
    }
}

What Is a Package?

A package is a namespace for organizing classes and interfaces in a logical manner.

Binary, Decimal and Hexadecimal Numbers: