Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Code Newbie > Submit Tutorials > Java
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 04-07-2003, 01:21 AM   #1 (permalink)
srt42
Registered User
 
srt42's Avatar
 
Join Date: Apr 2003
Location: Christchurch, New Zealand
Posts: 1
srt42 is on a distinguished road
Post What is OOP and Java?

Many people think Java is just a programming language for writing web pages. It can be. However the true power of Java lies in the Object Orientated (OO) approach it takes. This tutorial attempts to explain what OOP and Java are.

What is OOP?
-----------------
Object Orientated Programming is a way of splitting program code up into 'data' based sections, rather than 'method' based sections. It sets out a program in a logical, and easy to comprehend way. For example; say you want to write a database which stores information on a hall of residents at college/university. Think of some nouns that could be used to describe the 'objects' or classes that are inside such a thing. Obvious examples are 'Student', 'Hall', and 'Database'. There are probably better examples, but for this tutorial I will use these three examples. In Java, to represent this, you would write a Class (the definition of an object) for each of these. So we would have a class called Student etc.

Now think about what a Student has. A Student has a room number, a phone number, a name. These are important things to store. An object knows the state of these 'variables' which are called 'instance variables' - and a good OOP Class would not allow any other class to access this information directly. Here is a very basic section of Java code to represent this;
Code:
public Class Student { private int roomNumber; private String firstName; private String lastName; private int phoneNumber; }
'public' means that any other object can interact with this one. 'private' means that these variables can only be accessed from within this class. 'String' and 'int' are types, although very different. String is another class, and a firstName is a type of String. 'int' stands for integer, and is a 'primitive' data type. This is in-built within Java. For a new programmer this may sound daunting, so here is an explanation of one line;
Code:
private int roomNumber
This line means - roomNumber is a variable, that is of type int. It can hold a number value, without a decimal point, eg 1, -30, 370. roomNumber is also only accessable from within the class Student.

What is the difference between a Class and an Object?
----------------------------------------------------------------------
A Class is what you write. It is the 'description' of what all objects of that type will be made from. It is the sort of 'DNA' which builds objects. An Object is an instance of a Class. The Java Virtual Machine reads your code - and creates an object to match it. At that point you can change the variables within that object, so two objects of the same type can be very different. This can be related to real life;

Bob and Mary are both students, but they are different. They are objects of type Student. They have different phone numbers and room numbers.

Now what?
--------------
We've build a class... but it's pretty useless. It has variables, which are not even initialised. This is pretty important too. To do this we use something called a 'constructor' which is defined by using the class name. There are two major types. Null constructor's don't take in any values as arguments, and often set everything to 0, "", or null. Full constructor's can take in arguments, and set the instance variables to match them. Example...
Code:
public Class Student { private int roomNumber; private String firstName; private String lastName; private int phoneNumber; Student() { roomNumber = 0; firstName = ""; lastName = ""; phoneNumber = 0; } }
The above example as a null constructor. It 'assigns' default values to each of the variables. Without doing this Java will make a fuss, and you'll end up with 'null pointer exception' error messages. Below is an example of a full constructor
Code:
public Class Student { private int roomNumber; private String firstName; private String lastName; private int phoneNumber; Student(int rnum, String fname, String lname, int pnum) { roomNumber = rnum; firstName = fname; lastName = lname; phoneNumber = pname; } }
It assigns the instance variables to the corresponding arguments. Now that our Student can store data, and the data is initialised... now what???

Methods
-----------
Method's are similar to 'functions' in most other procedural languages. They interact with the data. In this way, even though the instance variables are private, other objects can find out about them. For example... a student might need to give out their name at the start of the year. The corresponding method might be getName(). It also might be split into getFirstName() and getLastName(). We'll use the latter in order to cut out more complicated code. Code for this method might be;
Code:
public String getFirstName() { return firstName; }
The method takes in nothing, but it says the return type is String. So the method returns 'firstName' which is a String.
How does the method know what the value of firstName is??? Because it's in the Student class. Here is another method that would 'set' the first name...
Code:
public void setFirstName(String name) { firstName = name; }
Which is similar to the full constructor, in the way it takes arguments and sets a variable to the argument. In order to stop confusion, I'll put it all together...
Code:
public Class Student { /* instance variables */ private int roomNumber; private String firstName; private String lastName; private int phoneNumber; /* null constructor */ Student() { roomNumber = 0; firstName = ""; lastName = ""; phoneNumber = 0; } /* full constructor */ Student(int rnum, String fname, String lname, int pnum) { roomNumber = rnum; firstName = fname; lastName = lname; phoneNumber = pname; } /* a setter method */ public void setFirstName(String name) { firstName = name; } /* a getter method */ public String getFirstName() { return firstName; } }
Now our class has it all. A Student as a name, a phone number, and a room number. Another object, say a Receptionist, could call the 'getFirstName()' method of a particular Student to get their name. At birth a Parent object could call the 'setFirstName()' method to set the child's name.

So Classes are the definitions of Objects. Objects know things, and so they have 'instance variables' to store their data. In order to manipulate data, they, and other objects can call 'methods'. It's quite simple, but it not always easy to put into code. I would really need to go into the detail of heirachy to help explain this more, but perhaps another time. Hope this helps. If your interested in learning OOP or Java specifically I suggest doing some sort of tertiary educational course - especially if you've been doing a lot of procedural programming before, in for example C or BASIC. If you want to nut it out yourself, try downloading BlueJ (www.bluej.org) and the JDK (Java Development Kit). BlueJ is an interactive programming environment. It's great for 'visualizing' Java objects and classes, and it easy to use.

Good luck, and remember Java isn't just what's on fancy web-pages.
__________________
srt42 is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparison of Different Programming Languages Punch-M6.net All Other Coding Languages 23 04-19-2007 01:34 PM
Recommend a good OOP book? Epsilon PHP 3 06-10-2004 09:03 AM
Java Class Basics: Static Methods bdl Java 0 02-29-2004 02:25 PM
How long did it take you guys? JLB87 Standard C, C++ 21 09-19-2003 08:05 PM
C++ for a Java programmer? Belisarius Standard C, C++ 5 08-03-2003 04:12 PM


All times are GMT -8. The time now is 06:04 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle