Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Advertisements

Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Dale Roberts Object Oriented Programming using Java - Inheritance Overview Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Object Oriented Programming using Java - Inheritance Constructors Dale Roberts, Lecturer Computer Science, IUPUI
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look - modified by Eileen Kraemer.
 2006 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Object Oriented Programming using Java - Polymorphism
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Dale Roberts Object Oriented Programming using Java - Packages Dale Roberts, Lecturer Computer Science, IUPUI Department.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Dale Roberts Introduction to Java - Access Specifiers Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Dale Roberts Program Control using Java - Boolean Expressions Dale Roberts, Lecturer Computer Science, IUPUI Department of.
Dale Roberts Object Oriented Programming using Java - Enumerations Dale Roberts, Lecturer Computer Science, IUPUI Department.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Dale Roberts Program Control using Java - Selection Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Classes. Consider this simplistic class public class ProjInfo {ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed.
ECE122 Feb. 22, Any question on Vehicle sample code?
Dale Roberts Object Oriented Programming using Java - OOD to OOP: ATM Case Study Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
CISC6795 Spring 11 Fordham Univ. Introduction to Classes and Objects 1.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
1 Chapter 3 – Object-Based Programming 2 Initializing Class Objects: Constructors Class constructor is a specical method to initialise instance variables.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Today’s lecture Review chapter 5 go over exercises.
CLASS AND OBJECTS Valerie Chu, Ph.D. LeMoyne-Owen College 1.
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Methods.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 12.
Dale Roberts Introduction to Java - Input, Program Control and Instantiation Dale Roberts, Lecturer Computer Science, IUPUI
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Dale Roberts Object Oriented Programming using Java - Getters and Setters Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
Object Oriented Programming using Java - Composition
CompSci 230 S Programming Techniques
Classes and Objects: A Deeper Look
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Object-Oriented Programming: Inheritance
Object Oriented Programming using Java - Class Instance Variables
Road Map Introduction to object oriented programming. Classes
Functions Declarations CSCI 230
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises UTPA – Fall 2012 This set of slides is revised from.
The University of Texas – Pan American
Java Programming Language
Chapter 8 Classes and Objects: A Deeper Look
Chapter 4 Constructors Section 4.4
Classes Member Qualifiers
Presentation transcript:

Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts 2 Time Class Case Study If a class does not define a constructor the compiler will provide a default constructor Instance variables Can be initialized when they are declared or in a constructor Should maintain consistent (valid) values

Dale Roberts 3Outline Time1.java (1 of 2) private instance variables Declare public method setTime Validate parameter values before setting instance variables

Dale Roberts 4Outline Time1.java (2 of 2) format strings

Dale Roberts Time Class Case Study (Cont.) String method format Similar to printf except it returns a formatted string instead of displaying it in a command window new implicitly invokes Time1 ’s default constructor since Time1 does not declare any constructors

Dale Roberts 6Outline Time1Test.ja va (1 of 2) Create a Time1 object Call toUniversalString method Call toString method

Dale Roberts 7Outline Time1Te st.java (2 of 2) Call setTime method Call setTime method with invalid values

Dale Roberts 8 Time Class Case Study: Overloaded Constructors Overloaded constructors Provide multiple constructor definitions with different signatures No-argument constructor A constructor invoked without arguments The this reference can be used to invoke another constructor Allowed only as the first statement in a constructor’s body

Dale Roberts 9Outline Time2.ja va (1 of 4) No-argument constructor Invoke three-argument constructor

Dale Roberts 10Outline Time2.java (2 of 4) Call setTime method Constructor takes a reference to another Time2 object as a parameter Could have directly accessed instance variables of object time here

Dale Roberts 11Outline Time2.java (3 of 4)

Dale Roberts 12Outline Time2.java (4 of 4)

Dale Roberts 13 It is a syntax error when this is used in a constructor’s body to call another constructor of the same class if that call is not the first statement in the constructor. It is also a syntax error when a method attempts to invoke a constructor directly via this. Common Programming Error 8.3

Dale Roberts 14 Common Programming Error 8.4 A constructor can call methods of the class. Be aware that the instance variables might not yet be in a consistent state, because the constructor is in the process of initializing the object. Using instance variables before they have been initialized properly is a logic error.

Dale Roberts 15Outline Time2Test.ja va (1 of 3) Call overloaded constructors

Dale Roberts 16Outline Time2Test.ja va (2 of 3)

Dale Roberts 17Outline Time2Te st.java (3 of 3)

Dale Roberts Default and No-Argument Constructors Every class must have at least one constructor If no constructors are declared, the compiler will create a default constructor Takes no arguments and initializes instance variables to their initial values specified in their declaration or to their default values Default values are zero for primitive numeric types, false for boolean values and null for references If constructors are declared, the default initialization for objects of the class will be performed by a no-argument constructor (if one is declared)

Dale Roberts 19 Common Programming Error If a class has constructors, but none of the public constructors are no-argument constructors, and a program attempts to call a no-argument constructor to initialize an object of the class, a compilation error occurs. A constructor can be called with no arguments only if the class does not have any constructors (in which case the default constructor is called) or if the class has a public no-argument constructor.

Dale Roberts 20 Software Engineering Observation Java allows other methods of the class besides its constructors to have the same name as the class and to specify return types. Such methods are not constructors and will not be called when an object of the class is instantiated. Java determines which methods are constructors by locating the methods that have the same name as the class and do not specify a return type.

Dale Roberts Acknowledgements Deitel, Java How to Program