projects | back
Introduction
What is object oriented programming? Well in the shortest way possible writing object-oriented programs involves creating classes, creating objects from those classes, and creating applications, which are stand-alone executable programs that use those objects. But that's a lot of nothing to someone who doesn't know much about programming right? Well, not really; A class is just a blueprint, it defines data fields and methods, An object is just an instance* of a class(So for example: Computer c = new Computer();). Throughout this essay I will be using a Computer as an example.
package uk.josh-king.concepts;
/**
* This is the Computer class which holds all of the data for the individual
* computers. Method overriding allows Java to invoke method based on a
* particular object at run-time instead of declared type while coding.
*
* @author Josh
* @since 08/06/2013
*/
public class Computer {
protected String name;
protected String manufacturer;
protected double price;
protected EnumOS os;
/**
* This is the main computer constructor.
*
* @param s
* Name of the computer
* @param s0
* Manufacturer which makes the computer
* @param d
* Price of the individual computer
* @param s1
* Operating system for the computer
*/
public Computer(final String s, final String s0, final double d, final EnumOS s1) {
this.name = s;
this.manufacturer = s0;
this.price = d;
this.os = s1;
}
public enum EnumOS {
WINDOWS, LION;
}
/**
* @return the name
*/
public final String getName() {
return this.name;
}
/**
* @return the manufacturer
*/
public final String getManufacturer() {
return this.manufacturer;
}
/**
* @return the price
*/
public final double getPrice() {
return this.price;
}
/**
* @return the os
*/
public final EnumOS getOs() {
return this.os;
}
}
There are three main types of object orientated techniques;
* Polymorphism
* Encapsulation
* Inheritance
Polymorphism
Polymorphism is an object orientated concept which advise the use of a common interface instead of concrete implementation while writing code. When we program an interface our code is capable of handling any new requirement or enhancement which may arise due to new implementation of our common interface. If we don't use common interface and rely on concrete implementation, we always need to change and duplicate most of our code to support new implementation. SO in short it makes it easier to add new factors into the code. There are two kinds of polymorphism; Overriding and Overloading.
Overriding occurs when a class declares a method with the same signature as that of an inherited method. It is usually used to make the methods within a class more specific. Invoking the superclass' method (from inside the class) is done with the syntax super.name(parameters).For example;
package uk.josh-king.concepts;
public class AcerComputer extends Computer {
public AcerComputer() {
super("Aspire", "Acer", 156.04, EnumOS.WINDOWS);
}
}
Overloading occurs when a class declares two or more methods with the same name but different signatures. When a message is sent to an object or class with overloaded methods, the method with the best matching signature is the one that is invoked. If the message and the method have a different number of parameters, no match is possible. However if there is no clear best match, Java reports a syntax error.
Encapsulation
Encapsulation means putting together all the variables (instance variables) and the methods into a single unit called Class. Programmers sometimes refer to encapsulation as using a black box*. A programmer can access and use the methods and data contained in the black box but cannot change them. Below example shows the Computer class with properties, which can be set once while creating object using constructor arguments. Properties can be accessed using getter methods which are having public access modifiers.
/**
* @return the name
*/
public final String getName() {
return this.name;
}
/**
* @return the manufacturer
*/
public final String getManufacturer() {
return this.manufacturer;
}
/**
* @return the price
*/
public final double getPrice() {
return this.price;
}
/**
* @return the os
*/
public final EnumOS getOs() {
return this.os;
}
Inheritance
Inheritance is commonly used state and behavior from other classes. In this example, Computer now becomes the superclass of HPComputer, AcerComputer, and AppleComputer. In the Java, each class is allowed to have one direct superclass. The syntax for creating a subclass is simple; At the beginning of your class declaration, use the extends keyword, followed by the name of the class to inherit from:
package uk.josh-king.concepts;
public class AppleComputer extends Computer {
public AppleComputer() {
super("Mac", "Apple", 999.99, EnumOS.LION);
}
}
instance*: An object is an instance of a class.
black box*: In science and engineering, a black box is a device, system or object which can be viewed in terms of its input, output and transfer characteristics without any knowledge of its internal workings.