# Object Oriented with Java/Object concepts

This unit will cover:
* How an object’s response to a messaged based on the state of its attributes.
* Different types of messages:
   * Messages with/without arguments
   * Message that returns a value
   * Messages that change state of receiver
   * Messages that does not change state of receiver
* Polymorphism and object collaboration
* Inheritance, classes and sub-classes

## Polymorphism

Different classes may share exactly the same protocol which is known as polymorphism, so those messages may overlap which is useful. So a message to which objects of more than one class can respond is called a polymorphic message.
A polymorphic message:
* Is found in more than one class with the same name.
* It may differ in the behaviour or arguments, or no change at all.

## Class Fields

Any class is composed of three parts: class name, class attributes and clas methods (protocol)
Assume the below class diagram of a rectangle.
* Class Rectangle
* double lengthm width
* setLength(double), setWidth(double), getLength, getWidth, and area.
Figure1. Class diagram of a Rectangle
You can figure out that:
Class name: Rectangle
Class attributes: length, width
Class methods: setLength(double), setWidth(double), getLength, getWidth, and area.

### Class Methods

Methods could be classified into:* Method with/without arguments;
* Method change the state of an object;
* Method that does not change object state;
* Method returns an answer, it does not change the state of an object, it obtains information about object state. This answer could be a value, or an object, and is used as an argument on subsequent message, or to collaborate with other objects.
In Object Oriented Programming Language it is common that when you define an attribute in a class, then you need to define at least two methods for each attribute: setter method and getter method.
* Setter method: a method that sets the value of one of a receiver's attribute. A setter method name usually starts with set followed by attribute name then parenthesis as in setLength. Any message that changes the state of an object is known as a mutator method.
* Getter method: a message that returns as its message answer, the value of one of a receiver's attributes. A getter message name usually starts with get followed by attribute name then parenthesis as in getx. A getter message always returns an answer. The getter message is known as accessor method since it is only access the attribute value without doing any change.

## Classes and sub-classes

You may use more than one class in your program. The most popular types of relationship between classes are:
* "is-a" relationship, which represents the inheritance principle.
* "has-a" relationship that represents composition principle.
Inheritance: In this case you have a super class (parent class), and a sub-class (child class). The super class will have common attributes and protocols of parent and child. The child class will inherit all attributes and protocols from his parent; in addition the sub-class will have its own special attributes or messages which are not found in the parent class.
Example:
Assume you have two classes: class A, and class B as shown in table 1:
| Class Name | attributes | protocol |
| Class A | int x, y | setx,getx, sety, gety; |
| Class B | int x, y, z | setx,getx, sety, gety, setz, and getz; |
Table1. Class A and B with its attributes and protocols
An attribute declaration is similar to variable declaration, so when you declare it, you need to determine data type then variable name as follows:
Data types: could be any primitive data type or structural data type.
Primitive data types: are fond in all programming languages and may differ in syntax such as:
int (number without digits) // denoted as int in Java float (any number) // denoted as float in Java char (one character and denoted by single quote such as: 'a', '+') boolean (either true or false)
Structural data type: such as records, and classes.
So in table 1, class B includes all attributes and messages of class A. In addition class B has extra messages: setz, and getz, also an extra attribute: z.
Now to avoid repetition of declaring these common attributes, and messages twice, we put all common attributes and messages in an upper class called “Super-class”, and do another class that inherits these (sub-class), and has its own extra attributes and messages. Such a relationship between classes is known as inheritance.