Encapsulation

Object oriented programming (OOP) is a term used to define how certain programming languages organize and adapts code. The basic principle is fluent between all programming languages that use this structure, and most common and up to date languages do.

OOP uses a structure of encapsulating data into different “parts” and “chucks” that can interact with each other, or work independently. This allows not only code to be reused, but also gives the ability to write applications that work intuitively and perform based on user command instead of sequentially carrying out actions.

OOP applications can be broken down into 4 separate types of “parts” that each have its own purpose:

  • Classes

You can think of a class as a base that objects build upon. Classes have a simple set of “guidelines” that objects use to help carry out their operation. A good and simple example of this is pizza. Pizza would be the class, and each type of pizza would be an object of pizza, like pepperoni pizza. The details that all pizzas share would be constructed in the pizza class, like pizza dough, sauce, and the shape. Classes can also be objects of other classes, so you could say that pizza is an object of junk food.

  • Instances (of classes, sometimes referred to as objects)

Instances, or objects, take the foundation of a class and add its own attributes that are specific to what the object is. So let’s look at the object pepperoni and cheese pizza. It would have a class of pizza, which will give it all the basic characteristics of a pizza, and then it would add that it has cheese, and pepperonis.

  • Methods

Methods, sometimes referred to as functions, are separated sets of common instructions that are only carried out when it is called to. So let’s say that with our pizza program, we wanted the ability to make a pizza. We could create a makePizza method that would hold all the instructions for making a pizza.

  • Interfaces

Interfaces provide us with a way of separating our logic with our visual components. This is the part of the application that we can build our application window with buttons, text fields, etc. So for our pizza program example, a restaurant employee would be our interface. It gives us a way to display and give our pizza to our customers, and for them to give us instructions on how to make the pizza.

  • Packages

Packages are simply a deceleration of where our files belong to. It gives us a way to group everything we have created together and let every part of the application know what it belongs to. For our pizza program, our restaurant building would be our package.

Here is an illustration of the pizza program in code form so you can get a better idea:

pizza class

package pizzaPlace;

class pizza{

	void pizza(){
    	// fields and methods defining a pizza go here
    }

}

peppironiCheese class

package pizzaPlace;

class pepperoniCheese extends pizza{

	public void pepperoniCheese(){

    }
	// fields and methods defining a pepperoni pizza go here

}

pizzaEmployee interface

package pizzaPlace;

interface pizzaEmployee{

	// fields and methods defining how the employee interacts with a customer (user) goes here

}

employeeDavid class

package pizzaPlace;

class employeeDavid implements pizzaEmployee{

	// fields and methods defining indevidual characteristics of this employee, like their name goes here

}

pizzaActions class and method to make pizza

package pizzaPlace;

class pizzaActions{

	pizza makePizza(pepperoniCheese){

        PizzaPlace.pepperoniCheese pepCheese = new PizzaPlace.pepperoniCheese();
        pizza = pepCheese.cookPizza();

    	return pizza;
    }

}

So that is a short and simple introduction to Object Oriented Programming. I recommend from this point you start out with the basics of the programming language of your choice.

Leave a Reply