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

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
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Written by: Dr. JJ Shepherd
Chapter 10 Introduction to Arrays
Chapter 6 Introduction to Defining Classes
Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
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)
ASP.NET Programming with C# and SQL Server First Edition
Chapter 10 Classes Continued
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 12 Support for Object oriented Programming.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
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.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
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 5 Introduction to Defining Classes
Object-Oriented Programming Chapter Chapter
ISBN Object-Oriented Programming Chapter Chapter
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Chapter 16 UML Class Diagrams 1CS6359 Fall 2012 John Cole.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Classes, Interfaces and Packages
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
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.
ISBN Chapter 12 Support for Object-Oriented Programming.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Creating and Using Objects, Exceptions, Strings
Java Primer 1: Types, Classes and Operators
Intro To Classes Review
Chapter 3: Using Methods, Classes, and Objects
Section 11.1 Class Variables and Methods
Chapter 4: Writing Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Corresponds with Chapter 7
Lesson 5: Introduction to Defining Classes
Defining Classes and Methods
Object-Oriented Programming
Object Oriented Programming in java
Presentation transcript:

Chapter 6 Introduction to Defining Classes

Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class and a model class. Use visibility modifiers to make methods visible to clients and restrict access to data within a class. Write appropriate mutator methods, accessor methods, and constructors for a class.

Objectives: Understand how parameters transmit data to methods. Use instance variables, local variables, and parameters appropriately. Organize a complex task in terms of helper methods.

4 Vocabulary accessor actual parameter behavior constructor encapsulation formal parameter helper method identity Instantiation lifetime mutator scope state visibility modifier

The Internal Structure of Classes and Objects ★ An object is a runtime entity that contains data and responds to messages. ★ A class is a software package or template that describes the characteristics of similar objects. ✓ Instance variable declarations which define an 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. ★ Instantiation: The process of creating a new object. The Internal Structure of Classes and Objects

Classes, Objects, and Computer Memory: ★ When a Java program is executing, the computer ’ s memory must hold: ✓ All class templates in their compiled form. ✓ Variables that refer to objects. ✓ Objects as needed. ★ Each method ’ s compiled byte code is stored in memory as part of its class ’ s template. The Internal Structure of Classes and Objects

★ Memory for data is allocated within objects. ★ Although all class templates are in memory at all times, individual objects come and go. ✓ An object occupies memory with it is instantiated, and disappears when no longer needed. ✓ Garbage collection: the JVM process of keeping track of which objects need to be stored and which can be deleted. The Internal Structure of Classes and Objects

Three Characteristics of an Object: ★ Behavior: defined by the methods of its class. ★ State: at any moment the instance variables have particular values, which change in response to messages sent to the object. ★ Identity: distinguish from other objects in memory, as handled by the JVM. The Internal Structure of Classes and Objects

★ Of the variables, there can be none, one, or several. ✓ When there are none, the garbage collector purges the object from memory. The Internal Structure of Classes and Objects

Clients, Servers, and Interfaces: ★ Clients send messages. ✓ Only need to know the server ’ s interface. ✓ Information hiding hides the server ’ s data requirements and list of supported methods from clients. The Internal Structure of Classes and Objects

A Student Class Using Student Objects: ✴ First, declare variables, then assign values to variables before using them. ✴ Mutators: messages that change an object ’ s state. ✴ Accessors: messages that access the object ’ s state. Used to see if a mutator works correctly.

✴ Implicit use of toString when a Student object is sent to a terminal window A Student Class

Objects, Assignment, and Aliasing: ✴ An object can be assigned two variables. ✴ At any time, it is possible to break the connection to a variable and the object it references by assigning the null value to the variable. A Student Class

✴ How variables are affected by assignment statements A Student Class

Primitive Types, Reference Types, and the null Value: ✴ In Java, all types fall into two categories: ➡ Primitive: 1 box that contains a value of primitive type. ✦ int, double, boolean, char, and longer and shorter versions of these. ➡ Reference: a box that contains a pointer to an object. ✦ String, Student, Scanner, and all classes. A Student Class

✴ The difference between primitive and reference variables A Student Class

✴ Can assign reference variables the null value. ➡ If it pointed to an object, and no other variable points to the object, the object ’ s memory goes to garbage collection. The Student variable before and after it has been assigned the value null. A Student Class

✴ Null pointer exception: when a program attempts to run a method with a null object. A Student Class

The Structure of a Class Template: ✴ All classes have a similar structure consisting of 4 parts: ➡ The class ’ s name and some modifying phrases. ➡ A description of the instance variables. ➡ One or more constructor method that indicates how to initialize a new object. ➡ One or more methods that specify how an object responds to messages. A Student Class

✴ Class definitions: usually begin with the keyword public. ✴ Class names: user-defined symbols that adhere to rules for naming variables and methods. A Student Class

✴ Java organizes classes in a hierarchy. ➡ Base: Object. ➡ Superclasses and subclasses. ➡ Each class, except Object, can have one parent and any number of children. A Student Class

✴ Inheritance: a new class inherits the characteristics of its superclass. ➡ Extends the superclass by modifying and adding. ✴ Instance variables are nearly always private. ✴ Visibility modifiers: private and public. ➡ Determine whether clients can see them. A Student Class

✴ When an object receives a message, it activates the corresponding method, which manipulates the object ’ s data as represented by the instance variables. Constructors: ✴ Purpose of a constructor is the initialize the instance variables of a newly instantiated object. A Student Class

✴ Constructors are only ever activated when the keyword new is used. ✴ A class template can have more than one constructor, as long as each has a unique parameter list. ✴ All constructors must have the same name as the class. ✴ Default constructors have empty parameter lists. A Student Class

✴ A class is easier to use when it has a variety of constructors. Chaining Constructors: ✴ Used when a class has several constructors. ✴ Simplifies code by calling one constructor from another: ➡ This( ); A Student Class