Email Subscription Form

Tuesday, April 9, 2013

Introduction to Java: Object-Oriented Programming

Java is one of the most commonly used programming languages, and it works well with Webdriver and JQuery, which we will be using in our automated tests.  Java is an Object-Oriented Programming (OOP) Language.  You may have had exposure to OOP languages in the past, but if not, the concept may seem a little foreign at first.  Having grown up before OOP, I had a particularly tough time with it.  I hope that the explanation below will make it easier on you!

Think about the tasks that you do when you get ready in the morning.  You might: exercise, shower, and get dressed.  If you were writing a program, or class, to tell a robot to do these things, you would probably write a little subroutine, or method, for each task.  For example:

public class getReady {  //this is the class

exercise();  //this calls the exercise method
shower();
getDressed();

public void exercise() {  //this is the exercise method
pushups;
situps;
squats;
}

public void shower() {  //this is the shower method
get in shower;
lather;
rinse;
repeat;
}

public void getDressed() { //this is the get dressed method
put on shirt;
put on pants;
put on shoes;
}

}

Now consider your friend, who exercises and showers at night instead of in the morning.  His nighttime routine is: exercise, shower, put on pajamas.  If you were programming a robot to do your friend's nighttime activities, it might look like this:

public class getReadyForBed {  //this is the class

exercise();
shower();
putOnPajamas();  //this calls the put on pajamas method


public void exercise() {
pushups;
situps;
squats;
}


public void shower() {
get in shower;
lather;
rinse;
repeat;
}

public void putOnPajamas() {  //this is the put on pajamas method
put on pajama top;
put on pajama bottoms;
}

}

Notice that the two classes have two methods that are exactly the same.  Why not share the code instead of copying and pasting it into each class?  That way if you ever need to make a change to your exercise method, you will only have to change it once.  Let's make a base class that contains all the methods we might need:

public class Ready {    //this is our base class


public void exercise() {
pushups;
situps;
squats;
}

public void shower() {
get in shower;
lather;
rinse;
repeat;
}

public void getDressed() {
put on shirt;
put on pants;
put on shoes;
}


public void putOnPajamas() {
put on pajama top;
put on pajama bottoms;
}



}

Now, we can change our two programs to call upon the existing methods instead of copying and pasting them:

public class getReady extends Ready {  //this is your routine


exercise();
shower();
getDressed();

}



public class getReadyForBed extends Ready {  //this is your friend's routine

exercise();
shower();
putOnPajamas();

}

See how much space this saves?  There are other reasons why OOP is a good idea, but the main thing to understand, in my opinion, is that OOP saves you from having to copy and paste code.






5 comments:

  1. This is the best explanation I've been able to find to this day! This has been so helpful!

    ReplyDelete
    Replies
    1. I'm so happy that you found this so helpful, Carla! :-)

      Delete
  2. This is the most simplified explanation I've ever seen of OOP.

    ReplyDelete
  3. Normaly Java is use on Mobile stages, web based application, well known for big business application advancement. Is Java just promotion? Is it extremely pervasive? Is each Java application advancement organization solid? more info

    ReplyDelete

New Blog Location!

I've moved!  I've really enjoyed using Blogger for my blog, but it didn't integrate with my website in the way I wanted.  So I&#...