programming, technology

How to think in objects and classes – Java –Part 1


Object Oriented Programming (OOP)

You may be a bit familiar with sequential programming and even the use of functions but OOP is a different concept all together. You need to change how you see things. You need to change your paradigm with this one and that’s what this article seeks to help you do.

I’m using Java because that’s the language that taught me OOP. This article is for those who want to grasp the concept. It’s about changing how you think about programming. You are not Just going to write a bunch of instructions and click “run” and just hope it runs from top to bottom without errors.

Objected Oriented Programming? What is that?

Okay so let’s define it our own way. We like to think differently, think in objects and classes. OOP means instead of writing raw instructions (or using written functions), we are going to confine our functions and variables. We are going to organize our code into bigger chunks than functions could ever do. We are going to store our functions and variables in “Classes”. Keep these two terms in mind: Classes, Objects.

Classes

A class is a pattern from which objects are created. Picture a class to be species. So for instance all sapiens are humans but each human is different — that’s an object.

 

This is an empty class but don’t worry, we will add some flesh to it soon but before we go on, I just want to make sure what we have there is clear. (We need to do a bit of java before we concentrate on the OOP).

If you’re using an IDE such as eclipse or NetBeans then most likely you’ll get a starter code to make things easier.

Basics

From the image above, the name of our class is JavaApplication1 and that’s also the name of the source file. Always remember this: The name of your file must match the name of your class and also follow the convention of using camel case for class names. Example: ThisIsTheNameOfTheClass.

This class has one method called main. Usually you may have multiple files having classes in them but one of them must have this method. This is the method that actually runs and sets the application in motion.

Enough talking, let’s do some object oriented programming. Create another class file in the same package as the previous class and paste this code in it.


package javaapplication1;
public class Person {
public String first_name;
public String surname;
public String location;

public void walk(){
System.out.println("Walking...");
}

public void talk(){
System.out.println("Hello there!");
}
}

This class is a pattern for creating person objects. So you can picture this class as a factory that makes “people” lol. An object created from this class will have three attributes namely: first_name, surname and locations. Think of these as details of a person in real life. The class also has two methods namely: walk and talk. These are actions that an object from this class can perform just as people can walk and talk.

Demonstration with objects

Now, let’s start with objects. Let’s create objects of this class and run it in the main function of the previous class. Remember main() is where it all happens.


package javaapplication1;

public class JavaApplication1 {

//this is a class in java

public static void main(String[] args) {

//        System.out.println("Hello world");

Person person1 = new Person();
person1.first_name = "John";
person1.surname = "Johnson";
person1.location = "Accra";
//we've given person1 some details

System.out.println("First Name: "+person1.first_name);
System.out.println("Surname: "+person1.surname);
System.out.println("Location: "+person1.location);

//let's walk and talk
person1.walk();
person1.talk();
}
}

Note that both classes are in the same package so we can just create a new object in the main. This person has been given a first name, surname and a location. Run the program and see the outcome. We can go ahead and create a different object from the same class with different details but that object will have the same attributes and methods; the attributes can be set to a different string. The actions are the same for every object. Every person talks and walks.

Don’t just say “hello there”

What if we could make a person anything we wanted them to say and not just “hello there” ? Maybe we could make a person say they’re name. Let’s get a bit creative and confident 🙂

A little modification to the Person class.


package javaapplication1;

public class Person {
public String first_name;
public String surname;
public String location;

public void walk(){
System.out.println("Walking...");
}

public void talk(String what_to_say){
System.out.println(first_name+" says: "+what_to_say);
}
}

And then we do this in the main 🙂


package javaapplication1;

public class JavaApplication1 {

//this is a class in java

public static void main(String[] args) {

//        System.out.println("Hello world");

Person person1 = new Person();
person1.first_name = "John";
person1.surname = "Johnson";
person1.location = "Accra";
//we've given person1 some details

Person person2 = new Person();
person2.first_name = "Sophie";
person2.surname = "Sawyer";
person2.location = "England";

//let's walk and talk
person1.walk();
person1.talk("Hi there.. what's your name");

person2.talk("Hey, I am "+ person2.first_name +" "+person2.surname);
person2.talk("You?");

person1.talk("Call me "+ person1.first_name );
}
}

Don’t stop here. Get even more creative with the objects 🙂
Have fun!

Advertisements

6 thoughts on “How to think in objects and classes – Java –Part 1”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s