 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,

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

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Writing Classes (Chapter 4)
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.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
(c) University of Washington01-1 CSC 143 Java Programming as Modeling Reading: Ch. 1-6.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
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 4 Introduction to Classes, Objects, Methods and strings
Inheritance A Review of Objects, Classes, and Subclasses.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
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 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.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Non-Static Classes What is the Object of this lecture?
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 3 Implementing Classes
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Re-Intro to Object Oriented Programming
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Object-Oriented Programming: Classes and Objects
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming: Classes and Objects
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.
Object Based Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Classes and Methods
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Introduction to Classes and Objects
More on Classes and Objects
Defining Classes and Methods
JAVA CLASSES.
Kinds of methods accessor: A method that lets clients examine object state. Examples: distance, distanceFromOrigin often has a non-void return type mutator: A.
Object Oriented Programming in java
Defining Classes and Methods
Classes CS 21a: Introduction to Computing I
Defining Classes and Methods
Classes and Objects Systems Programming.
Presentation transcript:

 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account, bus, etc,etc,etc…  An object is an idea that represents a real- world object  Objects are characterized by state and behavior

 A person has a state described by their weight, height, eye color, hair color, profession, race, ethnicity, etc, etc, etc  People also have behaviors, of which there are probably millions. Speaking, thinking, reading, running, jumping, etc, etc, etc  A variable that represents an object is called an object reference

 A class is a software blueprint (a template, or outline) for implementing objects  An object is considered an instance of a class ◦ Usually have many different instances of one class in a program  The current state of an object is maintained in it’s data fields or instance variables

 A class essentially defines a new data type. We have used classes before ◦ Scanner class ◦ JButton, JFrame, Jpanel ◦ Random class  A class as a blueprint never has a ‘main’ method

 From now on, we need to differentiate between the classes we have coded in the past with main methods, and the classes that we’ll be using now to define objects  A class that holds a blueprint for an object will be referred to as simply a class. These classes don’t ‘run’ or ‘compile’  A class that has a main method is what we will use to test our classes. These will be called either Driver programs or Client programs.

 3 types of methods: ◦ Constructors ◦ Accessors ◦ Mutators  All method headers, with the exception of constructors, look like this: (access specifier) (return type) (name) (parameter list)

 The access specifier tells which other methods can call the method  A return type of void signals that the method does not return a value  Items in the parameter list are separated by commas

 Creates/initializes an object of the class  Easily recognizable and distinguishable from other methods: ◦ Same name as the class ◦ No return type (not even the word void)  Different constructors provide different ways initializing class objects  Default constructors & value constructors

 Has no arguments. It provides reasonable initial values for an object  Bank Account default constructor implementation  In a client program: BankAccount b = new BankAccount();  Constructs a BankAccount object with a balance of 0 and a password equal to the empty string.

 Constructor with parameters that actually initializes the instance variables of the object with values  Bank Account value constructor implementation  In a client program: ◦ BankAccount c = new BankAccount(“Mr. K”, 1000);  Creates a BankAccount object with Mr. K as the password, and 1000 as the balance

 An accessor method accesses a class object without altering the object  Used to return some information about the object into the client program  BankAccount accessor implementation  In a client program: ◦ BankAccount b1 = new BankAccount(“PW1”,500.0); ◦ BankAccount b2 = new BankAccount(“PW2”,600,0); ◦ if (b1.getBalance() > b2.getBalance()) ◦ ……

 The. operator (dot operator) indicates the following method (getBalance()) is a method of the class to which b1 and b2 belong, that is, the BankAccount class.  You know where to look to find the method if you know what type of object called it

 A mutator method changes the state of an object by modifying at least one of its instance variables  BankAccount mutator implementation  In a client program: ◦ b1.withdraw(“PW1”, 200.0); ◦ b2.deposit(“PW2”,37.89);

 Outputting your object normally will give you the class name followed by a memory location of that object:  You need to write your own toString() method for each of your classes that will format your object appropriately

 Header: ◦ public String toString()  For Bank account, maybe: ◦ return “Bank Account: balance = $” +myBalance;  Now: ◦ BankAccount b = new BankAccount(600); ◦ System.out.print(b); will produce: ◦ Bank Account: balance = $600

 A static variable contains a value that is shared by all instances of the class  Does not pertain/refer to any particular instance of the class  Typical uses: ◦ Keep track of statistics for objects of the class ◦ Accumulate a total ◦ Keep track of total number of objects created

 A static final variable cannot be changed.  Capitalized, often declared public  Keyword static indicates there is a single value that applies to whole class, instead of a new instance for each object of the class  In a client class: ClassName.VARIABLE ◦ Math.PI ◦ BankAccount.OVERDRAWN_PENALTY