Coming up Constructors Overloading With one parameter With two parameters Overloading Constructors and methods
Lecture 5 Objects Ahoy OO
Coming up Constructors Overloading With one parameter With two parameters Overloading Constructors and methods
Constructors OO Look a bit like methods: Arguments No return type public class DVD{ public DVD(){ } Arguments No return type Same name as class
What are they used for? OO The constructor contains the code that is run when you create an object The constructor runs before the object is assigned to a reference It lets you set it up for use
Why not just use set methods? Okay, so lets make a Table (one that you’d eat at) OO
public class Table{ int size; public void sitAt(int numOfPeople){ //Sit some people at the table } public void setSize(int theSize){ size = theSize; ----------------------------------- public class UseTheTable{ public static void main(String[] args){ Table t = new Table(); t.sitAt(8); t.setSize(3); What happens if....
The problem OO The object creation is two step Make the object Set the size If someone else uses your class Will they remember? Or will there be 8 people trying to eat off a coffee table?
It’s worse OO When you declare a data type It defaults to its default value... Numbers default to 0 Boolean defaults to false Objects default to null So the last example would have 8 people seated at a table of size 0!
These constructors seem pretty important.... OO These constructors seem pretty important.... So why haven’t I used/heard of them before? You have. Table t = new Table(); new calls the constructor
But I didn’t write any constructors when I called new! Java helpfully puts in a blank constructor for you.
public class Table{ int size; public void sitAt(int numOfPeople){ //Sit some people at the table } public void setSize(int theSize){ size = theSize; public Table(){ Java’s helpful hidden constructor
So now we know its there... .....We can use it Set the table size to a small table when it is created
public class Table{ int size; public void sitAt(int numOfPeople){ //Sit some people at the table } public void setSize(int theSize){ size = theSize; public Table(){ size = 1;
Or we can pass it an argument....
public class Table{ int size; public void sitAt(int numOfPeople){ //Sit some people at the table } public void setSize(int theSize){ size = theSize; public Table(int sizeToSet){ size = sizeToSet;
Or two arguments...
Uh oh... They have the same name... public class Table{ int size; String wood; public void sitAt(int numOfPeople){ //Sit some people at the table } public void setSize(int theSize){ size = theSize; public Table(int sizeToSet){ size = sizeToSet; public Table(int sizeToSet, String woodType){ wood = woodType; Uh oh... They have the same name...
That can’t work, can it? OO How would the program know which method I wanted to call? They both have the same name Actually it does work You can do the same thing with methods
public void sitAt(int numOfPeople){ numOfSitters = numOfPeople; } public class Table{ int size; String wood; String partyName; int numOfSitters; public void sitAt(int numOfPeople){ numOfSitters = numOfPeople; } public void sitAt(int num, String name){ partyName = name public void setSize(int theSize){ size = theSize; public Table(int sizeToSet){ size = sizeToSet; public Table(int sizeToSet, String woodType){ wood = woodType;
Compiler error! Which method is called? OO If you call someTable.sitAt(4); If you call someTable.sitAt(4, “Martin”); If you call someTable.sitAt(); public void sitAt(int numOfPeople){ numOfSitters = numOfPeople; } public void sitAt(int num, String name){ partyName = name Compiler error!
This is called OO Overloading A method is recognised by its return type and its signature ie the stuff in the brackets, like (int, String)
Coming up Constructors Overloading With one parameter With two parameters Overloading Constructors and methods
Overloading Rules OO Return types can be different As long as the arguments are different too i.e. You can’t just change the return type You can change the access levels to more or less restrictive i.e. You can change public to private, and vice versa Overloading is different to another principle, overiding, which we cover in inheritance.