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.

Slides:



Advertisements
Similar presentations
Looking inside classes Fields, Constructors & Methods Week 3.
Advertisements

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
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.
Chapter 8: User-Defined Classes and ADTs J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
INTRODUCTION TO JAVA PROGRAMMING Chapter 1. What is Computer Programming?
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Object-Oriented Analysis and Design
Chapter 2 Classes and Methods I Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Object Oriented Software Development
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.
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 8 More Object Concepts
Writing Classes (Chapter 4)
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
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.
Chapter 8: User-Defined Classes and ADTs
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
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.
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 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
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.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Java Programming: Guided Learning with Early Objects
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Chapter 4: Writing Classes
Creating and Using Classes
User-Defined Classes and ADTs
Inheritance Basics Programming with Inheritance
Corresponds with Chapter 7
Chapter 8: User-Defined Classes and ADTs
Computer Programming with JAVA
Defining Classes and Methods
Object Oriented Programming in java
Presentation transcript:

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. Reingold

Chapter Preview In this chapter we will: formally introduce the class construct as it is used in the Java language describe selected class components (instance variables, constructors, instance methods) demonstrate the use of classes to subdivide large programs discuss the concepts of mutability and visibility introduce the concept of operator overloading

Objects Composed of –values known as attributes or instance variables –operations known as instance methods that can be performed on these values Encapsulate data values within a single entity Their behavior is often generalized to allow reuse in other programs Inheritance is the OOP mechanism that allows one object to serve as a basis for another

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

Rights of a Class To define the public interface for the class To hide the class implementation details from the client To protect “internal” data from access by the client To change the implementation details at any time (provided that the public interface remains intact) To change the public interface with the concurrence of the client

Building Class Definitions public class name { declarations of instance variables constructor definitions method definitions }

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);

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 other by the number or types of its parameters For example OutputBox has several print methods, each taking a different type of parameter (int, double, String, boolean)

Methods Calling Methods Sometimes to allow transparent access of a new method by a client overloading can be used For example in Clock we might rewrite the original display method to call a different variant public void display (DrawingBOX d, int r) { display (d, d.getDrawableWidth()/2, d.getDRawableHeight()/2, r); }

this - Disambiguating Identifiers “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 – Changing 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 and Instance Variables By default class instance variables are accessible to some classes but not others Using visibility qualifiers in instance variable declarations allows you to enforce information hiding from the client Examples: public int x; // Any client using object o can // access x by writing o.x private int y; // No client can access y without // using the class methods

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 Object is changed by assigning new values to one or more the object’s instance variables Example: d = new DrawingBox(); c = new Clock(); c.set(10,20); c.display(d, 50, 50, 50); c.set(5, 40);

Non-mutation New object is created that is similar to old object, except for required changes The methods within the class must create new instances of the class Example: public Clock set_nonmut (int hr, int min) { return new Clock(hr, min); } Note: return type is “Clock” not “void”

Class Composition 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 Containment (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

Need figure 5.6 here

Need figure 5.7 here

Need figure 5.8 here

Need figure 5.9 here

main Special methods called static methods or class methods –have no receiver –are not permitted to refer to instance variables –can be invoked when no class instances exist “main” is a static method “main” is the first method invoked by the Java runtime environment when an application begins execution In many applications “main’s” first (and only) action is to create one or more objects and then invoke it’s methods