Download presentation
Presentation is loading. Please wait.
Published byKellie Baker Modified over 9 years ago
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 A software bundle that includes data and methods that perform useful operations on the data. The data and methods in an object are the ones that were defined in the class from which they were created. An object created from a class is called an instance of the class. They are created using constructors. Syntax to create new objects: new ClassName(parameters)
4
Data Hiding Data encapsulation refers to the fact that data and code are combined into single objects. 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. Data Encapsulation
5
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. Because of the concept of data hiding, they are usually declared private. Then, public methods may be provided to set and get the value of these fields. These methods are called mutator and accessor methods respectively.
6
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;
7
Constructors Used to create objects from a class 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. They are used to perform operations at the time an object is created and 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.
8
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
9
Methods Methods can manipulate the internal data of an object (its fields). 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.
10
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) { … }
11
Accessor vs. Mutator Methods Accessor methods are those methods that return the value of the fields without changing them. Each field that the programmer wishes to be viewed by other classes needs an accessor. Mutator methods are those class methods that change the value of fields. Each field that the programmer wishes to be modified by other classes needs a mutator. 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”.
12
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.
13
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.
14
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.
15
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;
16
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.
17
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;
18
Instance vs. Static Variables public class BankAccount { private double balance; private int accountNumber; private static int lastAssignedNumber = 1000; }
19
Instance vs. Static Variables
20
Copy Constructors Copy constructors accept an object of the same class as an argument. They set the fields of the object being created to the same values as the fields of the object passed.
21
Aggregation Aggregation occurs when an instance of a class is a field in another class. For example, an instance of the TextBook class is a field in the Course class.
22
Deep Copy vs. Shallow Copy Deep copy – when making a copy of an aggregate object, also make copies of the objects it references (create new objects with the same data inside). Shallow copy - when making a copy of an aggregate object, make a reference copy of the objects it references (copy the memory address). This would allow code outside the class to modify private data inside the class creating a security hole.
23
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.
24
Javadoc The javadoc utility produces documentation similar to the one in the online Java API. It generates HTML files based on the comments you add to the source code. Write javadoc comments before classes, constructors, and method declarations. Begin javadoc comments with /** End javadoc comments with */ For every parameter, include a @param tag followed by the parameter name and its description. Include a @return tag for every method that returns a value.
25
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.