Learntofish's Blog

A blog about math, physics and computer science

Object Oriented Programming – An Introduction

Posted by Ed on September 20, 2009

In the following I will explain what Object Oriented Programming means in Java. And you will see that it is easy to understand once you’ve grasped the notion of an object. I encourage you to copy the code and run it in Java.

What is an object?
Definition: An object in Object Oriented Programming is characterized by
a) its attributes (properties)
b) methods that do something with the attributes (for example change them)

For example Homer Simpson is an object:
a) His attributes are: name, age, gender, etc.
b) And there are methods that you can apply to the object:
e.g. you can ask for Homer Simpson’s name or you can let him have his birthday that changes his age.

What is a class?
There are different kind of objects, for example:
– Marie Curie, Isaac Newton and Emmy Noether have something in common. They are persons. We say that they are objects of the class “Person”.
– Apple, banana, mango: These are objects of the class “Fruit”.
– Jacket, blouse, pullover, pants: These are objects of the class “clothes”.

Let’s get practical and type the following code in Java (to copy the code: go the the source code window below and point to the right top corner. There, click on the white symbol):

public class Person {

	//Declare attributes (properties) and initialize them
	String name = "No name yet";
	int age = -1;

	//Declare methods that can change the attributes
	void setName(String x){
		this.name=x;
	}

	void getName(){
		System.out.println("My name is "+this.name+".");
	}

	void setAge(int x){
		this.age=x;
	}

	void getAge(){
		System.out.println("I am "+this.age+ " years old.");
	}

	void birthday(){
		System.out.println("I've just had my birthday!");
		System.out.println("Now, I am "+ (this.age+1) +" years old.");
	}

	public static void main(String[] args) {

		//
		// ENTER THE FOLLOWING CODE HERE
		//

	}
}

Let’s examine the program:
– In line 01 we declare a class “Person”.
– In line 03-05 we define what kind of attributes (properties) our objects have. Here, our objects shall have a name and an age.
– In line 07-27 we define the methods that can be applied to our objects For example in line 08-10 the method setName() gives our objects a name.
– Our main() method (or program) starts in line 29.

Compiling the code above does not yield any console output since we haven’t written anything in our main() method yet. In the following we will add code to the main() method.

 

Creating an object and setting its attributes

public static void main(String[] args) {

	//Create object father
	Person father = new Person();

	//Set attributes
	father.setName("Homer");
	father.setAge(40);
}

Let’s examine the code:
– Here, in line 04 we create an object “father” from the class “Person” by typing
Person father = new Person();
In general, we create an object by typing
Class object = new Class();
Besides, the method Person() is called a “constructor”. The constructor has its name for obvious reasons: we construct an object.

– In line 07-08 we apply methods to our object “father”. We give him a name and an age:
father.setName(“Homer”);
father.setAge(40);

In general applying a method to an object is done by typing:
object.method();
In particular, you have noticed the dot. Whenever you want to apply a method to an object you use the dot.

Show the attributes on the console output
Compiling the code above still won’t give us any output on the console. Let’s add some code to the main() method (line 10-12):

public static void main(String[] args) {

	//Create object father
	Person father = new Person();

	//Set attributes
	father.setName("Homer Simpson");
	father.setAge(40);

	//Get attributes
	father.getName();
	father.getAge();
}

Now, we want the father to tell us his name and age. That is why in line 11-12 we apply the methods getName() and getAge() to our object father. Compiling the code yields the console output:
My name is Homer.
I am 40 years old.

Exercises:
1a) Using the “Person” class from above create an object “daughter”.
1b) Give her the name “Lisa Simpson” who is 8 years old.
1c) “daughter” shall tell us her name and age via the console output.
1d) Using the “Person” class from above create an object “son” with the attributes name=”Bart Simpson” and age=8.
1e) Make the son tell us his name. Afterwards, apply the method birthday() to son.

2a) Create a class “Fruit” with a main() method.
2b) The objects of this class should have the following attribute: color
The class has the methods setColor() and getColor().
2c) Create an object apple. Set its color to “red”. Then show the color on the console output by using the getColor() method.

Answers:

 

1a), 1b) and 1c)

public static void main(String[] args) {

	//1a)
	Person daughter = new Person();

	//1b)
	daughter.setName("Lisa Simpson");
	daughter.setAge(8);

	//1c)
	daughter.getName();
	daughter.getAge();
}

The console output for 1c) is: My name is Lisa Simpson. I am 8 years old.

 

1d) and 1e)

public static void main(String[] args) {

	//1d)
	Person son = new Person();
	son.setName("Bart Simpson");
	son.setAge(8);

	//1e)
	son.getName();
	son.birthday();

}

For 1d) and 1e) the console output is:
My name is Bart Simpson.
I’ve just had my birthday! Now, I am 9 years old.
2a) and 2b)

//2a)
public class Fruit {

	//2b) Declare attributes
	String color;

	//2b) Define methods
	void setColor(String x){
		this.color=x;
	}

	void getColor(){
		System.out.println("My color is: "+this.color+".");
	}

	public static void main(String[] args) {

	}

}

For 2c) add the following code to the main() method:

public static void main(String[] args) {

	//2c)
	Fruit apple = new Fruit();
	apple.setColor("red");
	apple.getColor();

}

Here, we get the console output: My color is: red.

References:
1) Object-Oriented Programming Concepts
A tutorial by Sun Microsystems, the Java developer

2) Java Video Tutorial 5: Object Oriented Programming
A excellent tutorial by aphonik.

3) An introduction to object-oriented programming by High School eLearning
Clear explanation on the ideas of object oriented programming

4) Object-oriented language basics
A more advanced tutorial by javaworld.com

5) Eclipse and Java for Total Beginners
Video Tutorial on how to use Eclipse with an introduction to Object Oriented Programming

Advertisements

One Response to “Object Oriented Programming – An Introduction”

  1. […] is a great introductory series to object-oriented programming (oop) for Python. There’s also one for […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: