SE251:StudentClarifications:Lec3

From Marks Wiki
Revision as of 05:21, 3 November 2008 by Mark (Sọ̀rọ̀ | contribs) (1 revision(s))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Lecture 3 - Object Based Programming

Lecture slides

Getters and Setters

Skip this if you want, it's not a clarification. In this course you will be advised to use getter and setter functions that modify and return member variables. It's not widely agreed whether this is good coding practice or not. While it is a good idea to hide member variables behind accessor functions in case the logic changes, having both setters and getters can indicate a design issue. If the classes variables have a lot of setters and getters, then it's just being used to store data like a struct. It's not actually doing anything. There are no hard and fast rules, but if you find you have a class peppered with setThis and getThat, have a think and see if there's a better way of doing things.

Constructors

The constructor is just a method (function) that is called automatically when you create a new object of a particular class. You can use it for any initialisation that needs to be done. Any value that is required for the class to work should be passed into the constructor. You can pass parameters to a constructor like this:

Person bob = new Person(param1, param2, "Bob", 27);

Constructors are required to have the same name as the class they are in.

Method overloading

In java, methods with the same name which take different types of parameters are treated as different methods. When calling a method, java will choose the correct function based on the types of the objects being passed. This is usually used to perform the same function on different inputs. It also allows you to write several constructors (since they're required to have the same name).

This

The this keyword can cause some confusion. First I should cover scope and variable hiding. Scope controls the lifetime of variables, like this:

void Scope() {

 int i = 5;
 System.out.println(i)
 if (1) {
   int i = 10;
   System.out.println(i);
 }
 System.out.println(i);

}

If you run this program, it should print 5, then 10, then 5 again. What I want to focus on is why the 3rd println is 5 again. The variable i inside the if (1) {} block only exists within that block. While it exists, any statements involving i will use that variable, which contains 10. After that block has ended (i.e. after the first }) that variable will be destroyed, so statements involving i will use the old variable which contains 5. Java will always use the newest stack variable available. So the i we created first was hidden by the i we created second, for as long as the second i was in scope.

How does all this apply to 'this'? Well, 'this' is just a slightly special variable. Say you have a Dog object, and you call a method like:

Dog dog = new Dog(); // Create a Dog object. dog.bark(); // Call bark.

That's very similar to in C if you have a struct Dog, and a function bark like:

void bark(Dog this) { }

which you would call like:

bark(dog); // Call bark, passing in my dog variable as the 'this' parameter.

So in Java, it's like every method has a parameter called 'this' which isn't shown. When you call the method on an object (dog.bark()), it's like you're passing that object (dog) into the method as the 'this' parameter. But in java, you don't always have to write this.myVar to access the variable myVar. Java treats all the variables in the 'this' object as being in a scope outside the function. So if you type myVar, and there is no variable called myVar in your method, java will use the variable myVar in the 'this' object. If there is a variable myVar, then the myVar in the 'this' object will be hidden, so you can only access it by typing this.myVar.

Running applications

It was briefly explained how to add a main function to your program. But how then do you run the program? A simple way is to compile it normally, such that you get a MyProgram.class file. Then run it using the command:

java MyProgram.class

in a console.