Introduction to Constructors Lecture # 4. Copyright © 2011 Pearson Education, Inc. 3-2 Arguments Passed By Value In Java, all arguments to a method are.

Slides:



Advertisements
Similar presentations
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Chapter 6: A First Look at classes. Object-Oriented Programming  Object-oriented programming is centered on creating objects rather than procedures.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
Chapter 6: A First Look at Classes
A First Look At Classes Chapter 6. Procedural Programming In procedural- programming all the program functionality is written in a few modules of code.
Starting Out With Java 5 (Control Structures to Objects) Chapter 6 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Starting Out with Java: From Control Structures through Objects.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Objects and Classes Mostafa Abdallah
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
© 2010 Pearson Addison-Wesley. All rights reserved. 6-1 Chapter Topics Chapter 6 discusses the following main topics: –Classes and Objects –Instance Fields.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Topics Instance variables, set and get methods Encapsulation
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #1 Review.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Unified Modeling Language
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CS16: UML’s Unified Modeling Language. UML Class Diagram A UML class diagram is a graphical tool that can aid in the design of a class. The diagram has.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 3: A First Look at Classes and Objects.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Lecture 3: Introduction to Object and Classes
Examples of Classes & Objects
Chapter 3 Classes & Objects
Road Map Introduction to object oriented programming. Classes
Chapter 6: A First Look at Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Classes & Objects: Examples
Today’s topics UML Diagramming review of terms
Object Oriented Programming Review
Lecture 5- Classes, Objects and Methods
Object Oriented Programming in java
Introduction to Object-Oriented Programming
Chapter 9 Objects and Classes Part 01
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 4 Constructors Section 4.4
Chapter 7 Objects and Classes
Chapter 6: A First Look at classes
Presentation transcript:

Introduction to Constructors Lecture # 4

Copyright © 2011 Pearson Education, Inc. 3-2 Arguments Passed By Value In Java, all arguments to a method are passed “by value”. If the argument is a reference to an object, it is the reference that is passed to the method. If the argument is a primitive, a copy of the value is passed to the method.

Copyright © 2011 Pearson Education, Inc. 3-3 Instance Fields and Methods Fields and methods that are declared as previously shown are called instance fields and instance methods. Objects created from a class each have their own copy of instance fields. Instance methods are methods that are not declared with a special keyword, static.

Copyright © 2011 Pearson Education, Inc. 3-4 Instance Fields and Methods Instance fields and instance methods require an object to be created in order to be used. Example: RoomAreas.javaRoomAreas.java Note that each room represented in this example can have different dimensions. Rectangle kitchen = new Rectangle(); Rectangle bedroom = new Rectangle(); Rectangle den = new Rectangle();

Copyright © 2011 Pearson Education, Inc. 3-5 Constructors Classes can have special methods called constructors. Constructors are used to perform operations at the time an object is created. Constructors typically initialize instance fields and perform other object initialization tasks.

Copyright © 2011 Pearson Education, Inc. 3-6 Constructors Constructors have a few special properties that set them apart from normal methods. –Constructors have the same name as the class. –Constructors have no return type (not even void). –Constructors may not return any values. –Constructors are typically public. Example: ConstructorDemo.javaConstructorDemo.java Example: RoomConstructor.javaRoomConstructor.java

Copyright © 2011 Pearson Education, Inc. 3-7 The Default Constructor If a constructor is not defined, Java provides a default constructor. –It sets all of the class’ numeric fields to 0. –It sets all of the class’ boolean fields to false. –It sets all of the class’ reference variables, the default constructor sets them to the special value null. The default constructor is a constructor with no parameters. Default constructors are used to initialize an object in a default configuration.

Copyright © 2011 Pearson Education, Inc. 3-8 Constructors in UML In UML, the most common way constructors are defined is: Rectangle - width : double - length : double +Rectangle(len:double, w:double) + setWidth(w : double) : void + setLength(len : double): void + getWidth() : double + getLength() : double + getArea() : double Notice there is no return type listed for constructors.

Copyright © 2011 Pearson Education, Inc. 3-9 The String Class Constructor One of the String class constructors accepts a string literal as an argument. This string literal is used to initialize a String object. For instance: String name = new String("Michael Long");

Copyright © 2011 Pearson Education, Inc The String Class Constructor This creates a new reference variable name that points to a String object that represents the name “Michael Long” Because they are used so often, Strings can be created with a shorthand: String name = "Michael Long";

Copyright © 2011 Pearson Education, Inc The BankAccount Example BankAccount - balance : double - interestRate : double - interest : double +BankAccount(startBalance:double, intRate :double): + deposit(amount : double) : void + withdrawl(amount : double: void + addInterest() : void + getBalance() : double + getInterest() : double Example Files: BankAccount.java AccountTest.java

Copyright © 2011 Pearson Education, Inc Classes, Variables and Scope The list below shows the scope of a variable depending on where it is declared. –Inside a method: Visible only within that method. Called a local variable. –In a method parameter: Called a parameter variable. Same as a local variable Visible only within that method. –Inside the class but not in a method: Visible to all methods of the class. Called an instance field.

Copyright © 2011 Pearson Education, Inc Shadowing A parameter variable is, in effect, a local variable. Within a method, variable names must be unique. A method may have a local variable with the same name as an instance field. This is called shadowing. The local variable will hide the value of the instance field. Shadowing is discouraged and local variable names should not be the same as instance field names.

Copyright © 2011 Pearson Education, Inc Object Oriented Design Finding Classes and Their Responsibilities Finding the classes –Get written description of the problem domain –Identify all nouns, each is a potential class –Refine list to include only classes relevant to the problem Identify the responsibilities –Things a class is responsible for knowing –Things a class is responsible for doing –Refine list to include only classes relevant to the problem

Copyright © 2011 Pearson Education, Inc Object Oriented Design Finding Classes and Their Responsibilities Identify the responsibilities –Things a class is responsible for knowing –Things a class is responsible for doing –Refine list to include only classes relevant to the problem