Here are some Cliffs Notes on OOP to help you get going while perusing the suggested litterature:
Some OO Basics
Encapsulation, the main point of OO, means that you put logic and data inside a logical entity known as object. They can be anything you'd like.
Relationships between objects can be achieved through composition (aka. association) or inheritance.
In Java, all objects are instantiated from a class type. Think of class as a cookie cutter and the objects as the cookie that has been cut out. Like so:
// Declaration of class
public class MyAwesomeClass {
// Quite empty
}
// Instantiation of object from a class with "new"
MyAwesomeClass awesome = new MyAwesomeClass();
// The object now resides as a reference in the "awesome" variable.
In contrast: some other OO-languages, such as JavaScript, does this differently with a prototype since it is prototype based instead of class based.
A class contains fields (aka. members, properties, class variables) and methods (aka. class functions). Objects will have these fields and methods for themselves. It is good practice that the fields are accessed through methods called getters (aka. accessors) or setters (aka. mutators).
public class MyAwesomeClass {
private int myInteger;
// "information hiding" members related
// to the object are usually set to private
// the getter
public int getMyInteger() {
return this.myInteger;
}
// the setter
public void setMyInteger(int theInteger) {
// theInteger is a *parameter* for the setter
this.myInteger = theInteger;
}
}
// Usage:
awesome.setMyInteger(4);
System.out.print(awesome.getMyInteger); // should output 4
Constructors are called when you create an instance of a method. Like so:
public class MyAwesomeClass {
private int myInteger;
// The constructor has no return type, and the name is same as class
public MyAwesomeClass(int theInteger) {
this.myInteger = theInteger;
}
// getter and setter from before are here...
}
// Usage:
MyAwesomeClass awesome = new MyAwesomeClass(8);
System.out.print(awesome.getMyInteger); // should output 8
Overloading is a cool and sometimes useful concept:
class MyAwesomeClass {
private int myInteger;
// Overloading constructor example
public MyAwesomeClass() {
this(12); // will call the overloaded constructor with 12
}
// Overloads MyAwesomeClass() with another parameter
public MyAwesomeClass(int theInteger) {
this.myInteger = theInteger;
}
// Method overload example
public addInteger() {
this.addInteger(1); // calls the overloaded method with 1
}
public addInteger(int theInteger) {
this.myInteger += theInteger;
}
}
Composition
You can compose many objects together using references to other class types. In this case we have an MyAwesomeStrategy inside MyAwesomeClass.
In UML it is drawn like this:
+----------------+ 1 1 +-------------------+
| MyAwesomeClass |-------| MyAwesomeStrategy |
+----------------+ +-------------------+
In code it looks like this:
class MyAwesomeStrategy {
public void doIt() {
System.out.print("Trolololol");
}
}
class MyAwesomeClass {
private MyAwesomeStrategy strategy;
public MyAwesomeClass(MyAwesomeStrategy strategy) {
this.strategy = strategy;
}
public void doStrategy() {
this.strategy.doIt();
}
}
// in your main method:
MyAwesomeClass awesome = new MyAwesomeClass(new MyAwesomeStragety());
awesome.doStrategy(); // prints out Trolololol
Tip: Got a big class? Split it up to many using composition!
Inheritance
One object can inherit the properties and methods of another object. It can look like this in UML:
+-----------------+ +-------------------+
| MyOtherStrategy |----------|>| MyAwesomeStrategy |
+-----------------+ +-------------------+
And in code it looks like this:
class MyOtherStrategy extends MyAwesomeStrategy {
public void doIt() {
System.out.print("Stackoverflow Rules!");
}
}
// in your main method:
MyAwesomeClass awesome = new MyAwesomeClass(new MyOtherStragety());
// This works because the MyOtherStrategy object
// extends MyAwesomeStrategy
awesome.doStrategy(); // prints out Stackoverflow Rules!
// That's some cool POLYMORPHISM going on there!
Congrats! Now you've also learned the strategy pattern (sort of).
While inheritance is useful, it also breaks encapsulation. It does however have really good uses as long as you don't have to worry implementation about the classes. Remember the important OO mantra:
"Prefer composition over inheritance." - GOF
Phew! And that's just the start!