posted Oct 2, 2018, 6:12 AM by Konstantinovich Samuel   [ updated Oct 3, 2018, 11:30 AM ]
Goal: You down with OOP?

The notes for the next few days are are curated from the Oracle website's notes on objects just for you!
If you want the full set of Oracle notes you can find them here:

Object Oriented Programming (OOP)
This programming paradigm is very different from what you are used to. 

Four Tenets of OOP
  • Abstraction : a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it.
  • Encapsulation : a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.
  • Inheritance. (we will discuss later)
  • Polymorphism. (we will discuss later, but one example is overloading methods)
  • Never betray the Dark Brotherhood or its secrets. To do so is to invoke the Wrath of Sithis.

Oversimplification: The use of Objects to organize data and functions is how we accomplish these four goals.

Classes are blueprints for objects.
They contain 
-Fields (instance variables)
-Constructors (special 'method like' things that are NOT methods!)

(look at the overall idea, do not try to understand everything)
public class Bicycle {
    // the Bicycle class has
    // three fields
    private int cadence;
    private int gear;
    private int speed;
    // the Bicycle class has
    // one constructor
    public Bicycle(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;

    // the Bicycle class has
    // three accessor methods
    public int getCadence() {
        return cadence;

    public int getSpeed() {
        return speed;

    public int getGear() {
        return gear;

    // the Bicycle class has // four mutator methods public void setCadence(int newValue) { cadence = newValue; } public void setGear(int newValue) { gear = newValue; } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; }


Declaring Classes

You've seen classes already:

class MyClass {
    //method declarations like main, and your methods
    //so far always public and static

Now you've seen classes defined in the following way:

class MyClass {
    // fields
    // method declarations

There are several kinds of variables:

  • Member variables in a class—these are called fields.
  • Variables in a method or block of code—these are called local variables.
  • Variables in method declarations—these are called parameters.

Access Modifiers

The first (left-most) modifier used lets you control what other classes have access to a member field. For this course consider only public and private Other access modifiers exist like protected.

  • public modifier—the field is accessible from all classes.
  • private modifier—the field is accessible only within its own class.
In the spirit of encapsulation, it is common to make fields private. This means that they can only be directly accessed from the Bicycle class. We still need access to these values, however. This can be done indirectly by adding public methods that obtain the field values for us:

Here is what public variables would look like:

public class Point {
    //public means we can access x and y
    //from any other class that creates a point.
    public int x;
    public int y;

    // a constructor!
    public Point(int a, int b) {
	x = a;
	y = b;

public class CreateObjectDemo {

    public static void main(String[] args) {
        Point p,q,r;
        p = new Point(1,2);
        q = new Point(3,4);
        r = q;
        r.x = 0; //public means we can access x and y from this class

        System.out.println("p: " + p.x + "," + p.y);
        System.out.println("q: " + q.x + "," + q.y);
        System.out.println("r: " + r.x + "," + r.y);