Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TUT] Object-Oriented Programming: A Small Opening
#1
Hello there members of LFE! It's been quite some time since I posted a thread and I believe a decent tutorial, as such I've decided to make a small tutorial on understanding Object-Oriented Programming and some of it's concepts to allow any of you budding programmers to get a little bit of an easier start, especially during Bamboori's current game making competition.

So, what do you expect to learn from a tutorial such as this?

You will learn from this tutorial (provided you actually go through it):
- What is Object-Oriented Programming
- Why would Object-Oriented Programming be used
- What popular examples of programming languages use it
- What an Object is
- What a Class is

Now, in this tutorial you will in time see sections of code. This code is Java because I feel that it is a fairly easy language to understand and won't make you brick it before you've even begun. Later on I will add a post telling you that I have editted the main document of this tutorial to also include C++ or C. This may take a little while longer, but it will be placed there for those of you learning or remembering the C or C++ languages. I have not put any code in straight away so that you may understand the principles without delving too far into programming too quickly.

So, lets make a start on this tutorial...


What is Object-Oriented Programming?

Programming can be split into two general groups: Procedural and Object-Oriented. Throughout this tutorial I will use "OOP" for Object-Oriented Programming and "PP" for Procedural Programming. The difference between the two is that one focuses on the "objects" that are to be dealt with and the other focuses on the "actions" or "logic".

As the names suggest, Object-Oriented programming focuses around the "objects" and Procedural focuses on the "actions". In Object-Oriented programming, what you really want to know about is the "object", rather than what is needed to manipulate it.



Why would Object-Oriented Programming be used?

OOP is generally used because it makes the programming that much more manageable. In teams of programmers, each team can work on a specific "object". In PP, it'd be a lot harder to divide the work up into teams and is probably better for a solo effort or person to manage.

OOP can also be a lot faster to produce programs, this because of the less instances where "inaccuracies" or "errors" may be found in the coding or file, whereas with PP the files are generally a lot longer and therefore have a higher chance of holding these "inaccuracies" or "errors". With OOP you can search for the relevant file and filter through it far more easily than you can PP to find these, because they are that much smaller.

You will find such things as "Classes" in OOP which you would not in most cases of PP. Classes will only contain data that is relevant to it, and so programs will not have to sift through files that are not responsible for the data or are not meant to be used. This will limit data corruption and can heighten the security of the system. The data is described as "hidden". These "Classes" are also not just reusable by the program that you have created, but by other programs that involve them. This means that you can create something that can be used by multiple different programs, thus saving a large bundle of time.



What popular examples of Programming Languages use Object-Oriented Programming?

Now we're making good progress, and you're about to find some information that will encourage you to learn more OOP. These popular examples use OOP:
- Java
- C++
- Python
- VisualBasic.NET
- Ruby

What do you know about these programming languages? For starters you should probably know that some of these are what most programmers use today to create brilliant games for your computer. Yes, the games you know and love.

How many of you have played Minecraft? Did you know that it is entirely coded in Java? And how many of you possess a copy of 3D games such as Mass Effect and Mass Effect 2, particularly on the computer? These games would have been coded in C or C++, much like LITTLE FIGHTER 2!



What is an Object?

Time for us to get into the nitty gritty of the tutorial, the actual important part. If you look around you right now, you will find plenty of objects around you. Tables, chairs, the TV, your computer, all of these are objects.

Pretty simple right? Now lets dig a little deeper and progress into an object...

Objects in the real world have something known as a "state" and a "behaviour". I'm now going to use your computer as an example. The "states" your computer might have are; Make, colour, model, style (laptop or desktop), on, off, current brightness, current volume. Now, what "behaviours" does it have? Here are a few examples of behaviours; Turn on, turn off, increase brightness, decrease brightness, increase volume, decrease volume.

Now look about your environment and list 3 objects, with some of their states and with some of their behaviours. This should help you get to terms with objects.

In terms of software, these ideas in OOP are exactly the same as in the Real World. They have a state and they also have behaviours. An objects state is stored in "fields" or "variables" (depending on what programming language you use) and it's behaviour in "methods" or "functions" (also depending on what programming language you use). These "methods" or "functions" perform or "operate" on a programs state to change or modify it.



What is a Class?

To sum up classes in a fairly simple and easy to understand word, a class is a "Blueprint" from which objects are created. There are many different variations of the same object in the world, for example bikes. Every bike is made out of the same base components; gears, pedals, wheels, seat and frame.

Because of the variance of these components between different bikes, your bike (as an object) is an "instance" of the class of bikes. It is just one in a wide variety of bikes.

Now I'm going to show you a basic example of a Class, without going indepth into the coding of it for now:

    JAVA-Code:
class Bike {
 
       int gear = 1;
       int speed = 0;
 
       void changeGear(int newValue) {
            gear = newValue;
       }
 
       void speedUp(int increment) {
            speed = speed + increment;   
       }
 
       void applyBrakes(int decrement) {
            speed = speed - decrement;
       }
 
       void printStates() {
            System.out.println(" speed:"+speed+" gear:"+gear);
       }
}


This is a Class. As you can imagine and see, the "speed" and "gear" are the bike's state or variables and the "changeGear", "speedUp" and "applyBrakes" are the behaviours or methods after interaction with the outside world.

For those of you that understand principles of coding, you will notice there is no 'main' included. You will also understand the reason why, considering it is a Class. However for those of you that don't normally delve into programming, or this is more a first time for you, the 'main' method seen in most programs is of severe importance. It is where the program begins reading the code and also where other methods are stored. As the Class is a "Blueprint" for the bike, it can be used in an application, but isn't needed. The actual "Use" of a bike belongs in a "Class".


Well, I told you that this would be a rather small tutorial for me. You won't have to scroll down the page so much and you should have found this tutorial fairly easy to understand.

You should probably look out for the next tutorial on Object-Oriented Programming that is coming soon. It will cover "inheritance" and then will move you into some basic exercises for OOP in which you can actually write some code yourself. It will then go a fair bit more in-depth into the programming side so you can find what you're looking for within program examples or for personal use in developing programs.

I hope this tutorial has been a fair bit insightful for you all and managed to break programming down into a lot easier bitesize pieces. Unfortunately this is all for me today, I will post up questions for you to answer and more exercises early on next week (Monday more than likely) and will try to finish up the next tutorial for you all.



Have fun everyone,

Eddie
One day, I shall become, TUTORIAL-MAN: Superhero of writing overly long, overly annoying tutorials which most people probably won't read, but will give it a stab at the first 5 lines!
Reply
Thanks given by: John Fighterli
#2
the tut is very helpful to newcomers who were interested but were afraid after looking into the language ^^, though id like u to increase the explanation of the pic some ppl dunno what they shld do with (f.e"int").
the rest is easy to understand. i already had some experience in school about delphi though i didnt understand them but it worked =p
this tut will surely help me, but the only thing that bugs me is that i want each thing explained, sure it is hard work for u cause u expect us to know the little things but rlly some ppl dont ^^
Say Anything - Alive with the glory of love

My first char: a rudolf mod <--- waiting for comments

LF story reasonable beginning <--clicky (Dont u dare not to leave a comment!)
Story Progress (Click to View)

Fan of Jerry Hawks LF2 Fan movie, just epic animations~
Reply
Thanks given by: Eddie
#3
I'll work on developing this tutorial into a much longer one then on Monday, going more indepth into each part for you and others.
Thank you for the feedback, I'll extend it into variables and their types and such.
Usually get the complaint about having TOO MUCH information, but that's acceptable, not enough isn't however. So for Monday you'll see a longer article that'll actually go into the program itself and the different sections of analysis.

Hope this will help you further,
Eddie
One day, I shall become, TUTORIAL-MAN: Superhero of writing overly long, overly annoying tutorials which most people probably won't read, but will give it a stab at the first 5 lines!
Reply
Thanks given by: John Fighterli
#4
C&P from some text book? This was a year ago, and I'm pretty sure I may have been hungover during my first software development lecture, but I'm sure the first lecture used the same thing you said word by word. I'm 100% sure about the bike example, because I remember thinking that riding a bike takes effort and I hate effort.
[Image: doty7Xn.gif]

10 ʏᴇᴀʀs sɪɴᴄᴇ ɪʀᴄ ɢᴏᴏᴅ.ɪ ᴡᴀʟᴋ ᴛʜʀᴏᴜɢʜ ᴛʜᴇ ᴇᴍᴘᴛʏ sᴛʀᴇᴇᴛs ᴛʀʏɪɴɢ ᴛᴏ ᴛʜɪɴᴋ ᴏғ sᴏᴍᴇᴛʜɪɴɢ ᴇʟsᴇ ʙᴜᴛ ᴍʏ ᴘᴀᴛʜ ᴀʟᴡᴀʏs ʟᴇᴀᴅs ᴛᴏ ᴛʜᴇ ɪʀᴄ. ɪ sᴛᴀʀᴇ ᴀᴛ ᴛʜᴇ sᴄʀᴇᴇɴ ғᴏʀ ʜᴏᴜʀs ᴀɴᴅ ᴛʀʏ ᴛᴏ sᴜᴍᴍᴏɴ ᴛʜᴇ ɢᴏᴏᴅ ɪʀᴄ. ɪ ᴡᴀᴛᴄʜ ᴏᴛʜᴇʀ ɪʀᴄ ᴄʜᴀɴɴᴇʟs ʙᴜᴛ ɪᴛ ɪs ɴᴏ ɢᴏᴏᴅ. ɪ ᴘᴇsᴛᴇʀ ᴢᴏʀᴛ ᴀɴᴅ ᴛʀʏ ᴛᴏ ʀᴇsɪsᴛ ʜɪs sᴇxɪɴᴇss ʙᴜᴛ ɪᴛ ɪs ᴀʟʟ ᴍᴇᴀɴɪɴɢʟᴇss. ᴛʜᴇ ᴇɴᴅ ɪs ɴᴇᴀʀ.ɪ ᴛʜᴇɴ ᴜsᴜᴀʟʟʏ ʀᴇᴀᴅ sᴏᴍᴇ ᴏʟᴅ ɪʀᴄ ʟᴏɢs ᴀɴᴅ ᴄʀʏ ᴍʏsᴇʟғ ᴛᴏ sʟᴇᴇᴘ.


Reply
Thanks given by:
#5
eeeeeeeeeeeeeeeeeepic

Mind reposting it in the minecraft-forum tutorial section for modding? Extremely helpful for Codenabs.
My Creations: (Click to View)

Return (String) System.getNewsOfTheDay();
Barely active, expect slow responses. If at all.


Greetz,
Alblaka
Reply
Thanks given by:
#6
(07-29-2011, 04:25 PM)Lord Silva Wrote:  C&P from some text book? This was a year ago, and I'm pretty sure I may have been hungover during my first software development lecture, but I'm sure the first lecture used the same thing you said word by word. I'm 100% sure about the bike example, because I remember thinking that riding a bike takes effort and I hate effort.

rlly? my teacher tought me with the same example one year ago =p it must be a rlly good example, or like u said C&P.
But it is rlly helpful either way ^^
Say Anything - Alive with the glory of love

My first char: a rudolf mod <--- waiting for comments

LF story reasonable beginning <--clicky (Dont u dare not to leave a comment!)
Story Progress (Click to View)

Fan of Jerry Hawks LF2 Fan movie, just epic animations~
Reply
Thanks given by:
#7
Meh, why do you all use practical examples? We used polygons that have sides and vertices and points etc. :p

Either way, very nice tut, Eddie. Keep it up! :)
Silverthorn / Blue Phoenix
~ Breaking LFE since 2008 ~

"Freeze, you're under vrest!" - Mark, probably.

» Gallery | » Sprites | » DeviantArt
Reply
Thanks given by:
#8
(07-30-2011, 10:20 AM)Blue Phoenix Wrote:  Meh, why do you all use practical examples? We used polygons that have sides and vertices and points etc. :p

Either way, very nice tut, Eddie. Keep it up! :)

cause we r the "practical"* generation =p

since my teacher sucked, and couldnt explain it properly to about 50% of our class i hope to achieve
the sense of logic in this whole programming thing xD
If Eddie hadnt post a new TuT (where i can ask for explanation) i would have just forgot the whole thing and picked another subject =p





*Lazy
Say Anything - Alive with the glory of love

My first char: a rudolf mod <--- waiting for comments

LF story reasonable beginning <--clicky (Dont u dare not to leave a comment!)
Story Progress (Click to View)

Fan of Jerry Hawks LF2 Fan movie, just epic animations~
Reply
Thanks given by:
#9
Was taught the bicycle thing by my old teacher.
Looked into it now though, there isn't any C&P. Just written from some of my old notes if you'd like a photo and such?
:P

Still, can see what you mean - does seem a bit samey as any other article. Maybe I should redo the work as I am now? :P
Gimme a day or two and you can then re-slam it for me.

Bike example + Java = Common.
Would you prefer a different example? I'll do it that way. :D


P.S - Thank you everyone for the comments :P


Eddie
One day, I shall become, TUTORIAL-MAN: Superhero of writing overly long, overly annoying tutorials which most people probably won't read, but will give it a stab at the first 5 lines!
Reply
Thanks given by: John Fighterli




Users browsing this thread: 2 Guest(s)