Chapter 3 Introduction to Classes and Objects - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Chapter 3 Introduction to Classes and Objects

Description:

Definitions. Encapsulation combining attributes and methods into one group (object) ... Method with the same name as the class ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 23
Provided by: brenda54
Category:

less

Transcript and Presenter's Notes

Title: Chapter 3 Introduction to Classes and Objects


1
Chapter 3Introduction to Classes and Objects
  • Definitions
  • Examples

2
Definitions
  • Object instance of a class
  • Objects have attributes and behaviors
  • Attributes data
  • Behaviors methods or operations

3
Definitions
  • Encapsulation combining attributes and methods
    into one group (object)
  • Information hiding
  • Implementation details are hidden within the
    objects themselves
  • Abstraction another term for information hiding

4
Definition
  • Default package set of classes that are
    compiled in the same directory on disk.
  • We say that all such classes are considered to be
    in the same package
  • Classes in the same package are implicitly
    imported into the source code files of other
    classes in the same package.
  • Import is not required when one class in a
    package uses another class in the same package.

5
Definitions
  • Java classes contain
  • Methods implement operations
  • Fields implement attributes
  • Accessor methods
  • Methods that allow access to internal data fields
  • Example
  • getInterestRate()
  • //method name generally starts with get
  • //highly recommended that it starts with get

6
Definitions
  • Mutator methods methods that change values in
    data fields
  • Example
  • setInterestRate (double x)
  • //method name generally starts with set
  • //highly recommended that mutator methods start
  • //with set

7
Variables
  • Local variables
  • Variables declared in the body of a particular
    method
  • Fields
  • Variables declared inside a class declaration but
    outside the bodies of the classs method
    declarations
  • Also known as instance variables (if not static)

8
Fields instance variables
  • Can be declared as public, private, protected,
    default
  • Generally should be declared as private to
    promote data hiding
  • Methods generally declared as public

9
Example class (Circle.java)
  • public class Circle
  • private double radius 1.0 //field
  • double findArea()
  • return radius radius 3.13159

10
Declaring a reference to an object and creating
an object
  • Declaration Classname objectReference
  • Circle myCircle //myCircle is a reference to a
    Circle //object
  • Create a Circle object
  • myCircle new Circle()
  • Declare and create
  • Circle myCircle new Circle()

11
Circle Class
  • CircleClass.java
  • CircleProgram.java

12
Constructors
  • Method with the same name as the class
  • Allows construction of objects with different
    initial data values initializes object
  • Do not have a return type
  • Invoked using the new operator
  • Circle (double r) Circle ()
  • radius r radius 1

13
Constructors
  • Default constructor
  • Constructor with no parameters
  • If your class has no constructors, Java will
    automatically create a default constructor
  • If your class has a constructor, Java will not
    create a default constructor
  • You should include default constructors in your
    class.

14
Instance Variables
  • Normally should be initialized in constructors
  • Alternative Initialize when declarations are
    made
  • If instance variables are initialized in the
    declaration and you have constructors --- Be
    sure to define a default constructor even if the
    body of the default constructor is empty.
  • The following
  • AClass anObject new AClass()
  • is illegal if no default constructor

15
AClass anObject new AClass()
  • public class AClass
  • private int value 1
  • public AClass (int x)
  • value x
  • public void setValue(int x)
  • value x
  • public int getValue()
  • return value

16
Static Methods methods that do not require a
calling object
  • Uses the keyword static in method heading
  • You cannot do anything inside a static method
    that refers to a calling object
  • You cannot access instance variables from inside
    a static method
  • You cannot call a non-static method inside a
    static method )unless you first create a new
    object and use it as the calling object)
  • You can invoke a static method with an object
  • Confusing and is not normally done.

17
Method Syntax
  • access specifier
  • qualifier
  • return type
  • method name
  • (argument list)

18
Access Specifiers protecting class variables
and methods
19
Static Variables
  • Static variables belong to the class as a whole
    and not just to one object
  • private static int turn
  • Used to communicate between objects
  • One object can change the static variable and
    another object can read that change
  • Should normally be declared as private (unless
    declared as constants)

20
Final Instance Variables
  • Keyword final is used to specify that a variable
    is not modifiable (it is a constant)
  • private final int INCREMENT 5
  • Use all caps when denoting constants

21
Method arguments
  • All arguments are pass by value methods always
    receive a copy of the argument
  • If argument is an object reference (different
    story)

22
Class methods static methods
  • Example
  • Integer.parseInt(s)
  • main (String args)
  • An instance of the class is not necessary to
    access the static method
  • Static methods can only call static methods and
    can only access static variables
  • Method is usable by any code that has access to
    the method regardless of whether any objects of
    the class have been created.
Write a Comment
User Comments (0)
About PowerShow.com