object oriented programming
DESCRIPTION
Object Oriented programming. Instructor: Dr. Essam H. Houssein. Immutable Objects and Classes. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/1.jpg)
1
Chapter
10
THINKIN
G IN
OBJECTS
Object Oriented programming
Instructor:Dr. Essam H. Houssein
![Page 2: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/2.jpg)
Immutable Objects and Classes
Normally, you create an object and allow its contents to be changed later. Occasionally it is desirable to create an object whose contents cannot be changed, once the object is created. We call such an object an immutable object and its class an immutable class. The String class, for example, is immutable. If you deleted the set method in the Circle class in Listing 8.9, the class would be immutable, because radius is private and cannot be changed without a set method.
2
![Page 3: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/3.jpg)
3
If a class is immutable, then all its data
fields must be private and it cannot contain
public set methods for any data fields.
![Page 4: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/4.jpg)
4
For a class to be immutable, it must meet the
following requirements:
■ all data fields private;
■ no mutator methods;
■ no accessor method that returns a reference
to a data field that is mutable.
![Page 5: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/5.jpg)
5
The Scope of Variables
Instance and static variables in a class are referred to as the class’s variables or data fields.A variable defined inside a method is referred to as a local variable. The scope of a class’s variables is the entire class, regardless of where the variables are declared. A class’s variables and methods can appear in any order in the class. The exception is when a data field is initialized based on a reference to another data field.
![Page 6: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/6.jpg)
6
You can declare a class’s variable only once, but you can declare the same variable name in a method many times in different non nesting blocks.If a local variable has the same name as a class’s variable, the local variable takes precedence and the class’s variable with the same name is hidden. For example, in the following program, x is defined as an instance variable and as a local variable in the method.
![Page 7: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/7.jpg)
7
![Page 8: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/8.jpg)
8
The this Reference
The this keyword is the name of a reference that refers to a calling object itself. One of its common uses is to reference a class’s hidden data fields. For example, a data-field name is often used as the parameter name in set method for the data field. In this case, the data field is hidden in the set method. You need to reference the hidden data-field name in the method in order to set a new value to it.
![Page 9: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/9.jpg)
9
A hidden static variable can be accessed simply by
using the
ClassName.StaticVariable reference. A hidden
instance variable can be accessed by using the
keyword this, as shown in Figure 10.2(a).
![Page 10: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/10.jpg)
10
![Page 11: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/11.jpg)
11
Another common use of the this keyword is to
enable a constructor to invoke another constructor
of the same class. For example, you can rewrite the
Circle class as follows:
![Page 12: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/12.jpg)
12
![Page 13: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/13.jpg)
13
Class Abstraction and Encapsulation
Java provides many levels of abstraction. Class
abstraction is the separation of class
implementation from the use of a class. The
creator of a class describes it and lets the user know
how it can be used. The collection of methods and
fields that are accessible from outside the class,
together with the description of how these members
are expected to behave, serves as the class’s
contract.
![Page 14: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/14.jpg)
14
The details of implementation are encapsulated and
hidden from the user. This is known as class
encapsulation.
![Page 15: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/15.jpg)
15
Object-Oriented Thinking
Classes provide more flexibility and modularity for building reusable Software. The procedural paradigm focuses on designing methods. The object oriented paradigm couples data and methods together into objects. Software design using the object-oriented paradigm focuses on objects and operations on objects. The object-oriented approach combines the power of the procedural paradigm with an added dimension that integrates data with operations into objects.
![Page 16: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/16.jpg)
16
In procedural programming, data and operations on
the data are separate, and this methodology requires
sending data to methods. Object-oriented
programming places data and the
operations that pertain to them in an object. This
approach solves many of the problems inherent in
procedural programming.
![Page 17: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/17.jpg)
17
The object-oriented programming approach organizes programs in a way that mirrors the real world, in which all objects are associated with both attributes and activities. Using objects improves software reusability and makes programs easier to develop and easier to maintain. Programming in Java involves thinking in terms of objects; a Java program can be viewed as a collection of cooperating objects.
![Page 18: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/18.jpg)
18
Class Design Guidelines
1- CohesionA class should describe a single entity, and all the class operations should logically fit together to support a coherent purpose. You can use a class for students, for example, but youshould not combine students and staff in the same class, because students and staff are different entities.
![Page 19: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/19.jpg)
19
2- Consistency-Follow standard Java programming style and naming conventions. - Choose informative names for classes, data fields, and methods. - A popular style is to place the data declaration before the constructor and place constructors before methods.- naming consistency- no-arg constructor- If you want to prevent users from creating an object for a class, you may declare a private constructor in the class,
![Page 20: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/20.jpg)
20
3- EncapsulationA class should use the private modifier to hide its data from direct access by clients. This makes the class easy to maintain.Provide a get method only if you want the field to be readable, and provide a set method only if you want the field to be updateable.
![Page 21: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/21.jpg)
21
4- ClarityCohesion, consistency, and encapsulation are good guidelines for achieving design clarity.Additionally, a class should have a clear contract that is easy to explain and easy to understand.
![Page 22: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/22.jpg)
22
5- CompletenessClasses are designed for use by many different customers. In order to be useful in a wide range of applications, a class should provide a variety of ways for customization through properties and methods.
![Page 23: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/23.jpg)
23
6- Instance vs. Static
A variable or method that is dependent on a specific instance of the class should be an instance variable or method. A variable that is shared by all the instances of a class should be declared static.
![Page 24: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/24.jpg)
24
Always reference static variables and methods from a class name (rather than a reference variable) to improve readability and avoid errors.Do not pass a parameter from a constructor to initialize a static data field. It is better to use a set method to change the static data field.
![Page 25: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/25.jpg)
25
Instance and static are integral parts of object-oriented programming. A data field or method is either instance or static. Do not mistakenly overlook static data fields or methods. It is a common design error to define an instance method that should have been static. For example, the factorial(int n) method for computing the factorial of n should be defined static, because it is independent of any specific instance.
![Page 26: Object Oriented programming](https://reader036.vdocuments.net/reader036/viewer/2022062521/56816684550346895dda2c30/html5/thumbnails/26.jpg)
Thanks for Attention