© We Can Code IT, LLC
[ A ] bstraction
[ P ] olymporphism
[ I ] nheritance
[ E ] ncapsulation
We create abstractions to simplify our program domain. They allow us to focus on the problem we are solving rather than getting lost in minute details. You don't need to know how every subsystem in your car works in order to drive, do you?
Think about writing code as the process of creating a language to describe a problem we're solving. Abstractions are the building blocks of this language.
We are creating abstractions in the small whenever we:
This is why naming is so important: we need to accurately convey the abstraction we're describing to those that follow (as well as our later selves).
We apply the other OO principles (PIE) to create meaningful and useful abstractions of more complex concepts.
the quality or state of existing in or assuming different forms
Polymorphism allows us to represent intent, but allow the implementation to vary as needed based on context.
A method with the same name that accepts different argument types.
Examples:
Console
's WriteLine
methods.Assert.Equal
methodsBoth of these methods accept string
s, int
s, decimal
s.
A method that redefines a method from a superclass is said to override that method. A great example of this are the toString
, equals
, and hashCode
methods from java.lang.Object
that we have discussed. (Remember, Object
is the superclass of all classes.)
We also implement polymorphism by:
String
isA Object
)ArrayList
isA List
isA Collection
isA Iterable
)Inheritance is the mechanism whereby a class inherits behavior from a superclass. We call this extending the superclass. Recall that all classes, whether we tell them to or not, implicitly extend Object
.
Sometimes we create types (classes) that only exist so that they may be extended. We use the keyword abstract
to create these abstract classes, which may also declare abstract
methods.
The Java Collections Framework has several great examples of using inheritance and polymorphism: the Collection
s, List
s, and Map
s that you know and love.
Inheritance is what allows String
concatenation to work without us doing anything extra (though perhaps it ain't pretty). Like when we do this:
VirtualPet pet = new VirtualPet();
System.out.println("My pet is " + pet);
What happens behind the scenes is that the toString()
method of Object
(VirtualPet
's parent class) is being called. VirtualPet
has inherited this method from its parent.
Encapsulation at its simplest is hiding away information that isn't necessary to share. The more knowledge we have about an object, the more complex our problem solving becomes.
Knowledge is power. Power corrupts.
public class Circle {
private double radius;
public double getRadius() {
return radius;
}
}
The radius
instance variable above is private and exposed via an accessor method (getRadius
). Not only does this avoid radius
being manipulated externally and possibly resulting in an invalid state, but also gives us the flexibility to implement getRadius
in a different way if necessary. The code that asks our Circle
object for the radius doesn't need to know how the radius is being determined.
This is encapsulation. A more complex example of encapsulation would be an object responsible for calculating sales tax for a transaction. If the formula changes, yet it is encapsulated within the object, other objects requesting the tax calculation need not change. This would also likely involve polymorphism, since sales tax calculations vary by a number of factors, including location and product type.