Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)"— Presentation transcript:

1 COP3804 - INTERMEDIATE JAVA Designing Classes

2 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.

3 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)

4 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.

5 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.

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

7 Instance vs. Static Variables public class BankAccount { private double balance; private int accountNumber; private static int lastAssignedNumber = 1000; }

8 Instance vs. Static Variables

9 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_NAME Example: public static final int VALUE_OF_A = 4;

10 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.

11 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

12 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.

13 Syntax of a Method Implementation accessSpecifier returnType methodName(parameter list) { // body of the method } Examples: public String getLetterGrade() { return letterGrade; } private String toProperCase(String str) { … }

14 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”.

15 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.

16 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.

17 Commonly Included Methods Many 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.

18 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.

19 Shallow Copy vs. Deep Copy

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

21 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.

22 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 2 nd ed. Boston, USA: Addison-Wesley, 2012


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

Similar presentations


Ads by Google