Professional Documents
Culture Documents
Programming
What is Object-Oriented Programming?
x:0
Point
p y:0 Object
Objects
•An object possesses:
– Identity
a means of distinguishing
it
from other objects
– State
what the object
remembers
– Interface •Objects are:
what messages the – Building blocks for
object systems
responds to – Identifiable abstractions
– Behavior of
what the object can do real world objects
Instance variables or attributes
• define the state of an object in a class.
• can also be objects.
Methods
• functions that provide processing to the
object’s data.
• determine the behavior of an object.
• The collection of all publicly available
methods is called the class
interface.
Message and Object Communication
•Objects communicate via messages
•Interfaces define the set of valid messages that an
object can receive
•Messages, in Java, correspond to method calls
(invocations)
•Senders need a reference to the target object to send a
message
Accessing State
•State information can be accessed directly or by
using
messages
•Using messages:
–eliminates the dependence on implementation
–allows the developer to hide the details of the
underlying implementation
•“Accessor” messages are usually used instead of
accessing state directly
–example: getSpeed() may simply access a
state value called “speed” – or it could hide (or later
be replaced by) a calculation to obtain the same
value
Encapsulation
• Hides the implementation details of a class
• Forces the user to use an interface to access data
• Makes the code more maintainable
Accessing Object Members
• The dot notation: <object>.<member>
• to access the members of the class, use the dot
operator
p.setX(20);
p.display();
• the dot operator is used to access object members
including attributes and methods
• Examples:
obj1.setNum (47);
obj1.num = 47; //only allowed if x is public
Example of bad class Client code can access data
design: members directly:
class MyTime {
MyTime time1 = new MyTime();
public int seconds;
public int minutes; time1.seconds = 70; //no checking
public int hour;
time1.minutes = 70; // no checking
}
A better design:
Client code can access data
class MyTime { members directly:
private int seconds;
private int minutes; MyTime time1 = new MyTime();
private int hour; time1.setSeconds(70);
public void setSeconds(int sec) //error will be handled in the
{ // method
if ((sec >= 0) && (sec <=59))
seconds = sec;
else
// error handling code
}
Constructors
• A constructor is a method that is automatically
invoked when an object is created. It is normally
used to initialize data members.
• It has the same name as the name of the class. It
can receive parameters but it has no return value
• Example
public class MyPoint {
private int x;
private int y;
// constructor
public MyPoint (int a,int b) {
x = a; y = b;
}
Default Constructor
• If the programmer did not provide any constructor,
a default no-argument constructor is automatically
provided.
The default constructor takes no arguments
The default constructor has no body
• Enables the user to create object instances with new
Xxx() without having to write a constructor
• If the programmer writes a constructor, the default
constructor is not anymore provided.
Constructing and initializing objects
Calling new ...() causes the following events to
occur:
• memory is allocated for the new object and instance
variables are initialized to their default values
• explicit attribute initialization is performed
• the appropriate constructor is executed
Declaring
MyPoint p;
allocates space only for the reference. No object is
created unless the new operator is invoked.
package mypackage;
import mypackage;
is used to :
a. factor common attributes and methods in superclass
b. extend existing classes to support new abstractions
c. represent problem domain abstractions more accurately
Inheritance Example :
Generalization BankAccount
Employee Student
Teacher Doctor
Method Overriding
• The method name that is used in the
parent class can also be used in the child
class.
• If the method has different number and
type of parameters, it is still overloading, but
if the method has exactly the same signature,
it is called overriding.
• Overriding provides a way to redefine a
method in the parent class.
The Object Class
- the root of all classes in Java
- any class that is created automatically
“extends” the Object class.
- the Object class includes certain useful
methods which can be
overridden by any class. Some of the
useful methods are:
public boolean equals(Object obj);
- returns true if obj is equal to this object