Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.

Slides:



Advertisements
Similar presentations
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 6 Introduction to Defining Classes
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
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.
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.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Chapter 10 Classes Continued
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
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 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
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.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Introduction to Defining Classes
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes, Interfaces and Packages
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Topics Instance variables, set and get methods Encapsulation
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.
OOP Basics Classes & Methods (c) IDMS/SQL News
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
1 Guide gus; Creates a “mailbox” to hold the address of a Guide object. null gus.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Topic: Classes and Objects
Java Primer 1: Types, Classes and Operators
Intro To Classes Review
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Creating Your OwnClasses
Chapter 4: Writing Classes
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Corresponds with Chapter 7
Chapter 4: Writing classes
Defining Classes and Methods
JAVA CLASSES.
Object Oriented Programming in java
Chapter 7 Objects and Classes
Presentation transcript:

Chapter 10 Defining Classes

The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed and modified only by means of operations Class – software package or template that describes the characteristics of similar objects –These characteristics are of two sorts: Variable declarations that define and object’s data requirements Methods that define its behavior in response to messages Encapsulation – the combining of data and behavior into a single software package –An object is an instance of its class Instantiation – the process of creating a new object

Characteristics of an Object 1)An object has behavior as defined by the methods of its class 2)An object has state, which is another way of saying that at any particular moment its instance variables (explained later) have particular values 3)An object has its own unique identity, which distinguishes it from all other objects in the computer’s memory

Types of Variables 1)Primitive – contains a value -int, double, boolean, char, and the shorter and longer versions of these 2)Reference – contains a pointer to an object containing data stored elsewhere -all classes, for instance, String, reader, etc.. -Think of a reference variable as variable that stores an address to an object

The Structure of a Class Template All classes have a similar structure consisting of four parts: 1)The class’s name and some modifying phrases 2)A description of the instance variables 3)One or more methods that indicate how to initialize a new object (called constructor methods) 4)One or more methods that specify how an object responds to messages

Class Template public class extends { //Declaration of instance variables private ; …. //Code for the constructor method public { //initialize the instance variables …. } //Code for the other methods public ( ) {.... } //more methods }

Instance Variables Instance Variable – Storage for data in an instance of a class – Variable that can be used in any method (global variable for the class) – Instance variables are nearly always declared to be private. This prevents clients from referencing to the instance variables directly. Making instance variables private is an important aspect of information hiding

Clients, Servers, and Interfaces **When messages are sent, two objects are involved** client – sender of the message –Ex: You the user, another method, etc… server – receiver of the message –Ex: Object or method you are calling **A client’s interactions with a server are limited to sending it messages, so consequently a client needs to know nothing about the internal workings of a server** **A client needs to know only a server’s interface, or, the list of the methods supported by the server**

Visibility Modifiers public and private are examples of visibility modifiers – decides who is able to refer to them Methods are usually declared to be public, which allows clients to refer to them.

Constructor Methods The principal purpose of a constructor is to initialize the instance variables of a newly instantiated object. Constructors are activated when the keyword new is used and at no other time. A class template can include more than one constructor, provided each has a unique parameter list; however, all the constructors must have the same name, the name of the class. If a class template has no constructors, the JVM provides a default constructor behind the scenes. The constructor initialized numeric variables to zero and object variables to null.

Accessor Method Accesses a class object without altering the object. Returns some information about the object. Ex: method that returns a value or a method that prints

Mutator Method Changes the state of an object by modifying at least one of its instance variables Ex: Method that receives a value from a client and makes a change to an instance variable

Scope The scope of a variable or method is the region in which that variable or method is visible and can be accessed.

Static Methods vs. Instance Methods Instance Methods – operate on individual objects of a class –Ex: constructors, accessors and mutators Static Methods – perform an operation for the entire class, not its individual objects –Ex: when the client and server are the same class

Method Overloading When two or more methods in the same class have the same name but different parameter lists. Two methods can be named the same as long as the parameter lists are different. –Example: public int product(int number1, int number2) public int product(double number1, double number2) The return type of the method is irrelevant This allows a choice of ways to initialize objects of a class.

equals Method vs. == The equals method is used when we need to compare the content of the text present in the String objects. This method returns true when two String objects hold the same content (i.e. the same values). The == operator is used when we have to compare the String object references. If two String variables point to the same object in memory, the comparison returns true. Otherwise, the comparison returns false. Note that the ‘==’ operator does not compare the content of the text present in the String objects. It only compares the references the 2 Strings are pointing to.