Presentation is loading. Please wait.

Presentation is loading. Please wait.

Coming up Constructors Overloading With one parameter

Similar presentations


Presentation on theme: "Coming up Constructors Overloading With one parameter"— Presentation transcript:

1 Coming up Constructors Overloading With one parameter
With two parameters Overloading Constructors and methods

2 Lecture 5 Objects Ahoy OO

3 Coming up Constructors Overloading With one parameter
With two parameters Overloading Constructors and methods

4 Constructors OO Look a bit like methods: Arguments No return type
public class DVD{ public DVD(){ } Arguments No return type Same name as class

5 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

6 Why not just use set methods?
Okay, so lets make a Table (one that you’d eat at) OO

7 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....

8 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?

9 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!

10 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

11 But I didn’t write any constructors when I called new!
Java helpfully puts in a blank constructor for you.

12 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

13 So now we know its there... .....We can use it
Set the table size to a small table when it is created

14 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;

15 Or we can pass it an argument....

16 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;

17 Or two arguments...

18 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...

19 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

20 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;

21 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!

22 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)

23 Coming up Constructors Overloading With one parameter
With two parameters Overloading Constructors and methods

24 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.


Download ppt "Coming up Constructors Overloading With one parameter"

Similar presentations


Ads by Google