chapter 3 introduction to classes and objects definitions examples

22
Chapter 3 Introduction to Classes and Objects Definitions Examples

Upload: magnus-lester

Post on 18-Jan-2016

245 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 3 Introduction to Classes and Objects Definitions Examples

Chapter 3Introduction to Classes and Objects

Definitions

Examples

Page 2: Chapter 3 Introduction to Classes and Objects Definitions Examples

Definitions

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

Page 3: Chapter 3 Introduction to Classes and Objects Definitions Examples

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

Page 4: Chapter 3 Introduction to Classes and Objects Definitions Examples

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.

Page 5: Chapter 3 Introduction to Classes and Objects Definitions Examples

Definitions

Java classes contain– Methods – implement operations – Fields – implement attributes

Accessor methods– Methods that allows access to internal data fields

Example:

getInterestRate()

//method name generally starts with “get”

//highly recommended that it starts with “get”

Page 6: Chapter 3 Introduction to Classes and Objects Definitions Examples

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”

Page 7: Chapter 3 Introduction to Classes and Objects Definitions Examples

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 class’s method declarations

– Also known as instance variables (if not static)

Page 8: Chapter 3 Introduction to Classes and Objects Definitions Examples

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

Page 9: Chapter 3 Introduction to Classes and Objects Definitions Examples

Example class (Circle.java)

public class Circle

{

private double radius = 1.0; //field

double findArea()

{

return radius * radius * 3.13159;

}

}

Page 10: Chapter 3 Introduction to Classes and Objects Definitions Examples

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 objectmyCircle = new Circle();

Declare and create

Circle myCircle = new Circle();

Page 11: Chapter 3 Introduction to Classes and Objects Definitions Examples

Circle Class

CircleClass.java CircleProgram.java

Page 12: Chapter 3 Introduction to Classes and Objects Definitions Examples

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;} }

Page 13: Chapter 3 Introduction to Classes and Objects Definitions Examples

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.

Page 14: Chapter 3 Introduction to Classes and Objects Definitions Examples

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 but other constructors exist.

Page 15: Chapter 3 Introduction to Classes and Objects Definitions Examples

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;}

Page 16: Chapter 3 Introduction to Classes and Objects Definitions Examples

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.

Page 17: Chapter 3 Introduction to Classes and Objects Definitions Examples

Method Syntax

[access specifier]

[qualifier]

return type

method name

(argument list)

Page 18: Chapter 3 Introduction to Classes and Objects Definitions Examples

Access Specifiers – protecting class variables and methods

Specifier Class Subclass Package World

Private X

Protected X X X

Public X X X X

Package (default)

X X

Page 19: Chapter 3 Introduction to Classes and Objects Definitions Examples

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)

Page 20: Chapter 3 Introduction to Classes and Objects Definitions Examples

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

Page 21: Chapter 3 Introduction to Classes and Objects Definitions Examples

Method arguments

All arguments are pass by value – methods always receive a copy of the argument

If argument is an object reference (different story)

Page 22: Chapter 3 Introduction to Classes and Objects Definitions Examples

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.