Object-Oriented Programming
notes
this page could be a lot more – largely because of the huge power of Object-Oriented-Programming.
instead I have chosen to limit the scope of this page to simple definitions.
this page pairs well with:
public class Circle extends Object {
// data:
private static final double PI = 3.14159;
public int x = 0, y = 0;
private int r = 6;
static int no_circles = 0;
// constructors:
public Circle() { super(); no_circles++; }
public Circle(int x, int y, int r) { this(); this.x=x; this.y=y; this.r=r; }
public Circle(int x, int y) { this(x,y,5); }
// methods:
public double getArea() { return PI * r * r; }
@Override public String toString() {
return "[" + x + ", " + y + " r=" + r + "]";
}
@Override public boolean equals(Object obj) {
if (obj == this) return true;
if (!(obj instanceof Circle other)) return false;
return x==other.x && y==other.y && r==other.r;
}
// Getters/Setters
public int getR(){ return r; }
public void setR(int r){ if(r>0) this.r=r; }
}
Animal lion = new Animal()lion is the variable name, Animal is the type, and Animal() calls the constructor of the Animal class to return an object of that type.
A class is a blueprint for the state (data/fields) and behaviours (methods) of an object. It is reused for the creation of objects and can make use of some clever patterns to improve scalability.
One thing to remember with OOP is its adjacency with real-world modelling: I, Aayush Bajaj am an object; produced from the class People. I have attributes, that can either be private or public; i.e. I can tell information to members outside of my class / package, or I could not.
Inheritance allows subtype polymorphism, but this is not the only way to achieve polymorphism.
Inheritance tightly couples the code. The "is-a" relationship is a necessary, but not sufficient condition for using inheritance.
Many times composition is still preferred to inheritance despite the "is-a".
Embodies the "has-a" relationship. Builds objects from parts, favouring encapsulation and changeability.
class Engine { void start(){} }
class Car { private final Engine e = new Engine(); void start(){ e.start(); } }
Degree of interdependence (between what?). Symptoms of high coupling include:
- many imports
- reaching through objects (feature envy)
- Noun: a change made to the internal structure of software to make it easier to understand and cheaper to modify without change its observable behaviour
- Verb: to restructure software by applying a series of refactorings without changing its observable behaviour.
Pure contracts: types without implementation or state.
interface PaymentGateway { boolean charge(long cents); }
class StripeGateway implements PaymentGateway { public boolean charge(long c){ return true; } }
Partial implementations with state and constructors; can define template methods and share code among subclasses.
abstract class Shape {
abstract double area();
final double scaleTo(double k){ return k*k*area(); } // template utility
}
class Rect extends Shape { double w,h; Rect(double w,double h){this.w=w;this.h=h;} double area(){return w*h;} }
Translates to mean "many forms". It allows objects of different types to be treated as objects of a common type.
There are two main types:
-
Compile-time Polymorphism:
- method overloading. a class may have many methods with the same name, but chooses the correct one based on the parameters at compile-time.
-
Run-time Polymorphism:
- method overriding. allows the implementation in a subclass to be used instead of the implementation in the superclass. this choice is made at run-time.
- subtype polymorphism / LSP. is the replacement of a super-class with an object of its subclass.
abstract class vs. interface
this was a point of confusion for me during an interview:
| feature | abstract class | interface |
|---|---|---|
| method implementation | can have abstract methods and concrete methods | always abstract only |
| state (variables) | all types | only public, static, final |
| constructors | allowed | not allowed |
| inheritance | can only extend one abstract class | can implement multiple interfaces |
| relationship | "is-a" | "can-do" or "has-a capability" |
references
- https://docs.oracle.com/javase/tutorial/java/concepts/index.html
- Gamma et al., Design Patterns
- Bloch, Effective Java (3e).
- Fowler, Refactoring (2e).