• We just launched and are currently in beta. Join us as we build and grow the community.

Object Oriented Programming (Code)

shady1995

Keyboard Warrior
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
168
Likes
199
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Introduction:
Hello and welcome to a tutorial on Object Oriented Programming, with code examples as opposed to theory.

Ready the theory page
Before starting this page make sure you have read and understand the theory page which can be found here.

Instances
The use of OOP is to have multiple instances of one class running at the same time, each with it's own variables and methods. For the tutorials below I am going to use an example of a worker/student database where user information gets entered to a database.

Examples:
So, first we need to create a class where we want to run the instances of our OOP from as well as the OOP class which will contain the variables and methods of our OOP variable types...

Main class:
  1. public

    class

    Main {
  2. public

    static

    void

    main(

    String

    args[

    ]

    )

    {
  3. //This is ran first out of everything. It's also commented out by the front two "//" so this line doesn't get ran.
  4. }
  5. }

OOP class:
  1. public

    class

    Student {
  2. private

    String

    firstName;
  3. private

    String

    lastName;
  4. private

    int

    age;
  5. public

    Student(

    String

    fName, String

    lName, int

    studentAge)

    {
  6. this

    .firstName

    =

    fName;
  7. this

    .lastName

    =

    lName;
  8. this

    .age

    =

    studentAge;
  9. }
  10. }

So now we are going to create a few user variables using our "Student" OOP class. In our constructor for our "Student" class we accept the arguments; First Name, Last Name and Age, so we must write these when creating our new variables...

  1. public

    class

    Main {
  2. public

    static

    void

    main(

    String

    args[

    ]

    )

    {
  3. //This is ran first out of everything. It's also commented out by the front two "//" so this line doesn't get ran.
  4. Student first =

    new

    Student(

    "John"

    , "Qwop"

    , 34

    )

    ;
  5. Student second =

    new

    Student(

    "Sam"

    , "Sail"

    , 16

    )

    ;
  6. Student third =

    new

    Student(

    "Lisa"

    , "Porter"

    , 23

    )

    ;
  7. }
  8. }

Now we have created some variables through OOP. We can now use the methods created in our OOP ("Student") class while keeping them separate. To show this, here is a custom method in the "Student" class which output the information to the console of the student:

  1. public

    void

    outputDetails(

    )

    {
  2. System

    .out

    .println

    (

    "This student is "

    +

    this

    .firstName

    +

    " "

    +

    this

    .lastName

    +

    " and is "

    +

    this

    .age

    +

    " years of age."

    )

    ;
  3. }

Here is the use of that method from our Main class on all created students...

  1. first.outputDetails

    (

    )

    ;
  2. third.outputDetails

    (

    )

    ;

And here is the output in the console once the program has finished running...

This student is John Qwop and is 34 years of age.
This student is Lisa Porter and is 23 years of age.

Finished!

Book traversal links for Object Oriented Programming (Code)

  • ‹ Multi-Dimensional Arrays
  • Up
  • Object Oriented Programming (Theory) ›

 

442,401

317,942

317,951

Top