Chapter 9: Objects and Classes

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Pass By Value

Primitive data types pass the actual value printRadius(5.0);

Default Constructor

Provided automatically only if no constructors are explicitly defined in the class.

Local Variable

A variable defined inside a method

Immutable Class

Used to create an immutable object. For a class to be immutable: 1) ALL data fields must be private 2) There can't be any mutator methods for data fields 3) No accessor methods can return a reference to a data field that is mutable

Private

Used to prevent direct modifications of data fields

Instance Variable

dependent on a specific instance

this()

-this(1.0) invokes a constructor -this can be used to reference a hidden data field of the object being constructed. -this() MUST be the first line -can be uses in a no-arg constructor that will then call upon the constructor with argument that matches this(argument)

Constructors (3 rules)

1) A constructor must have the same name as the class itself. 2) Constructors do not have a return type—not even void. 3) Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.

"new" operator

1) Declaration: The code set in bold are all variable declarations that associate a variable name with an object type. 2) Instantiation: The new keyword is a Java operator that creates the object. 3) Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

Class

A template, blueprint, or contract that defines what an objects data fields and methods will be.

(.) dot operator

After an object is created, its data can be accessed and its methods can be invoked using the dot operator (.), also known as the object member access operator.

Object

An instance of a class that has an id, state, and behavior: Id - name State - represented by data fields Behavior - defined by methods

Immutable Object

An object who's contents cannot be changed.

Visibility Modifiers

Public - accessible from any other class Private - accessible only from within its own class Default - accessible by any class in the same package "package-private"

Pass by Sharing

Reference type pass a reference of the object. The object referenced in the method is the same as the object being passed.

Abstraction

Separates class implementation (black box) from how the class is used. [black box]--[class contract*]<--->[client use] *class contract holds signatures of public methods and constants

Static Variables

Store values for the variables in common memory location. Therefore, if one object changes the value of a static variable, all objects of the same class are affected. All instances of a class will share the data of the variable. static int numberOfObjects;

null

The default value of a data field is null for a reference type 0 for a numeric type false for a boolean type \u0000 for a char type.

Encapsulation

The details of implementation are encapsulated and hidden from the user via class abstraction

Calling Object

The object on which an instance method is invoked is called a calling object.

Constructor (usage)

Used to construct objects. ClassName myClass = new ClassName(arguments);

Variables of Primitive Type

When you assign one variable to another, the other variable is set to the same value. For a variable of a primitive type, the real value of one variable is assigned to the other variable.

Accessing Objects

references a data field in the object. objectRefVar.dataField invokes a method on the object. objectRefVar.method(arguments)

this

the name of a reference that an object can use to refer to itself. Use: 1) To reference object's instance members 2) can be used to reference a class' hidden data fields. a) static var: ClassName.staticVariable; b) instance var: this.propertyName;

Static Method

Can be called without creating an instance of the class. CANNOT access instance members of the class. static int getNumberOfObjects(){ return numberOfObjects; }

Array of Objects

Declare and create array of 10 circle objects: Circle[] myCircle = new Circle[10]; Initialize: for(int i = 0; i<myCircle.length; i++){ myCircle[i] = new Circle; }

setter method signature (mutator)

Enables a private data field to be updated. public void setPropertyName(dataType propertyValue)

Variables of Reference Type

Every variable represents a memory location that holds a value. When you declare a variable, you are telling the compiler what type of value the variable can hold. A reference variable is a reference to where an object is located in memory. When you assign one variable to another, for a variable of a reference type, the reference of one variable is assigned to the other variable. EX: The value of Circle object c holds a reference to where the contents of the Circle object are stored in memory.

Hidden Variable

If a local variable has the same name as a class' variable, the local variable takes precedence and the class' variable with the same name is hidden

Class Variables and their scope

Instance and static variables in a class are referred to as the class' variables or data fields. Their scope is the entire class.

Instance vs Static Method Accessibility

Instance methods can only be used after the instances are created via reference variables. CAN invoke instance method, static method/ access instance data field, static data field Static methods and static data can be accessed from a reference variable or class name. CAN invoke static method/access static data field CANNOT invoke instance method/access instance data field* *Static methods and static data fields do not belong to a particular object so they do not have access to instances To access instance method/data field use a reference variable. public static void main(String[] args){ Duck d = new Duck(2); d.getAge(); }

getter method signature (accessor)

Makes private field accessible by having it return its value. public returnType getPropertyName() public boolean isPropertyName()

Reference Variables

Objects are accessed by these which contain references to the object. A class is a reference type, which means that a variable of the class type can reference an instance of the class. ClassName objectRefVar = new ClassName(); The following declares myCircle as Circle type Circle myCircle; The variable myCircle can reference a Circle object. The next statement creates an object and assigns its reference to myCircle: myCircle = new Circle();

Instance Method

can only be invoked on a specific instance

UML diagram format

methodName(parameterName:parameterType):returnType setRadius(newRadius:double):void


Set pelajaran terkait

XCEL STUDY MATERIAL- MISSISSIPPI LAWS AND RULES

View Set

prepU 39 Neurocognitive disorders mental health 243 CSN

View Set

Fat Soluble vitamins (A, D, E, K)

View Set

Chapter 16.2 The Spread of the Protestant Reformation

View Set

Chapter 7: Skeletal System Gross Anatomy Study Guide

View Set

Test Market Failures ECON-2302 Summer 16

View Set

Myers, Psychology for AP Unit 4-1

View Set

General Biology - FINAL Exam (Ch. 15 and 19)

View Set

SW LEA Title 16 - Criminal Proceedings Article 2.5 Peace officers Part 2 Sunrise Review POST 2021

View Set