Presentation is loading. Please wait.

Presentation is loading. Please wait.

FIT1002 2006 1 Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are.

Similar presentations


Presentation on theme: "FIT1002 2006 1 Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are."— Presentation transcript:

1 FIT1002 2006 1 Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are defined and used be able to write “safe” constructors be able to manage interdependent constructors Reading: Savitch, Sec. 4.4 (Constructors)

2 FIT1002 2006 2 Constructors We have used (invoked) constructors all along: Person x = new Person(); constructors are code that is invoked to generate a new object.

3 FIT1002 2006 3 Constructors A constructor is a (pre-)defined behavior that is executed when a new instance is generated. its primary tasks are 1.to allocate a block of memory and 2.to initialize the object

4 FIT1002 2006 4 Default Constructor Every class that does not explicitly declare constructors automatically has a default constructor. This constructor allocates the object and initializes the instance variables according to their declarations.

5 FIT1002 2006 5 Default Constructor public class Person { int age = 99; String name = “nobody”; } Person x = new Person(); x

6 FIT1002 2006 6 Default Constructor public class Person { int age = 99; String name = “nobody”; } Person x = new Person(); age name x

7 FIT1002 2006 7 Default Constructor public class Person { int age = 99; String name = “nobody”; } Person x = new Person(); age name 99 x

8 FIT1002 2006 8 Default Constructor public class Person { int age = 99; String name = “nobody”; } Person x = new Person(); age name 99 “nobody” x

9 FIT1002 2006 9 Default Constructor public class Person { int age = 99; String name = “nobody”; } Person x = new Person(); age name 99 “nobody” x

10 FIT1002 2006 10 Constructors and Initialization public class Person { int age = 99; String name = “nobody”; } Person x = new Person(); public class Person { int age; String name; } Person x = new Person(); x.age = 99; x.name = “nobody”; The following two ways of initializing achieve the same result thanks to the default constructor:

11 FIT1002 2006 11 Constructors and Initialization Person x = new Person(); public class Person { int age; String name; } Person x = new Person(); x.age = 99; x.name = “nobody”; Instead of using initializing declarations for instance variables or initializing them explicitly, we can explicitly define a constructor. The following two versions have exactly the same effect: public class Person { int age; String name; public Person() { this.age = 99; this.name=“nobody”; }

12 FIT1002 2006 12 Constructors and Initialization Person x = new Person(); public class Person { int age; String name; } Person x = new Person(); x.age = 99; x.name = “nobody”; Note that all you have done is to move the explicit initialization code after a call to new (in red) into a constructor declaration. The big advantage is that now every Person object that is generated (with “new”) will automatically be initialized in a consistent way. public class Person { int age; String name; public Person() { this.age = 99; this.name=“nobody”; }

13 FIT1002 2006 13 Constructor Syntax public class Person { int age; String name; public Person() { this.age = 99; this.name=“nobody”; } Constructor Definition Looks very much like a method declaration (but not quite) Is executed every time when “new Person()” is executed Note: the “this” above is purely stylistic and could be dropped

14 FIT1002 2006 14 Constructor Syntax The name of the constructor must be the class name (case-sensitive)! You must not declare a return type! You cannot have a return statement! Constructor definitions should be given before any method definition. public class Person { int age; String name; public Person() { this.age = 99; this.name=“nobody”; }

15 FIT1002 2006 15 Constructor Behaviour A constructor looks very much like a method. It can only be called with a “new” statement. It cannot be called like a normal method. Its body code is executed like a method. Some additional functions are executed automatically: 1.the memory block for the new object is allocated, 2.the object initialization is performed, 3.a reference to the allocated object is bound to “this”, 4.now the body code is executed, 5.the object “this” is returned to the code that called “new”, so that the object can be used in an expression, assignment, etc.

16 FIT1002 2006 16 Default Constructors If a class does not define any constructors, a default constructor is automatically generated. The default constructor performs steps 1-3 and 5 above. When a class declares an explicit constructor, the default constructor is not generated.

17 FIT1002 2006 17 A Trivial Constructor What does this constructor do?? Why? How is it called? Do we still have a default constructor? What is the difference? public class Person { int age=99; String name=“nobody”; public Person() { }

18 FIT1002 2006 18 Declarations Revisited As a general rule, if you want variables initialized, do this in a constructor and not in an initializing declaration. Only this is safe if you are using inheritance. public class Person { int age=99; String name=“nobody”; } public class Person { int age; String name; public Person() { this.age = 99; this.name=“nobody”; }

19 FIT1002 2006 19 Declarations Revisited As a general rule, if you want variables initialized, do this in a constructor and not in an initializing declaration. Only this is safe if you are using inheritance. public class Person { int age=99; String name=“nobody”; } public class Person { int age; String name; public Person() { this.age = 99; this.name=“nobody”; }

20 FIT1002 2006 20 Non-default Constructor constructors can have parameters This is commonly used for initialization parameters the default constructor “disappears” if any constructor is declared, so also in this case. public class Person { int age; String name; public Person( int anAge, String aName ) { this.age = anAge; this.name = aName; }

21 FIT1002 2006 21 Overloading Constructors You can have more than one constructor for the same class This is an overloading situation Which constructor will selected based on the parameters of the “new” public class Person { int age; String name; public Person( int anAge, String aName ) { this.age = anAge; this.name = aName; } public person( int anAge ) { this.age = anAge; this.name = “John Doe”; }

22 FIT1002 2006 22 Overloading Constructors Person p = new Person(41, “Eric de la Morte”); public class Person { int age; String name; public Person( int anAge, String aName ) { this.age = anAge; this.name = aName; } public person( int anAge ) { this.age = anAge; this.name = “John Doe”; }

23 FIT1002 2006 23 Overloading Constructors Person p = new Person(41); public class Person { int age; String name; public Person( int anAge, String aName ) { this.age = anAge; this.name = aName; } public person( int anAge ) { this.age = anAge; this.name = “John Doe”; }

24 FIT1002 2006 24 Overloading Constructors Person p = new Person(); public class Person { int age; String name; public Person( int anAge, String aName ) { this.age = anAge; this.name = aName; } public Person( int anAge ) { this.age = anAge; this.name = “John Doe”; }

25 FIT1002 2006 25 Copy Constructors A useful type of constructor is a “copy constructor”, which generates a cloned instance of a master object. public class Person { int age; String name; public Person( Person masterObject ) { this.age = masterObject.age; this.name = masterObject.name; }

26 FIT1002 2006 26 Copy Constructors A copy constructor can be used to avoid privacy leaks… public class Person { private int age; private String name; private Person partner; public Person getPartner() { Person cloned = new Person(partner); return cloned; } … // other methods }

27 FIT1002 2006 27 Copy Constructors The copy constructor must copy each instance variable individually: public class Person { private String name; private int age; private Person partner; public Person(Person original) { this.name=original.Name; this.age=original.age; this.partner=original.partner; } … public Person getPartner() { return new Person(partner); }

28 FIT1002 2006 28 Cloning Advanced (optional): Copying of an object is required quite often. Generally it is called cloning. The top-class of all objects defines a general clone operation that all our objects inherit and can use (instead of defining a copy constructor). This does exactly the same as our copy constructor. The built-in cloning allocates a new instance and copies all field values. public class Person implements Cloneable { private String name; private int age; private Person partner; … public Person getPartner() throws CloneNotSupportedException { return partner.clone(); } Note: you must include the “implements Cloneable” in the class header and the “throws CloneNotSupprtedException” as well as the cast in the method header wherever you call clone(). (see advanced notes)

29 FIT1002 2006 29 Interfaces Advanced (optional): What does “implements Cloneable” in the class definition actually mean? This indicates that the class “Person” implements a so-called “interface” with the name “Cloneable”. An interface is a defined list of behaviors that a class which implements it must offer. Implementing an interface is like inheriting a number of method headers without implementation of the method. The implementing class must give concrete methods implementations for these (much as if it would override an inherited method). A class can only be a subclass of a single other class, but it can implement several interfaces. Details: Savitch, Chapter 13

30 FIT1002 2006 30 CloneNotSupportedException Advanced (optional): What does “throws …” in the method definition actually mean? Java supports an error handling mechanism, termed “Exceptions”. When a method encounters an error it can “throw” an exception it. It is then aborted and the caller receives the exception (instead of a method result). The caller can test whether an exception occurred and react appropriately. If the calling code does decide not to handle the exception explicitly this must be indicated in the code by a “throws” declaration for the appropriate type of exception in the calling code. we will give brief introduction to exceptions later. For now you can view this just as a piece of syntax… Details: Savitch, Chapter 9

31 FIT1002 2006 31 Tasks of a Constructor General tasks of a constructor are: initializing instance variables checking initialization values public class Person { int age; String name; public Person( int anAge, String aName ) { if (anAge>0) this.age = anAge; else age=0; // we can do better than this --> later this.name = aName; }

32 FIT1002 2006 32 What to do if things go wrong Surely setting an instance variable to an arbitrary value if the initialization value passed to the constructor is invalid is not a good way. You cannot explicitly return anything from the constructor, so how can you signal a problem?

33 FIT1002 2006 33 What to do if things go wrong One solution is to print an error and abort the program by calling “System.exit(0)”: public class Person { int age; String name; public Person( int anAge ) { if (anAge>0) this.age = anAge; else { System.out.println(“Invalid Age”); System.exit(0); } treat this as syntax

34 FIT1002 2006 34 What to do if things go wrong (II) A better way is to signal an error that can be recognized and handled by other parts of the program. We will come back to this in detail in a separate lecture (Wk 11: I/O & Exceptions): public class Person { int age; String name; public Person( int anAge ) { if (anAge>0) this.age = anAge; else { throw new Error(“Invalid Age”); } Error Message (String) treat this as syntax

35 FIT1002 2006 35 Calling other methods in a constructor a constructor can call other methods public class Person { int age; String name; public Person( int anAge ) { if ( ageValid(anAge) ) this.age = anAge; else { throw new Error(“Invalid Age”); } public boolean ageValid( int age ) { return age>0; }

36 FIT1002 2006 36 Calling other constructors Constructors can call constructors of other classes public class Couple { Person partner1; Person partner2; public Couple(String name1, String name2, int age1, int age2 ) { partner1 = new Person(age1, name1); partner2 = new Person(age2, name2); } … // other methods }

37 FIT1002 2006 37 Calling other constructors Constructors can call other (overloaded) constructors in the same class. The following has the same effect as the previously declared two- argument constructor: public class Person { int age; String name; public Person( int anAge ) { if (anAge>0) this.age = anAge; else throw new Error(“Invalid Age”); } public Person( int anAge, String aName ) { this(anAge); this.name = aName; } A constructor in the same class is called as this(… parameters…)

38 FIT1002 2006 38 Advice on Constructors always write constructors for your classes include a default constructor (unless it doesn’t make sense) perform all initialization in the constructors test all initialization parameters

39 FIT1002 2006 39 Reading Constructors Savitch, Sec. 4.4


Download ppt "FIT1002 2006 1 Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are."

Similar presentations


Ads by Google