Computer Science and Engineering College of Engineering The Ohio State University Classes and Objects: Members, Visibility The credit for these slides.

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Lecture 2: Object Oriented Programming I
Road Map Introduction to object oriented programming. Classes
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
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.
GP3, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 3 We start at 13:00 Slides are available from the course home page Feel free to.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
Copyright Curt Hill Variables What are they? Why do we need them?
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Introduction to Defining Classes
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
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.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Object-Based Programming in VB.NET. Must Understand Following: Encapsulation Information hiding Abstract Data Type Class, Instance, Reference Variable.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Classes, Interfaces and Packages
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Class Fundamentals BCIS 3680 Enterprise Programming.
Comp1004: Building Better Objects II Encapsulation and Constructors.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Primitive/Reference Types and Value Semantics
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Overloading and Constructors
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Classes and Methods
Recap Week 2 and 3.
Defining Classes and Methods
Defining Classes and Methods
Chapter 7 Objects and Classes
Chapter 5 Classes.
Presentation transcript:

Computer Science and Engineering College of Engineering The Ohio State University Classes and Objects: Members, Visibility The credit for these slides goes to Professor Paul Sivilotti at The Ohio State University. The slides have been modified a bit for use at Clemson by Professors Murali Sitaraman and Jason Hallstrom.

Computer Science and Engineering The Ohio State University Object-Oriented Programming  Fundamental component is an object A running program is a collection of objects  An object encapsulates: State (i.e. data) Behavior (i.e. how state changes)  Each object is an instance of a class Class declaration is a blueprint for objects A class is a component type  e.g. Stack, String, Person An object is an instance of that component Pencil mathTool = new Pencil();

Computer Science and Engineering The Ohio State University Graphical View of Instances instance of Pencil mathTool p1 pencilCase[0] p2 1 class/type (“Pencil”) 3 objects/instances 4 references/variables

Computer Science and Engineering The Ohio State University Good Practice: Files and Classes  Declare one class per file  Give file the same name as the class declaration it contains class HelloWorldApp declaration appears in HelloWorldApp.java class Pencil is defined in Pencil.java

Computer Science and Engineering The Ohio State University Example Class Declaration public class Pencil { private boolean hasEraser; private String color; private int length; public int sharpen (int amount) { length = length - amount; return length; } public String getDescription () { if (length < 15) { return “small: ” + color; } else { return “large: ” + color; }

Computer Science and Engineering The Ohio State University Members  Two kinds of members in a class declaration Fields, i.e. data (determine the state) private boolean hasEraser; private String color; private int length; Methods, i.e. procedures (access/modify the state) public int sharpen (int amount) { length = length – amount; return length; }  (Much later: nested classes and nested interfaces)

Computer Science and Engineering The Ohio State University Graphical View of Object sharpen()getDescription() length 14 consumeEraser() hasEraser true “red” mathTool An object of type “Pencil” called “mathTool” state behavior color

Computer Science and Engineering The Ohio State University Object Creation and Deletion  Explicit object creation with new() ; java.util.Date d = new java.util.Date(); Integer count = new Integer(34); Pencil p1 = new Pencil(“red”);  Unlike C/C++, memory is not explicitly freed References just go out of scope (become null) { //create a Date object (called d) java.util.Date d = new java.util.Date();... } //d out of scope, object is unreachable Automatic garbage collection (eventually) deletes unreachable objects

Computer Science and Engineering The Ohio State University Initialization of an Object’s Fields  Implicit: Default initial values based on type e.g. boolean is false, reference type is null private boolean hasEraser;//implicitly false  Explicit: Initialization with field declaration private int length = 14;  Special method: “constructor” Syntax: name is same as class, no return type public class Pencil { private String color; public Pencil (String c) { color = c; } Invoked by new(), so can have parameters Runs after implicit/explicit field initialization

Computer Science and Engineering The Ohio State University Default Initial Values  For fields only  Does not apply to local variables TypeDefault boolean false byte 0 short 0 int 0 long 0L float 0.0f double 0.0d char ‘\u0000’ reference null

Computer Science and Engineering The Ohio State University Example Constructor public class Pencil { private boolean hasEraser; private String color; private int length = 14; public Pencil (String c) { color = c; hasEraser = (length >= 10); }... same methods as before... }

Computer Science and Engineering The Ohio State University Graphical View of Object sharpen()getDescription() length 14 consumeEraser() hasEraser true “red” color Pencil()

Computer Science and Engineering The Ohio State University Good Practice: Establish Invariant  Class representations typically have a representation invariant What is true of the state for all instances? e.g., All long pencils have erasers length >= 10 ==> hasEraser So the state (false, “green”, 14) is not valid  A constructor can call other methods of its own object Danger! Convention (representation invariant) might not hold at call point

Computer Science and Engineering The Ohio State University Visibility  Members can be private or public member-by-member declaration private String color; public int length; public int sharpen (int amount) {... }  Private members Can be accessed only by instances of same class Provide concrete implementation / representation  Public members Can be accessed by any object Provide abstract view (client-side)

Computer Science and Engineering The Ohio State University Example public class Pencil { private String color; private int length = 14; private boolean isValid(String c) {…} public Pencil(String c, int l) {…} public String toString() {…} public void setColor(String c) {…} } public class CreatePencil { public void m() { Pencil p = new Pencil(“red”, 12); p.setColor(“blue”); p.color = “blue”; } } OKCompile-time Error

Computer Science and Engineering The Ohio State University Graphical View of Member Visibility “red” isValid()toString() length 14 setColor() Pencil() color private public

Computer Science and Engineering The Ohio State University Example  See PencilA.java Concrete state (i.e., representation) is hidden from clients Client-side view (more abstract, given in the interface) is accessed and manipulated through public methods  See PencilB.java Different representation Exact same behavior as far as the outside world is concerned

Computer Science and Engineering The Ohio State University Good Practice: Member Declarations  Group member declarations by visibility Java’s convention: private members at top  No fields should be public Common idiom: Public “accessor” methods for getting and setting private fields (aka getters/setters) public class Pencil { private int length; public int getLength() {... } public void setLength() {... } }

Computer Science and Engineering The Ohio State University Method Invocation  Syntax: objectreference.member p.color = “red”; p.toString().length();  Reference is implicit inside same object public class Pencil { private String color; public Pencil() { color = “red”; }  Explicit reference to same object available as this keyword (from within the object itself) this.color = “red”;

Computer Science and Engineering The Ohio State University Good Practice: Formal Arguments  Constructor arguments that are used directly to set object fields can be given the same name as the field Formal argument “hides” class field variable Refer to class field variable using explicit this public class Pencil { private int length; public Pencil(int length) { this.length = length; }

Computer Science and Engineering The Ohio State University Method Overloading  A class can have more than one method with the same name as long as they have different parameter lists public class Pencil {... public void setPrice(float newPrice) { price = newPrice; } public void setPrice(Pencil p) { price = p.getPrice(); }  How does the compiler know which method is being invoked? Answer: it compares the number and type of the parameters and uses the matched one p.setPrice(3.4);  Differing only in return type is not allowed

Computer Science and Engineering The Ohio State University Multiple Constructors  Default constructor: no arguments Fields initialized explicitly in declaration or implicitly to language-defined initial values Provided automatically only if no constructor defined explicitly public class Pencil { String color; //initialized implicitly to null int length = 14; //initialized explicitly … }  Another constructor: one same-class argument public Pencil (Pencil p) {... }  One constructor can call another with this() If another constructor called, must be the first statement public Pencil (Pencil p) { this(p.color); //must be 1st line length = 10; }

Computer Science and Engineering The Ohio State University Summary  Classes and objects Class declarations and instantiations  Instance members Fields, i.e. state Methods, i.e. behaviors  Constructors  Visibility private: Visible only to instances of same class public: Visible to instances of any class  Overloading Multiple implementations of same method name Distinguished by formal parameter types