22
COP3804 - INTERMEDIATE JAVA Designing Classes

COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Embed Size (px)

Citation preview

Page 1: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

COP3804 - INTERMEDIATE JAVA

Designing Classes

Page 2: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Class• Template or blueprint for creating objects. Their definition includes

the list of properties (fields) and behavior (methods) that the objects created from it will have.

• Class Declaration Syntax:accessSpecifier class ClassName

{

variables (instance or class)

constants

constructors

methods

}

• accessSpecifier may be public (visible from any class) or not specified, in which case it’s visible from within the package.

Page 3: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Object• Software bundle of related state (fields) and behavior

(methods).

• They get all the properties and behavior that were defined in the class from which they were created.

• They are created using constructors.

• Syntax to create new objects:

new ClassName(parameters)

Page 4: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Instance Variable Declaration• Instance variables, also known as non-static fields, store values that

are unique to each instance of a class.

• Declaration syntax:

accessModifier dataType variableName

Example:

private String letterGrade;

• Access modifiers determine what classes can access the field:• public: it can be accessed from all classes.• protected: it can be accessed within the same package and sub-classes.• No modifier: it can be accessed within the same package.• private: it can only be accessed within its own class.

• They are usually declared private. Then, public methods may be provided to set and get the value of these fields.

Page 5: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Data Encapsulation• The process of hiding the internal state of objects by

making its fields private and requiring all interaction to be performed through the object's public methods.

• This way allows for the methods to perform data validation before changing a field.

Page 6: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Static Variable Declaration• Static variables are also called class variables.

• There is only one copy of this variable to be shared among all objects of the class.

• They are declared using the static keyword.

• Declaration syntax:

accessModifier static dataType variableName

Example:

public static double interestRate = 5;

Page 7: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Instance vs. Static Variables

public class BankAccount

{

private double balance;private int accountNumber;private static int lastAssignedNumber = 1000;

}

Page 8: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Instance vs. Static Variables

Page 9: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Class Constants• Values that do not change throughout program execution.• Identified with the reserved word final.• They may be declared as public since they cannot be

modified.• It makes sense to make them static so that there is a

single copy for all objects.• Declaration syntax:

accessModifier static final dataType CONSTANT_NAMEExample:

public static final int VALUE_OF_A = 4;

Page 10: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Constructors• Used to create objects from the class blueprint and return their

address in memory.

• Their declaration looks like a method declaration but they use the name of the class and have no return type.

• Their main objective is to initialize the fields of an object.

• Syntax: new ClassName(parameters)

Note: The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class.

Page 11: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Constructors• If a class declaration does not include any constructor,

Java automatically provides a default, no-argument constructor.

• The default constructor sets all numeric fields to 0, boolean fields to false, and reference fields to null.

• Constructors can be overloaded, meaning, there could be multiple constructors as long as their signature is different.

note: signature refers to the number and data type of the parameters

Page 12: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Methods• Methods can manipulate the internal data of an object (its

fields).• Syntax of a method implementation:

accessSpecifier returnType methodName(parameters)

{

// body of the method

}

• Methods can be accessors, used to access the object’s fields without changing their value, or mutators, used to change the value of an object’s fields.

Page 13: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Syntax of a Method ImplementationaccessSpecifier returnType methodName(parameter list)

{

// body of the method

}

• Examples: public String getLetterGrade()

{

return letterGrade;

}

private String toProperCase(String str)

{

}

Page 14: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Accessor vs. Mutator Methods• Accessor methods are those class methods that return

the value of the fields without changing them.

• Mutator methods are those class methods that change the value of fields.

• It is common practice to make all the class fields private and provide methods to get their values and methods to set their values.

• Sometimes, accessor methods are called “getters” and mutator methods are called “setters”.

Page 15: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Parameter vs. Argument• Parameters refers to the list of variables in a method

declaration.

• Arguments are the actual values that are passed in when the method is invoked.

• When you invoke a method, the arguments used must match the declaration's parameters in type and order.

Page 16: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Call by value vs. call by reference• Parameter variables come into existence when the method

starts getting executed and they cease to exist when the method finishes.

• As the method starts, the parameter variable is set to the same value as the corresponding argument.

• If the parameter variable gets modified inside the method, that has no effect on the argument because they are separate variables.

• If the argument is a reference to an object, then mutator methods may be used inside the method to modify the state of the object.

Page 17: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Commonly Included MethodsMany classes provide an implementation for the following methods:

• toString - it returns a String representing the state of an object (the data stored in the object’s fields). It gets called implicitly when an object is passed as an argument to print or println, or when using the concatenation operator. Providing an implementation for this method overrides the one defined in the Object class.

• equals – compares the contents of two objects of the same class: the object calling the method and the object being passed as an argument. Providing an implementation for this method overrides the one defined in the Object class.

• copy – it creates a new object and sets the fields to the same values as the ones in the object calling the method.

Page 18: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Method and Constructor Overloading

Some classes provide several constructors with different parameter lists. Also, several methods with the same name may be provided, as long as they have a different parameter list.

This concept is called overloading and it makes classes more flexible in the sense that they give the user of the class more options on how to call the overloaded methods or constructors.

Page 19: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Shallow Copy vs. Deep Copy

Page 20: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Packages• Used to organize related classes and interfaces.

• Classes that are declared in other packages need to be imported.

• Syntax to import a class:

import packageName.ClassName;

Page 21: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Garbage Collection• The Java platform allows you to create objects without

having to worry about destroying them when you no longer need them.

• The Java runtime environment deletes objects when it determines that they are no longer being used. This process is called garbage collection.

Page 22: COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

References

• Horstmann, Cay. Big Java 4th ed. New York, USA: John Wiley & Sons, Inc., 2010.

• Oracle. The Java Tutorials, 2013. Web. 25 Aug. 2013. http://docs.oracle.com/javase/tutorial/index.html

• Gaddis, Tony, and Godfrey Muganda. Starting out with Java: from Control Structures through Data Structures 2nd ed. Boston, USA: Addison-Wesley, 2012