Object Oriented Programming using Java - Class Instance Variables

Slides:



Advertisements
Similar presentations
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look 1 Xiang Lian The University of Texas – Pan American Edinburg,
Advertisements

Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look - modified by Eileen Kraemer.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
 2006 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
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.
Dale Roberts Introduction to Java - Access Specifiers Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
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.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Dale Roberts Object Oriented Programming using Java - OOD to OOP: ATM Case Study Dale Roberts, Lecturer Computer Science, IUPUI
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
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.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
 2006 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
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.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
計算機程式語言 Lecture 03-1 國立台灣大學生物機電系 林達德 3 3 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 12.
 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
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Object Oriented Programming using Java - Composition
CompSci 230 S Programming Techniques
Classes and Objects: A Deeper Look
Classes and Objects: A Deeper Look
GUI Programming using Java - Key Events
Introduction to Classes and Objects
Object-Oriented Programming: Inheritance
Variable Declarations, Data types, Expressions
Variable Declarations, Data types, Expressions
Introduction to Classes and Objects
Lecture 23 Polymorphism Richard Gesick.
Object Based Programming
Object-Oriented Programming: Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
Chapter 6 Methods: A Deeper Look
Lecture 22 Inheritance Richard Gesick.
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.
Object-Oriented Programming: Inheritance
Introduction to Classes and Objects
The University of Texas – Pan American
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look UTPA – Fall 2012 This set of slides is revised from lecture.
Object Oriented Programming in java
Introduction to Classes and Objects
Chapter 8 Classes and Objects: A Deeper Look
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

Object Oriented Programming using Java - Class Instance Variables Department of Computer and Information Science, School of Science, IUPUI Object Oriented Programming using Java - Class Instance Variables Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

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

Outline Time1.java (2 of 2) format strings

Time Class Case Study Instance variables Can be initialized when they are declared or in a constructor Should maintain consistent (valid) values If a class does not define a constructor the compiler will provide a default constructor public services (or public interface) public methods available for a client to use

Software Engineering Observation 8.1 Methods that modify the values of private variables should verify that the intended new values are proper. If they are not, the set methods should place the private variables into an appropriate consistent state.

8.2 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

Software Engineering Observation 8.2 Classes simplify programming, because the client can use only the public methods exposed by the class. Such methods are usually client oriented rather than implementation oriented. Clients are neither aware of, nor involved in, a class’s implementation. Clients generally care about what the class does but not how the class does it.

Software Engineering Observation 8.3 Interfaces change less frequently than implementations. When an implementation changes, implementation-dependent code must change accordingly. Hiding the implementation reduces the possibility that other program parts will become dependent on class-implementation details.

Outline Create a Time1 object Time1Test.java (1 of 2) Call toUniversalString method Call toString method

Outline Time1Test.java Call setTime method (2 of 2) Call setTime method with invalid values

Controlling Access to Members A class’s public interface public methods a view of the services the class provides to the class’s clients A class’s implementation details private variables and private methods are not accessible to the class’s clients This object-oriented concept is called “Encapsulation”.

Outline MemberAccessTest .java Attempting to access private instance variables

Referring to the Current Object’s Members with the this Reference Any object can access a reference to itself with keyword this Non-static methods implicitly use this when referring to the object’s instance variables and other methods Can be used to access instance variables when they are shadowed by local variables or method parameters A .java file can contain more than one class But only one class in each .java file can be public

Outline Create new SimpleTime object ThisTest.java (1 of 2) Declare instance variables Method parameters shadow instance variables Using this to access the object’s instance variables

Outline ThisTest.java (2 of 2) Using this explicitly and implicitly to call toUniversalString Use of this not necessary here

Common Programming Error 8.2 It is often a logic error when a method contains a parameter or local variable that has the same name as a field of the class. In this case, use reference this if you wish to access the field of the class—otherwise, the method parameter or local variable will be referenced.

Error-Prevention Tip 8.1 Avoid method parameter names or local variable names that conflict with field names. This helps prevent subtle, hard-to-locate bugs. Standard software engineering practice avoids this error through the use of prefixes. Parameters start with p_, local variables start with l_, instance variables start with i_. The letter used for the prefix is less important than setting a standard and sticking with it.

Performance Tip 8.1 Java conserves storage by maintaining only one copy of each method per class—this method is invoked by every object of the class. Each object, on the other hand, has its own copy of the class’s instance variables (i.e., non-static fields). Each method of the class implicitly uses this to determine the specific object of the class to manipulate.

Acknowledgements Deitel, Java How to Program