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

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
FIT Objectives By the end of this lecture, students should: understand how parameters are passed to and from methods understand what the differences.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 10 Classes Continued
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Object Oriented Programming Elhanan Borenstein Lecture #4.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 6: User-Defined Functions
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
ECE122 Feb. 22, Any question on Vehicle sample code?
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Introduction to Object-Oriented Programming Lesson 2.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
The Object-Oriented Thought Process Chapter 03
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Java Primer 1: Types, Classes and Operators
CSC 205 Java Programming II
Extending Classes.
Java Programming Language
Java Programming Language
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CMSC 202 Exceptions.
SPL – PS3 C++ Classes.
Presentation transcript:

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

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

FIT 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

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

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

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

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

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

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

FIT 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:

FIT 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”; }

FIT 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”; }

FIT 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

FIT 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”; }

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

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

FIT 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() { }

FIT 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”; }

FIT 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”; }

FIT 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; }

FIT 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”; }

FIT 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”; }

FIT 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”; }

FIT 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”; }

FIT 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; }

FIT 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 }

FIT 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); }

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

FIT 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

FIT 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

FIT 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; }

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

FIT 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

FIT 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

FIT 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; }

FIT 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 }

FIT 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…)

FIT 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

FIT Reading Constructors Savitch, Sec. 4.4