Social Media

Tag Archives for " encapsulation "

Object Oriented Principles

I was interviewing some students for a placement, and asked the standard “What are the four principles of OO development?” question as a warm-up. I was surprised that they struggled to name all four principles off the top of their head. In their defence the courses they came from were not purely software driven, and also would have covered other programming styles like procedural.

Lets start – the four principles of OOP are –

  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance


Abstraction is about exposing just enough information or functions, while hiding the complexity. This can be seen in Java through the external methods (public methods) in classes and interfaces, for example –

[sourcecode language=”java”]

String encapsulationExample = "encapsulation".substring(0, 6);


The developer only needs to know what the substring method does, and doesn’t need to understand how it does it. The implementation is “encapsulated”.


This is the flipside of abstraction. With encapsulation we hide the implementation of the abstracted methods. This means the internal implementation of the external methods. An example of this is getter/setter methods to access private members of a class


Polymorphism in coding terms can be summed up as “one method, many implementations”. A literal definition is “of many forms” (“many” – poly, “forms” – morph). It allows us to define specialisms of methods in subclasses through overloading and overriding

I could waste my time coming up with an example of polymorphism in practice, but the best example Ive found is from stackoverflow (

[sourcecode language=”java”] public abstract class Human {

public abstract void goPee();

[/sourcecode] [sourcecode language=”java”] public class Male extends Human {

public void goPee() {
System.out.println("Stand Up");

[/sourcecode] [sourcecode language=”java”] public class Female extends Human {

public void goPee() {
System.out.println("Sit Down");


public static void main(String args) {

ArrayList<Human> group = new ArrayList<Human>();

group.add(new Male());
group.add(new Female());
// … add more…

// tell the class to take a pee break
for (Human person : group) {


Output –
Stand Up
Sit Down


Inheritance is the process where a classes behaviour is derived from an existing superclass. This allows the developer to have common code contained in the superclass, and specialisms in the subclasses. A subclass inherits in java using the extends keyword.