Salesforce coding lessons for the 99%
Finally, Apex tutorials for point-and-click admins! Written by a self-taught Google engineer.
  • Beginner Tutorials
    • Apex
    • Certifications
    • Career Info
    • Technical Architect
    • Visualforce
    • Videos
  • Apex Academy
  • Success Stories
  • About Me
  • Misc
    • Mailbag
    • Challenges
    • Links
    • Login to my Org
Follow @dvdkliuor SUBSCRIBE!

How to use objects in code

November 6, 2014

Preface – This post is part of the Object Oriented Thinking series.

“Many Bothans died to bring us this information.”

“The enemy battlestation is heavily shielded and carries a firepower greater than half the starfleet. Its defenses are designed around a direct large-scale assault. A small, one-man fighter should be able to penetrate the outer defense. The Empire doesn’t consider a small, one-man fighter to be any threat…”

“The target area is a small thermal exhaust port only two meters wide. Only a precise hit with a proton torpedo will start a chain reaction which should destroy the station.”

“May the Force be with you…”

**ALL PILOTS, MAN YOUR SHIPS!!**

User lukeSkywalker = [SELECT Id FROM User WHERE Title = 'Jedi'];

// Create Luke's X-Wing using the default constructor
XWing lukesXWing = new XWing();
// Use dot notation to manually set the attributes of the X-Wing
lukesXWing.pilot = lukeSkywalker; lukesXWing.protonTorpedoes = 4; lukesXWing.deathStarsDestroyed = 0; lukesXWing.powerRemaining = 1.00; lukesXWing.shield = new DeflectorShield(); lukesXWing.droid = new AstromechDroid('R2-D2');
// Create a team of X-Wings to help with the battle! List<XWing> redSquadron = new List<XWing>(); List<User> pilots = [SELECT Id FROM User WHERE Title = 'Pilot']; for (User pilot : pilots) { // Use our custom constructor to quickly initiate new X-Wings
XWing x = new XWing('Red ' + pilot.Id, pilot);
redSquadron.add(x); System.debug(x.Name + ' standing by!'); }

**ENTERING HYPERSPACE**

// Use an X-Wing method and store its return value
Boolean isSuccess = lukesXWing.initiateHyperdrive();
if (isSuccess != true) { System.debug('I have a bad feeling about this...'); } for (XWing x : redSquadron) { // You don't always have to use the return value!
x.initiateHyperdrive();
}

Silence.

**FIGHTERS ABOVE YOU, COMING IN!!**

**RED SIX IS UNDER HEAVY FIRE!!**

// Use dot notation to traverse relationships
redSquadron[5].shield.decreaseShields(0.50); redSquadron[5].shield.decreaseShields(0.30);
System.debug('BAROOM!!'); redSquadron.remove(5);

**LOCK S-FOILS INTO ATTACK POSITION!!**

“Luke, trust your feelings.”

lukesXWing.shootLasers(2); lukesXWing.shootLasers(4); lukesXWing.shootLasers(10);
System.debug('ZREEEET!');

**THIS IS RED LEADER, WE’RE BEGINNING OUR ATTACK RUN!!**

“Several fighters have broken from the main group.”

“Come with me.”

**I CAN’T SHAKE HIM – I’M HIT BUT NOT BAD!!**

lukesXWing.shield.decreaseShields(0.40);

**R2, SEE WHAT YOU CAN DO ABOUT IT!!**

lukesXWing.repair(); System.debug(lukesXWing.shield.capacity); // 1.00, baby!

**WE’RE ALMOST IN RANGE OF THE EXHAUST PORT!!**

**ALMOST THERE!!**

**ALMOST THERE!!**

**IT’S NO GOOD, WE CAN’T MANEUVER!!**

**THIS IS IT!!**

redSquadron.remove(0);
redSquadron.remove(3);
redSquadron.remove(2);
redSquadron.clear();

**RED SQUADRON HAS BEEN WIPED OUT!!**

**LUKE YOU’RE OUR ONLY HOPE!!**

“I’m on their leader.”

“Use the Force, Luke.”

“The Force is strong with this one.”

**LUKE YOUR TARGETING COMPUTER IS OFF!!**

**WHAT’S WRONG!!**

**NOTHING, I’M ALRIGHT!!**

“I have you now…”

“What?!”

**YEAHOO! YOU’RE ALL CLEAR KID!!**

**NOW LET’S BLOW THIS THING AND GO HOME!!**

lukesXWing.fireProtonTorpedo();

// Don't forget the test class!
System.assertEquals(1, lukesXWing.deathStarsDestroyed);

Next post: A practical example of using objects in code!

10 Comments
Anusha
May 4, 2021 @ 4:10 pm

Too good and pretty clear explination thank you so much

Reply
Harish
June 4, 2019 @ 2:41 am

You were unlimited buddy.

Awsome tutorial.

Reply
Anonymous
November 5, 2017 @ 9:51 pm

Awesome Man…!

Reply
Robert
September 15, 2017 @ 5:12 pm

This is the best explanation of using OOP concepts ever. You win the Internet.

Reply
    David Liu
    September 17, 2017 @ 7:55 pm

    YAY!!!!

    Reply
VISHNU J MENON
November 22, 2016 @ 10:14 pm

Amazing narrative and a superb fun way towards OOP concepts. Thanks David for putting up the time and effort for this. I just wanna point out a little thing though. I couldn’t find out repair() method in XWing class. So maybe R2 could not increase the shield capacity, Luke couldn’t blow up the Death Star resulting the mission and test class ending up as a total failure. :)

Reply
    David Liu
    November 22, 2016 @ 10:16 pm

    LOL!!!! Bravo!!!

    Reply
Rafael Sanchez
January 14, 2015 @ 12:47 pm

I love it, hahaha

Reply
Katie
November 16, 2014 @ 10:23 pm

Zreeeeeet! :)

Reply
Jan Vandevelde (@jan_vdv)
November 6, 2014 @ 11:54 pm

Love the analogy you are using. Keeps it light and funny to make sense of it all ;-) Can’t wait for the next post, looks like it’s gonna be a “lightsaber”!

Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


*

*

Theme: Simple Style by Fimply