Chapter 6 - A First Look at Classes

Ace your homework & exams now with Quizwiz!

UML Data Type and Parameter Notation

> Access modifiers are denoted as: + public - private > Variable types are placed after the variable name, separated by a colon. > Method return types are placed after the method declaration name, separated by a colon. > Method parameters are shown inside the parentheses using the same notation as variables.

Classes: Where Objects Come From

A class is code that is a description of an object. It specifies the data that an object can hold ( the object's fields), and the actions that an object can perform ( the object's methods). You can think of class as a code "blueprint" that can be used to create a particular type of object. It serves a similar purpose as blueprint of a house. Each object that is created from a class is called an *instance* of the class.

Constructors

A constructor is a method that is automatically called when an object is created. Constructors are used to perform operations at the time an object is created. They typically initialize instance fields and perform other object initialization tasks. They are called "constructors" because they help construct an object. *Constructors have a few special properties that set them apart from normal methods: * > Constructors have the same name as the class. > Constructors have no return type (not even void). > Constructors may not return any values. > Constructors are typically public.

No-Arg Constructor

A constructor that does not accept arguments.

Method's Signature

A method signature consists of the method's name and the data types of the method's parameters, in the order that they appear. The return type is not part of the signature.

Accessor Method AKA "getters"

A method that gets a value from a class's field but does not change it.

Mutator Method AKA "setters"

A method that stores a value in a field or changes the value of a field in some other way.

Shadowing

A parameter variable is, in effect, a local variable. Within a method, variable names must be unique. A method may have a local variable with the same name as an instance field. This is called shadowing. The local variable will hide the value of the instance field. Shadowing is discouraged and local variable names should not be the same as instance field names

Access Specifiers

An access specifier is a Java keyword that indicates how a field or method can be accessed. * Public * When the public access specifier is applied to a class member, the member can be accessed by code inside the class or outside. * Private * When the private access specifier is applied to a class member, the member cannot be accessed by code outside the class. The member can be accessed only by methods that are members of the same class.

Objects and Classes

An object is a software component that exists in memory, and performs a specific task in a program. An object is created from a class that contains code describing the object. In software, an object has 2 general capabilities: 1) An object can store data. This data is often known as fields 2) An object can perform operations. These operations are known as methods. Examples of objects you have previously learned about: Scanner object: Used if you need to read input from kb Random object: Used if you need to generate random numbers PrintWriter object: Used if you need to write output to a file - When a program needs the services of a particular object, it creates that object in memory, and then calls that object's method's as necessary.

Data Hiding

Data hiding is an important concept in object-oriented programming. - An object hides its internal data from code that is outside the class that the object is an instance of. - Only the class's methods may directly access and make changes to the object's internal data - Code outside the class must use the class's public methods to operate on an object's private fields.

Instance Fields and Methods

Each instance of a class has its own set of fields, which are known as instance fields. You can create several instances of a class and store different values in each intance's fields. The methods that operate on an instance of a class are known as instance methods. (methods that are not declared with a special keyword, static)

Explicit and Wildcard import statements

Explicit imports name a specific class import java.util.Scanner; Wildcard imports name a package, followed by an * import java.util.*;

Class Constructor Overload

If we were to add the no-arg constructor we wrote previously to our Rectangle class in addition to the original constructor we wrote, what would happen when we execute the following calls? > The first call would use the no-arg constructor and box1 would have a length of 1.0 and width of 1.0. > The second call would use the original constructor and box2 would have a length of 5.0 and a width of 10.0.

Scope of Instance Fields

Instance fields are visible to all of the class's instance methods. Variables declared as instance fields in a class can be accessed by any instance method in the same class as the field. > If an instance field is declared with the public access specifier, it can also be accessed by code outside the class, as long as an instance of the class exists.

String class Constructor

One of the String class constructors accepts a string literal as an argument. This string literal is used to initialize a String object. For instance: String name = new String("Michael Long"); > This creates a new reference variable 'name' that points to a String object that represents the name "Michael Long" Because they are used so often, String objects can be created with a shorthand: String name = "Michael Long";

Uninitialized Local Reference Variables

Reference variables can be declared without being initialized. Rectangle box; This statement does not create a Rectangle object, so it is an uninitialized local reference variable. A local reference variable must reference an object before it can be used, otherwise a compiler error will occur. After declaring the reference variable, the following statement can be used to assign it the address of an object. box = new Rectangle(7.0, 14.0); box will now reference a Rectangle object of length 7.0 and width 14.0.

Creating an Object:

Step 1) You declare a reference variable. Step 2) You create the object in memory, and assign its memory address to the reference variable. - After you do these 2 steps, you can use the reference variable to work with the object. - Example of how you create an object from the Random class: Random rand = new Random(); > Random rand declares a variable named rand, which can be used to reference an object of the Random type > new Random() The new operator creates an object from the Random class, and returns that object's memory address.

Packages and import Statements

The classes in the Java API are organized into packages. An import statement tells the compiler which package a class is located in.

java.lang

The java.lang package is automatically made available to any Java class. Automatically imported into every Java Program.

Binding

The process of matching a method call with the correct method is known as binding. The compiler uses the method signature to determine which version of the overloaded method to bind the call to.

Overloading Methods and Constructors

Two or more methods in a class may have the same name as long as their parameter lists are different. When this occurs, it is called *method overloading*. This also applies to constructors. Method overloading is important because sometimes you need several different ways to perform the same operation. public int add(int num1, int num2) { int sum = num1 + num2; return sum; } public String add (String str1, String str2) { String combined = str1 + str2; return combined; }

Default Constructor

When an object is created, its constructor is *always* called. If you do not write a constructor, Java provides one when the class is compiled. The constructor that Java provides is known as the default constructor. > It sets all of the object's numeric fields to 0. > It sets all of the object's boolean fields to false. > It sets all of the object's reference variables to the special value null.

Passing Objects as Arguments

When an object is passed as an argument to a method, the object's address is passed into the method's parameter variable. As a result, the parameter variable references the object and the receiving method has access to the object.

Stale Data

When the value of an item is dependent on other data and that item is not updated when the other data is changed, it is said that the item has become stale. - Data that requires the calculation of various factors has the potential to become stale. - To avoid stale data, it is best to calculate the value of that data within a method rather than store it in a variable

Writing a Simple Class

You can write your own classes to create the objects that you need in a program. 1) Draw a UML, Unified Modeling Language, diagram. Top: Class Name, Middle: Fields (Variables), Bottom: Methods 2) Use an editor to create a new file name. Ex. Rectangle.java. In the Rectangle.java file we will start by writing a general class "skeleton": public class Rectangle{} In general, terms, fields, and method's that belong to a class are referred to as the class's members.


Related study sets

Industrial Psych Exam One (Chpts. 1-6)

View Set

Advantages and disadvantages of political pressure

View Set

49. Intro Ind & comp analysis (Web + Sch Note)

View Set

PN Nursing Care of Children Online Practice 2020 A with NGN

View Set

Ch. 5 - Networking Concepts (Quiz)

View Set