Constructors. Defining Constructors  A constructor is a special kind of method that is designed to perform initializations, such as giving values to.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Feb 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

COMP 110: Introduction to Programming Tyler Johnson Mar 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Classes and Objects in Java
Intro to CS – Honors I More Objects and Methods GEORGIOS PORTOKALIDIS
Inheritance Writing and using Classes effectively.
Overloading. Overloading Basics  We have seen that you can give the same name to methods in different classes.  You can also give the same name to methods.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Intro to CS – Honors I Inheritance and Polymorphism GEORGIOS PORTOKALIDIS
Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
More About Objects and Methods
Information Hiding and Encapsulation
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Static Methods and Static Variables.
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any.
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
COMP More About Classes Yi Hong May 22, 2015.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Static Methods and Static Variables.
Introduction To Scientific Programming Chapter 5 – More About Objects and Methods.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Static Methods and Static Variables.
More About Objects and Methods Chapter 5. When an Object Is Required Methods called outside the object definition require an object to precede the method.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Static Methods and Static Variables.
Lecturer: Dr. AJ Bieszczad Chapter 5 COMP 150: Introduction to Object-Oriented Programming 5-1 l Programming with Methods l Static Methods and Static Variables.
Object-Oriented Design. A Historical Perspective Programs  Procedures to manipulate data Earlier: procedural  Think actions/procedures first, data second.
COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Static Methods and Static Variables.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 4.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Objects and Classes Mostafa Abdallah
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
COMP Inheritance Basics Yi Hong June 09, 2015.
Class and Method Definitions. UML Class Diagram Automobile - fuel: double - speed: double - license: String + increaseSpeed(double howHardPress): void.
Catie Welsh March 23,  Lab 6 due Friday by 1pm 2.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Polymorphism l Constructors.
Classes - Intermediate
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Learners Support Publications Constructors and Destructors.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Day Programming with Methods. The “do” loop The “do” loop is a type of while loop that only does one iteration (does one loop at least) It is hardly.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Constructors and Destructors
Static data members Constructors and Destructors
Arrays in Classes and Methods
More About Objects and Methods
Something about Java Introduction to Problem Solving and Programming 1.
CSC 113 Tutorial QUIZ I.
Defining Classes and Methods
Constructors and Destructors
JAVA Constructors.
Announcements Lab 5 was due today Program 3 due Monday by 12pm
More About Objects and Methods Chapter 5 Programming with Methods
Chapter 4 Constructors Section 4.4
Basic Exception Handling
More About Objects and Methods Chapter 5 Programming with Methods
Presentation transcript:

Constructors

Defining Constructors  A constructor is a special kind of method that is designed to perform initializations, such as giving values to instance variables.  A constructor is a method that is called when a new object is created.  A constructor can perform any action you want, but it is meant to perform initializations.  Constructors serve very much the same purpose as set methods.

Defining Constructors (cont’d)  Unlike set methods, constructors are called automatically whenever you create an object using the new operator.  Constructors have the same name as the class.  Constructors are normally overloaded so there are multiple definitions of the constructor, each with different numbers or types of parameters.

Differences between Constructor and Set Methods  Headings of constructor methods do not have the word void. They do not return anything, but unlike other methods that don’t return values, the word void is omitted.  Unlike set methods, the constructors give values to all the instance variables even though there may not be an argument for each instance variable.

Differences between Constructor and Set Methods (cont’d)  Whenever you define at least one constructor, you should be sure to include a constructor with zero parameters. Such a constructor is called a default constructor.

Using Constructors  Constructors are called at the time that you use new to create an object.  Whenever a class definition does not have a constructor definition, Java automatically creates a default constructor, that is, one with zero parameters.  However, once you add at least one constructor, then you are in charge of all constructors, i.e., no default constructor is created by Java.

Using Constructors (cont’d)  When you create a new object with the operator new, you must always include a call to a constructor after the operator new. PetRecord fish = new PetRecord(“Wanda”, 2, 0.25);  If we don’t specify any arguments, the default constructor is invoked. PetRecord newBorn = new PetRecord();

Using Constructors (cont’d)  A constructor can be called only when you create a new object with the operator new.  newBorn.PetRecord(“Fang”, 1, 150.0); is invalid.  Since you cannot call a constructor for an object after it is created, you need some other way to change the values of the instance variables of an object.  That is the purpose of the set methods, e.g., newBorn.set(“Fang”, 1, 150.0);

More about Constructors  Constructors are not usually included in class diagrams.  Constructors can invoked other methods in the class, like set methods.  For most classes you create, you should also define a default constructor.

An Example Using Constructors /** Class for basic pet records: name, age, and weight. */ public class PetRecord { private String name; private int age;//in years private double weight;//in pounds public void writeOutput( ) { System.out.println("Name: " + name); System.out.println("Age: " + age + " years"); System.out.println("Weight: " + weight + " pounds"); } public PetRecord(String initialName, int initialAge, double initialWeight) { name = initialName; if ((initialAge < 0) || (initialWeight < 0)) { System.out.println("Error: Negative age or weight."); System.exit(0); } else { age = initialAge; weight = initialWeight; } }

An Example Using Constructors (cont’d) public void set(String newName, int newAge, double newWeight) { name = newName; if ((newAge < 0) || (newWeight < 0)) { System.out.println("Error: Negative age or weight."); System.exit(0); } else { age = newAge; weight = newWeight; } } public PetRecord(String initialName) { name = initialName; age = 0; weight = 0; }

An Example Using Constructors (cont’d) public void set(String newName) { name = newName; //age and weight are unchanged. } public PetRecord(int initialAge) { name = "No name yet."; weight = 0; if (initialAge < 0) { System.out.println("Error: Negative age."); System.exit(0); } else age = initialAge; }

An Example Using Constructors (cont’d) public void set(int newAge) { if (newAge < 0) { System.out.println("Error: Negative age."); System.exit(0); } else age = newAge; //name and weight are unchanged. } public PetRecord(double initialWeight) { name = "No name yet"; age = 0; if (initialWeight < 0) { System.out.println("Error: Negative weight."); System.exit(0); } else weight = initialWeight; }

An Example Using Constructors (cont’d) public void set(double newWeight) { if (newWeight < 0) { System.out.println("Error: Negative weight."); System.exit(0); } else weight = newWeight; //name and age are unchanged. } public PetRecord( ) { name = "No name yet."; age = 0; weight = 0; }

An Example Using Constructors (cont’d) public String getName( ) { return name; } public int getAge( ) { return age; } public double getWeight( ) { return weight; } }

Using Constructors and set Methods import java.util.*; public class PetRecordDemo { public static void main(String[] args) { PetRecord usersPet = new PetRecord("Jane Doe"); System.out.println("My records on your pet are inaccurate."); System.out.println("Here is what they currently say:"); usersPet.writeOutput( ); Scanner keyboard = new Scanner(System.in); System.out.println("Please enter the correct pet name:"); String correctName = keyboard.nextLine( ); System.out.println("Please enter the correct pet age:"); int correctAge = keyboard.nextInt( ); System.out.println("Please enter the correct pet weight:"); double correctWeight = keyboard.nextDouble( ); usersPet.set(correctName, correctAge, correctWeight); System.out.println("My updated records now say:"); usersPet.writeOutput( ); } }