Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)

Slides:



Advertisements
Similar presentations
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Advertisements

Road Map Introduction to object oriented programming. Classes
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
1 SWE Introduction to Software Engineering Lecture 23 – Architectural Design (Chapter 13)
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Object-Oriented Analysis and Design
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Object Oriented Software Development
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
Writing Classes (Chapter 4)
An Object-Oriented Approach to Programming Logic and Design
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Chapter 1 What is Programming? Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
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.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
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.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
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 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Object-Oriented Programming Chapter Chapter
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
ISBN Object-Oriented Programming Chapter Chapter
Chapter 16 UML Class Diagrams 1CS6359 Fall 2012 John Cole.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Chapter 5 System Modeling. What is System modeling? System modeling is the process of developing abstract models of a system, with each model presenting.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
CMSC 345 Fall 2000 OO Design. Characteristics of OOD Objects are abstractions of real-world or system entities and manage themselves Objects are independent.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Java Programming: Guided Learning with Early Objects
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
Corresponds with Chapter 7
Lecture 22 Inheritance Richard Gesick.
Defining Classes and Methods
Object Oriented Programming in java
Creating and Using Classes
Presentation transcript:

Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold

Chapter Preview In this chapter we will: describe user-defined classes –instance variables –constructors –instance methods present several examples of classes discuss the concepts of mutability and visibility describe method overloading

Object-Oriented Programming OOP supports the view that programs are composed of interacting objects Objects are composed of –values known as attributes or instance variables –operations (actions) that can be performed on these values know as instance methods Messages requesting an action or value are sent to objects Objects respond to messages that are in their protocols or interfaces

Objects Encapsulate data values within a single entity Their behavior is often general enough to allow reuse in a variety of situations Often form a basis from which other objects can be derived using a mechanism known as inheritance Are a type container that is stored on the system heap A client is program that uses an object

Client Rights To declare variables of the class type To create instances of the class using constructors To send messages to instances of the class by invoking class instance methods To know the class public interface –instance method names –parameter number and types –return types To know the instance methods that alter (mutate) the instance

Class Rights To define the class public interface To hide all the implementation details from the client To protect internal data from client access To change implementation details at any time, provided the public interface remains intact To change the public interface with client concurrence

Revised Class Definition public class name { declarations of instance variables constructor definitions method definitions } Every class needs one or more constructor definitions

Revised Class Definition Instance variables –local data contained in class Method definitions –describe the class interface and how it responds to each client message Constructors definitions –describe how to initialize instance variables in a new object

Constructors Look like regular instance methods Never have a return type Always have the same name as the class name May have parameters Default constructors have no parameters Constructors can be overloaded (more than one definition in the same class)

Constructors public class Clock { int hour, minute; // constructor public Clock( ) { hour = 12; minute = 0; } // other methods follow … }

Using Constructors // c1 set to 12:00 Clock c1 = new Clock(); // c1 set to 8:30 c1.setHour(8); c1.setMinute(30); // c2 set to 12:00 c1 still 8:30 Clock c2 = new Clock();

Overloading Constructors Classes can have more than one constructor All constructors have the same name (the class name) Each constructor differs from the others in either the number or types of its arguments new is used when using a constructor to create a new object

Overloading Constructors We could add the following to Clock public Clock(int h, int m) { hour = h; minute = m; } A client program could contain Clock c1 = new Clock(8, 20); Which is the same as writing Clock c1 = new Clock( ); c1.setHour(8); c1.setMinute(20);

Overloaded Clock Constructors public class Clock { int hour, minute; public Clock () { hour = 12; minute = 0; } public Clock (int h, int m){ hour = h; minute = m; } … }

Using Constructors Clock c1 = new Clock( ); // c1 set to 12:00 Clock c2 = new Clock(8, 20); // c2 set to 8:20 Clock c3 = new Clock(); // c3 set to 8:20 c3.setHour(8); C3/setMinute(20);

Overloading Methods Methods can also be overloaded This allows different versions of the method in the same class Each method variant must differ from the others by the number or types of its parameters Overloading allows methods with the same name to have different return types

Methods Calling Other Methods Methods are allowed to call other methods in the same class without specifying an explicit receiver This allows overloaded methods to call one another without repeating redundant code Example: public void display (DrawingBox d, int r) { display(d, d.getDrawableWidth()/2, d.getDrawableHeight()/2, r); }

Dot Notation We can also use dot notation to view instance variables of the same class that are different from the receiver Example: public boolean priorTo (Clock c) { return (hour < c.hour || hour == c.hour && minute < c.minute); }

this – Avoiding Variable Name Collisions “this” can be used to distinguish between references to instance variables and local identifiers or arguments public void set (int hour, int minute) { int totalMinutes = (hour * 60 + minutes); this.minute = totalMinutes % 60; } this.minute refers to the instance variable minute not the method argument

this – Passing the Receiver as an Argument “this” can be used to send a message to the current receiver of the message without explicitly naming the receiver public boolean after (Clock c) { return c.priorTo(this); } this is used as if it were a variable referring to the receiver of the message

this – Chaining Constructors “this” can be used to simplify constructor code by allowing one constructor to call another We can rewrite the clock constructors as: public Clock ( ) { this(12,0); } public Clock (int hour, int minute) { set(hour, minute); }

Visibility Qualifiers public int x; // client creating instance o of this // class can access x by writing o.x private int y; // no can access y directly, access // provided though class methods To enforce complete information hiding all instance variables should be declared using private The default visibility of instance variables lies between private and public (explained later in the text)

Visibility Qualifiers and Methods By default class methods are also accessible to some classes but not others Visibility qualifiers should also be used in method declarations Examples: public void f( ) { // Any client using object o // can send it a message // by writing o.f( ) private void g( ) { // No client can send g to // the object except another // method from this class

Visibility and UML Diagrams In UML diagrams –private variables and methods are indicated using a leading minus sign as a prefix –public variables and methods are indicates using a leading plus sign as a prefix –a blank prefix means that the variables and methods are not annotated and will have their default visibility

Mutation An object can be changed by assigning a new value to one or more of its instance variables Example: d = new DrawingBox(); c = new Clock(); c.set(10, 20); c.display(d, 50, 50, 50); c.set(5, 40);

Mutability Transforming an object from one state to another Only objects can be mutated, primitive values are not (e.g. x+4 does not change x) Objects are only mutable if its interface includes mutating methods

Nonmutation Creating a new object similar to original, but including the desired change Note the return type is Clock, not void Example: public Clock set_nonmut (int hour, int Minute) { return new Clock(hour, minute); }

What would happen? Consider the effects on the heap if the following sequence of statements was executed Clock c1 = new Clock(); Clock c2 = c1; c1.set(4, 30); c2.set(5, 40);

Heap After Assigning c1 to c2

Class Association Used to achieve certain desired behavior Association (or acquaintance) –classes and objects are linked in a manner that allows the object to remain visible to clients –classes can be linked when a client passes an object as a message argument to another object

Object Containing Reference to Another Object

UML Class Diagram for Clock-DrawingBox Association

Class Containment Composition (or aggregation) –an object of one class is constructed from objects of other classes so that only the composite object is visible to clients –the component objects are internal to the containing object Aggregation is a weaker form of containment and will not be used in this text

UML Class Diagram for Clock-DrawingBox Composition

Representation Independence In OOP the representation of data is encapsulated, this means that the data representation may be changed without affecting the previous class behavior Clients should not be able to detect that a change has occurred Sometimes this is know as implementation independence

main Every Java program needs a class containing a static method called main Static methods (or class methods) are special methods that do not require receivers Static methods cannot refer to class instance variables, but can be invoked when no class instances exist Static methods will be discussed in more detail in Chapter 10