Saturday, 25 August 2012

Java programming lesson #7 - Object-Oriented Programming(OOP)


Object oriented programming or OOP is a way of writing programs using objects. An object is a data structure in memory that has attributes and methods. The attributes of an object are the same as variables and the methods of an object are the same as functions or procedures.

The reason for using objects instead of the old procedural method of programming is because objects group the variables and methods about something together instead of keeping them all apart as in procedural programming. There are many other reasons why OOP is better which you will learn about later.

Using classes and objects

Before you can create an object you need to create a class. A class is the code for an object and an object is the instance of a class in memory. When you create an object from a class it is called instantiating the object. To help you understand think of the plan for a house. The plan for the house is like the class and the house that will be built from the plan is the object.

Creating a class

You have already been creating classes in the programs you have written so far. To show you how they work we will need to create a separate class in a separate file. This class will be called Student.

public class Student
{
}

Now we will add 2 variables to the class which are the student's name and student number.

public class Student
{
   String studentName;
   int studentNumber;
}

Next we must add methods for getting and setting these variables.

public class Student
{
   String studentName;
   int studentNumber;

   public void setStudentName(String s)
   {
      studentName = s;
   }

   public void setStudentNumber(int i)
   {
      studentNumber = i;
   }

   public String getStudentName()
   {
      return studentName;
   }

   public int getStudentNumber()
   {
       return studentNumber;
   }
}

Instantiating an object

Now that we have finished writing the class we must create the main program that will instantiate the object. We will call the main program class TestClass. It should look just like how every other java program starts off.

public class TestClass
{
   public static void main(String[] args)
   {
   }
}

Now we will instantiate the object. To do this we declare a reference to the object called stu and set it equal to a newly created object in memory.

public class TestClass
{
   public static void main(String[] args)
   {
      Student stu = new Student();
   }
}

Using the object

Now we can call the methods from the stu object to set the values of its variables. You can't set the values of the variables in an object unless they are declared as public. This makes it safer to work with variables because they can't be changed by mistake by another object.

public class TestClass
{
   public static void main(String[] args)
   {
      Student stu = new Student();
      stu.setStudentName("John Smith");
      stu.setStudentNumber(12345);
      System.out.println("Student Name: " + stu.getStudentName());
      System.out.println("Student Number: " + stu.getStudentNumber());
   }
}

Constructors

A constructor is a method that is run when an object is instantiated. Constructors are useful for initializing variables. The constructor is declared with only the name of the class followed by brackets. Here is an example of the Student class that initializes both student name and student number.

public class Student
{
   String studentName;
   int studentNumber;

   Student()
   {
      studentName = "No name";
      studentNumber = 1;
   }

   public void setStudentName(String s)
   {
      studentName = s;
   }

   public void setStudentNumber(int i)
   {
      studentNumber = i;
   }

   public String getStudentName()
   {
      return studentName;
   }

   public int getStudentNumber()
   {
       return studentNumber;
   }
}

You can also pass parameters to a constructor when you instantiate an object. All you need to do is add the parameters in the brackets after the constructor declaration just like a normal method.

public class Student
{
   String studentName;
   int studentNumber;

   Student(String s, int i)
   {
      studentName = s;
      studentNumber = i;
   }

   public void setStudentName(String s)
   {
      studentName = s;
   }

   public void setStudentNumber(int i)
   {
      studentNumber = i;
   }

   public String getStudentName()
   {
      return studentName;
   }

   public int getStudentNumber()
   {
       return studentNumber;
   }
}

Now all you need to do is put the parameters in the brackets when you instantiate the object in the main program.

public class TestClass
{
   public static void main(String[] args)
   {
      Student stu = new Student("John Smith",12345);
      System.out.println("Student Name: " + stu.getStudentName());
      System.out.println("Student Number: " + stu.getStudentNumber());
   }
}

0 comments:

Post a Comment