0% found this document useful (0 votes)
23 views

Java_PPT_ch03-2

Uploaded by

Hol Gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Java_PPT_ch03-2

Uploaded by

Hol Gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Chapter 3:cont..

Using Methods, Classes,


and Objects
2 Learning About Class
Concepts
 Every object is a member of a class
 Is-a relationships
 Object “is a” concrete example of the class
 Instantiation
 Reusability

Java Programming, Sixth Edition


3 Learning About Class
Concepts (cont'd.)
 Methods often called upon to return piece of
information to source of request
 Class client or class user
 Application or class that instantiates objects of another
prewritten class

Java Programming, Sixth Edition


4 Creating a Class

 Assign name to class


 Determine what data and methods will be part of
class
 Class header
 Optional access modifier
 Keyword class
 Any legal identifier for the name of class
 public class
 Accessible by all objects

Java Programming, Sixth Edition


5 Creating a Class (cont'd.)

Java Programming, Sixth Edition


6 Creating a Class (cont'd.)

 Extend
 Use as a basis for any other class
 Data fields
 Variables declared within class
 But outside of any method

Java Programming, Sixth Edition


7 Creating a Class (cont'd.)

 private access for fields


 No other classes can access field’s values
 Only methods of same class allowed to use private
variables
 Information hiding
 Most class methods public

Java Programming, Sixth Edition


8 Creating Instance Methods
in a Class
 Classes contain methods
 Nonstatic methods
 Instance methods
 “Belong” to objects
 Typically declare nonstatic data fields
 static class variables
 Are not instance variables

Java Programming, Sixth Edition


9

Java Programming, Sixth Edition


10 Creating Instance Methods
in a Class (cont'd.)

Java Programming, Sixth Edition


11 Declaring Objects and Using
Their Methods
 Declaring class does not create any actual objects
 Create instance of class
 Supply type and identifier
 Allocate computer memory for object
 Use new operator
Employee someEmployee;
someEmployee = new Employee();
 Or
Employee someEmployee = new Employee();

Java Programming, Sixth Edition


12 Declaring Objects and Using
Their Methods (cont'd.)
 Reference to the object
 Name for a memory address where the object is held
 Constructor method
 Method that creates and initializes class objects
 Can write own constructor methods
 Java creates a constructor
 Name of constructor method always same as name of
class

Java Programming, Sixth Edition


13 Declaring Objects and Using
Their Methods (cont'd.)
 After object instantiated
 Methods accessed using:
 Object’s identifier
 Dot
 Method call

Java Programming, Sixth Edition


14 Declaring Objects and Using
Their Methods (cont'd.)

Java Programming, Sixth Edition


15 Understanding Data Hiding

 Data hiding using encapsulation


 Data fields are usually private
 Client application accesses them only through public
interfaces
 Set method
 Controls data values used to set variable
 Get method
 Controls how value is retrieved

Java Programming, Sixth Edition


16 An Introduction to Using
Constructors
Employee chauffeur = new Employee();
 Actually calling method named Employee()
 Default constructor
 Requires no arguments
 Created automatically by Java compiler
 For any class
 Whenever you do not write constructor

Java Programming, Sixth Edition


17 An Introduction to Using
Constructors (cont'd.)
 Default constructor provides specific initial values to
object’s data fields
 Numeric fields
 Set to 0 (zero)

 Character fields
 Set to Unicode ‘\u0000’

 Boolean fields
 Set to false

 Nonprimitive object fields


 Set to null

Java Programming, Sixth Edition


18 An Introduction to Using
Constructors (cont'd.)
 Write a constructor method
 Must have same name as class it constructs
 Cannot have return type
 public access modifier

Java Programming, Sixth Edition


19 An Introduction to Using
Constructors (cont'd.)

Java Programming, Sixth Edition


20 Understanding that Classes
are Data Types
 Classes you create become data types
 Declare object from one of your classes
 Provide type and identifier

Java Programming, Sixth Edition


21 You Do It

 Creating a static method that requires no


arguments and returns no values
 Calling a static method from another class
 Creating a static method that accepts arguments
and returns values
 Creating a class that contains instance fields and
methods

Java Programming, Sixth Edition


22 You Do It (cont'd.)

 Creating a class that instantiates objects of another


class
 Adding a constructor to a class
 Creating a more complete class

Java Programming, Sixth Edition


23 Don’t Do It

 Don’t place semicolon at end of method header


 Don’t think “default constructor” means only
automatically supplied constructor
 Don’t think that class’s methods must:
 Accept its own fields’ values as parameters
 Return values to its own fields
 Don’t create class method that has parameter with
same identifier as class field

Java Programming, Sixth Edition


24 Summary

 Method
 Series of statements that carry out a task
 Declaration includes parameter type and local name
for parameter
 Can pass multiple arguments to methods
 Has return type
 Class objects
 Have attributes and methods associated with them
 Instantiate objects that are members of class

Java Programming, Sixth Edition


25 Summary (cont'd.)

 Constructor
 Method establishes object and provides specific initial
values for object’s data fields
 Everything is an object
 Every object is a member of a more general class
 Implementation hiding, or encapsulation
 private data fields
 public access methods

Java Programming, Sixth Edition

You might also like