Introduction to Object-Oriented Programming

Programming Paradigms

  • Imperative: C, Pascal, Fortran, COBOL
  • Functional: Lisp, Haskell, Erlang
  • Logic Prolog:
  • Object-Oriented: Smalltalk, Java, C++, Objective C
  • Most languages are multiparadigm
    • Lisp is a mixture of functional and imperative, with object-oriented features added on
    • Java, C++, and Objective C are a mixture of imperative and object-oriented


  • An object is a value that includes
    • State (data)
    • Behavior (methods that manipulate the state)
  • Objects are frequently (but not always) instances of a class


  • Define the state and behavior for a group of objects
  • Most, but not all, object-oriented languages have classes


  • An individual object belonging to a class is called an instance of the class

Instance Variables

  • Store state data for an individual object
  • They are referred to as
    • Fields, in java
    • Members, in C++
  • Each instance has its own set of instance variables

Class Variables

  • Store state data that is shared among all instances of a class
  • In Java and C++, class variables are declared as static

Instance Methods

  • Manipulate the instance variables of an object
  • Have access to instance variables, other instance variables, class variables, and class methods
  • In Java and C++ methods look like functions

Class Methods

  • Manipulate the class variables of an object
  • Only have access to class variables and other class methods
  • In Java and C++ class methods are declared as static


  • Special methods that are called to create and initialize objects
  • In Java and C++, have the same name as the class

Example Class

public class Counter {
  // Class variable
  static int serial = 0;

  // Instance variable
  int count;

  // Constructor
  public Counter() {
    count = 0;

  // Accessor (instance) method
  public int getCount() {
    return count;

  // Instance method
  public void increment() {

  // Modifier (instance) method
  public void setCount(int n) {
    count = n;

  // Accessor (class) method
  public static int getSerial() {
    return serial;


  • In a strongly typed language, the signature of a method or function defines the argument types and return type
  • In a dynamically typed language, the signature just indicates the number of arguments
  • int f(int x, int y);

Abstract Methods and Classes

  • A method is abstract if just the signature is given with no implementation
  • A class is abstract if it contains one or more abstract methods
  • Abstract classes can have some methods that are completely defined
  • Abstract classes cannot be instantiated


  • Java only
  • An interface is like an abstract class
  • Defines signatures and constants
  • Interfaces are an alternative to multiple inheritance

Access Levels

  • Fields and methods can be declared
    • private
    • protected
    • public
  • If there is no access modifier, the access is default

Java access levels

Modifier Class Package Subclass Other
public Y Y Y Y
protected Y Y Y N
default Y Y N N
private Y N N N