Object Oriented Programming

Object oriented programming is a helpful way of organizing code. By mapping physical objects to code objects, programmers are able to think more easily about a problem.

Terminology

class - an abstract representation of something with certain properties and abilities.

object - a specific instance of a class whose state is unrelated to any other object of the same class. Objects are instantiated, or created, from a class definition.

methods - the functions of a class which gives its objects certain capabilities.

members - the variables of a class which gives its objects certain properties.

constructor - a special function of a class that instantiates an object of that class and initializes its members to some default values specified by the programmer.

accessor (getter) - a method which retrieves a private value in an object.

mutator (setter) - a method which updates a private value in an object.

static - denotes a method or member that belongs to the entire class, not a specific object. For methods, this denotes that the method is called by the class, not a specific object. For variables, this denotes that the variable value is shared across all instantiated objects and should be accessed through the class.

scope - the visibility / context of a variable

inheritance - when a more general class provides an outline for a more specific class

Example: A Rectangle class can inherit from a Shape class.

The general class is called the parent or base class, and the specific class is called a subclass of the base class.

Inheritance removes code duplication and allows for greater code reuse. This is good because having multiple copies of the same code can be a problem if you have to change something later and need to find all of the copies to change. Code reuse allows changes to be made in one central location, propagating the results down where they are needed.

polymorphism - allowing multiple implementations for a method depending on the context; generally refers to method overloading and method overriding.

overload - multiple methods with the same name but different parameters.

override - the replacement of a parent class's method with a more specific implementation of the method by the subclass.

encapsulation - the hiding of implementation details to achieve a simpler interface.

Example:

// Class definition for animals in a pet store.
class Animal {
	// Member variables.
	// These variables are encapsulated; direct access to them is not 	
	// allowed, but instead is granted by the accessor functions below.
	private String name;
	private int id;
	private float price;
 
	// Constructor
	public Animal(String name, int id, float price) {
		this.name = name;
		this.id = id;
		this.price = price;
	}
 
	// Methods
	// setPrice() is a polymorphic method because
	// it is an example of method overloading.
	public void setPrice(float price) {
		this.price = price;
	}
 
	// Mutator (setter) method.
	public void setPrice(int price) {
		this.price = (float) price;
	}
 
	// Accessor (getter) methods.
	public String getName() {
		return this.name;
	}
 
	public int getId() {
		return this.id;
	}
 
	public float getPrice() {
		return this.price;
	}
}
 
// Object instantiation.
Animal fido = new Animal();

Practice Question: What is the difference between an interface and abstract class?

An interface specifies a set of methods, usually grouped under a common theme. It cannot have variables except for static final variables, used as constants. A class implements an interface by implementing all of the functions defined in the interface. A class can implement multiple interfaces to achieve different functionalities.

An abstract class is an incomplete class definition which declares methods for its subclasses to provide implementations for. It can contain variables. Because Java does not support multiple inheritance, a class may only extend one abstract class.

Neither can be instantiated.

Practice Question: Does Java allow multiple inheritance? Why / why not?

Java does not allow multiple inheritance because of the semantic ambiguity involved in permitting classes to have multiple super classes. The classic issue is the diamond problem:

Suppose two classes B and C inherit from A, and class D multiple inherits from both B and C. If a method in D calls a method defined differently in both B and C, then which function should D run?

However, Java allows multiple interfaces to be implemented. In the case of two interfaces with the same method, a compile time error results if the two methods do not have the exact same return type.

Practice Question: Design an object oriented version of a Tic Tac Toe game.

[[[ Code answer ]]]

Webfaction is the ULTIMATE hosting platform for any serious developer.
SSH, WordPress, Rails, Django, cronjobs, compile and execute anything.
ProgrammingInterview.com is proudly powered by Webfaction.