1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.

Slides:



Advertisements
Similar presentations
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Java Software Solutions
Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Objects and Classes Objects and Classes objects defining classes method declaration object references and aliases instance variables encapsulation and.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Introduction to Object-Oriented Programming
Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Object-Oriented Programming in C++
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Using Classes and Objects Chapters 3 Creating Objects – Section 3.1 The String Class – Section 3.2 The Scanner Class – Section 2.6 Instructor: Scott Kristjanson.
CSE 1201 Object Oriented Programming Using Classes and Objects.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
SEEM Java – Basic Introduction, Classes and Objects.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
Chapter 3 Implementing Classes
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Topic: Classes and Objects
Chapter 5: Enhancing Classes
Variables in Java A variable holds either
Creating Objects & String Class
More Object Oriented Programming
Corresponds with Chapter 7
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Java Classes Aliases & Null & This
Packages & Random and Math Classes
Introduction to Object-Oriented Programming
Classes, Objects and Methods
Outline Creating Objects The String Class The Random and Math Classes
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture: L&L, Familiarize yourself with Sun Website as a reference for the Java Class Library

“Static” Classes A class that has static members only – both attributes and methods is a “static” class We do not instantiate any objects of that class We use the class name to access its members Examples: –Math class Math.sqrt(doubleValue) or Math.PI –QuadraticSolver class in Project 1 QuadraticSolver.getSolution() 2

“Static” Classes 3 QuadraticSolver Typically, there are no variables There may be some class constants + getEquation(a : int, b : int, c : int) : String + getSolution(a : int, b : int, c : int) : String Class Name List of its Variables List of its Methods Math + PI (Other constants would be here) + sqrt(value : double) : double (Other methods would be here) Class Name List of its Variables List of its Methods

“Static” classes In such a class, we define attributes and methods including the reserved word static Examples: –In the Math class source code public static final double PI = 3.14…..; public static double sqrt(double input) { } –In the QuadraticSolver source code public static String getEquation(int a, int b, int c) { } public static String getSolution(int a, int b, int c) { } 4

“Static” Classes Although this is a valid way to break a Java program into smaller pieces, it is not the true intent of Object-Oriented Programming (OOP) It is more like procedural programming (e.g. C) In true OOP, we: –Use classes to encapsulate data and use methods to define the valid operations on that data –Instantiate objects from the classes and access methods using object names – not class names 5

6 Introduction to Classes A class defines the attributes and behavior of a specific type of object –Attributes are variables declared in the class –Behaviors are methods defined in the class Normally, we access an object by calling a method defined by its class We may sometimes access an attribute defined by its class, but this is discouraged

7 “Classifying” into Classes To understand the context of the word “class” in Java, think about the word “classify” Classes “classify” different “objects” based on the similarities in attributes and behaviors The desks, chairs, and tables in this room can be classified as “Furniture” class objects There’s a sense of common attributes and behaviors that all “Furniture” objects share

8 Introduction to Classes A class has a name that we can use as if it were a data type when declaring a variable When we declare a variable with the name of a class as its type, we are creating a reference variable (It can contain a reference to an object) We access an object’s methods / attributes using the reference variable name and the. notation, e.g. ClassName objectName; //reference variable objectName.methodName() // Note the ( ) objectName.variableName // Note no ( )

9 Example of a Class Definition We can draw a diagram of a class to outline its important features before writing code – its name, attributes, and behaviors BankAccount - balance... + BankAccount (initial : double) Note: Constructor + getBalance() : double + deposit(amount : double) : boolean + withdraw(amount : double) : boolean Class Name List of its Variables List of its Methods

10 Example of a Class Definition public class BankAccount { // an attribute or variable private double balance; // the constructor method public BankAccount(double initial) { balance = initial; }

11 Example of a Class Definition // other behaviors or normal methods public double getBalance() { return balance; } public boolean deposit(double amount) { balance += amount; return true; } // additional behaviors or methods } // end of class definition

12 Creating Objects To declare a variable as a reference to a BankAccount object, we use the class name as the type name BankAccount myAccount; This declaration does not create an object It only creates a reference variable that can hold a reference to a BankAccount object

13 Example of a Class Definition Declaring a BankAccount object: BankAccount myAccount = new BankAccount(100.00); //constructor Accessing other BankAccount methods: boolean status = myAccount.deposit(50.00); double myMoney = myAccount.getBalance(); Why can’t we just do this? myAccount.balance += 50.00;

14 Prototype for a Class Definition We use the Java reserved word private to prevent access to a variable or method from code that is written outside the class We use the Java reserved word public to allow access to a variable or method from code that is written outside the class Normally, we declare variables to be private Normally, we declare methods to be public We will see some valid exceptions later

15 Creating Objects We use the new operator to create an object Creating an object is called instantiation An object is an instance of a particular class myAccount is assigned a reference to an object of type BankAccount that encapsulates the balance BankAccount myAccount = new BankAccount(100.00); This calls the BankAccount constructor, which is a special method that initializes the object Instantiation operator

16 Invoking Methods Once an object has been instantiated, we can use the dot operator to invoke or “call” any of the object’s methods double myMoney = myAccount.getBalance(); A method invocation can be thought of as: –Asking an object to perform a service OR –Doing something to the state of the object

17 References A primitive variable contains the value itself, but a reference variable contains an object reference An object reference can be thought of as a pointer to the location of the object in memory Rather than dealing with arbitrary address values, we often depict a reference graphically $ BankAccount myAccount int num138 BankAccount object“Reference” (or Pointer)

18 Assignment Revisited The act of assignment takes a copy of a value and stores it in a variable For primitive types: num1 38 num2 96 Before: num2 = num1; num1 38 num2 38 After:

19 Reference Assignment For object references, assignment copies the reference: if (myAccount == yourAccount) // note use of == System.out.println(“The Same”); // no yourAccount = myAccount; if(myAccount == yourAccount) System.out.println(“The Same”); //yes myAccount yourAccount Before: $ $50.00 After: $ $50.00 Garbage: See later slide myAccount yourAccount

20 Aliases Two or more references that refer to the same object are called aliases of each other One object can be accessed using more than one reference variable Changing an object via one reference variable changes it for all of its aliases, because there is really only one object Aliases can be useful, but should be managed carefully (Do you want me to be able to withdraw money from your account? I doubt it!)

21 Garbage Collection When there are no longer any variables containing a reference to an object (e.g. the $50.00 on the earlier slide), the program can no longer access it The object is useless and is considered garbage Periodically, Java performs automatic garbage collection and returns an object's memory to the system for future use In other languages such as C/C++, the programmer must write explicit code to do the garbage collection

22 Garbage Collection Setting reference variable’s value to null, makes the object garbage (unavailable): myAccount = null; myAccount Before: $ Garbage now null After: $ No object myAccount

23 Garbage Collection If a reference variable’s value is equal to null, any reference to an attribute or method of that object will cause your program to fail. myAccount = new BankAccount(100.00); System.out.println(myAccount.balance()); // OK myAccount = null; // $100 BankAccount => garbage System.out.println(myAccount.balance()); // Fails