Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Final and Abstract Classes
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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)
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 8 More Object Concepts
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
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.
Chapter 8: Arrays.
1 Java Programming Using Methods, Classes, and Objects.
Inheritance in the Java programming language J. W. Rider.
ECE122 Feb. 22, Any question on Vehicle sample code?
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.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating 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.
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 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
Interfaces and Inner Classes
 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.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
3 Introduction to Classes and Objects.
Object-Oriented Programming: Classes and Objects
Java Primer 1: Types, Classes and Operators
Yanal Alahmad Java Workshop Yanal Alahmad
More About Objects and Methods
Chapter 3: Using Methods, Classes, and Objects
Selenium WebDriver Web Test Tool Training
University of Central Florida COP 3330 Object Oriented Programming
Road Map Introduction to object oriented programming. Classes
Object-Oriented Programming: Classes and Objects
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Interfaces.
Java Programming, Second Edition
Object Oriented Programming in java
CIS 199 Final Review.
Final and Abstract Classes
SPL – PS3 C++ Classes.
Presentation transcript:

Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects

In this chapter, you will:  Create methods with no arguments, a single argument, and multiple arguments  Create method that return values  Learn about class concepts  Create a class  Use instance methods  Declare objects  Organize classes  Use constructors

Methods  A method is a series of statements that carry out a task  Any class can contain an unlimited number of methods

Methods Methods must include:  A declaration  An opening curly brace  A body  A closing brace

Methods Method declarations contain:  Optional access modifiers  The return type for the method  The method name  An opening parenthesis  An optional list of method arguments  A closing parenthesis

Access Modifiers Access modifiers for a method can be:  public- most often methods are given public access  private – can’t be used by all classes  protected – can’t be used by all classes  static – objects aren’t required in order for these methods to be used

Creating Methods that Require a Single Argument  Arguments- Are communications to a method (parameters)  Implementation hiding- Allows that the invoking program must know the name of the method and what type of information to send it, but the calling program does not need to know how the method works

Creating Methods that Require a Single Argument The method declaration must include:  The type of the argument  A local name for the argument  For example: public void predictRaise (double moneyAmount)

Calling a Method  Can use either argument when calling the method & sending the parameter:  Constant  Variable  predictRaise(472.55)  predictRaise(mySalary)

Creating Methods that Require Multiple Arguments  Methods can require more than one argument  Pass multiple arguments by:  Listing the arguments in the call to the method  Separating them with commas  The declaration for a method that receives two or more arguments must list the type for each argument separately public void predictRaise(double moneyAmount, float balance)

A Method’s Return Type  The return type is known as the method’s type For example: public static void nameAndAddress()  This method is public and void means it returns no value

Return Statement  The return statement is the last statement in a method return balance;  Usually you use the returned value, but this is not required  The return statement sends back a value to the calling program

Learning about Class Concepts In object-oriented programming:  Everything can be considered an object  An object is an instantiation of a class, or a tangible example of a class  Every object is a member of a class  Your desk is an object and is a member of the Desk class  These statements represent is-a relationships

Learning about Class Concepts The concept of a class is useful because: Objects inherit attributes from classesObjects inherit attributes from classes All objects have predictable attributes because they are members of certain classesAll objects have predictable attributes because they are members of certain classes You must: Create the classes of objects from which objects will be instantiated (Car class)Create the classes of objects from which objects will be instantiated (Car class) Write other classes to use the objects (a program/class is written to drive to the airport & uses the car class to create a car object to drive)Write other classes to use the objects (a program/class is written to drive to the airport & uses the car class to create a car object to drive)

Creating a Class You must:  Assign a name to the class  Determine what data and methods will be part of the class To begin, create a class header with three parts:  An optional access modifier  The keyword class  Any legal identifier you choose for the name of the class

Access modifiers for Classes Access modifiers include:  public  This is the most used modifier  Most liberal form of access  Can be extended or used as the basis for other classes  final- used only under special circumstances  abstract- used only under special circumstances

Field/Variable Modifiers  Private  Public  Static  Final

Field Modifiers  Private  No other classes can access a field’s values  Only methods of the same class are allowed to set, get, or otherwise use private variables  Highest level of security  Also called information hiding  Provides a means to control outside access to your data

Using Instance Methods  Methods used with object instantiations are called instance methods  You can call class methods without creating an instance of the class (these methods have static as a modifier)  Instance methods require an instantiated object.

Declaring Objects To declare an object:  Supply a type and an identifier  Allocate computer memory for the object  Use the new operator Car myCar = new Car( ); Or Car myCar = new Car(blue, V8); The first example uses the defaults as the attribute values & the second sets the color & engine attributes

Organizing Classes  Most programmers place data fields in some logical order at the beginning of a class  For example, use a unique identifier for each employee  empNum  Last names and first names are organized together

Using Constructor Methods  Constructor methods- Methods that establish an object Employee chauffer = new Employee();  Calls a method named Employee()  When classes are written for the purpose of creating objects, they do not have a main method. Instead, they have a constructor method that sets the default attribute values.

Attribute Default Values  Numeric == 0  Character == Unicode ‘\u0000’  Boolean == false  Object types == null