OOP Chapter 3
OOP Chapter 3
OOP Chapter 3
[CoEg2182]
Chapter Three:
Objects and Classes
Object
• Object-oriented programming (OOP) involves programming using
objects.
• An object represents an entity in the real world that can be
distinctly identified. For example, a student, a desk, a circle, a
button, and even a loan can all be viewed as objects.
• An object has a unique identity, state, and behaviors.
• The state of an object consists of a set of data fields (also known as
properties) with their current values.
• The behavior of an object is defined by a set of methods.
Objects
data field 1 radius = 5 Data field, State
Properties
... State
(Properties)
... Behavior
method n
An object has both a state and behavior. The state defines the
object, and the behavior defines what the object does.
3
Classes
• Classes are constructs that define objects of the same
type.
• A Java class uses variables to define data fields and
methods to define behaviors.
• Additionally, a class provides a special type of methods,
known as constructors, which are invoked to construct
objects from the class.
Classes
class Circle {
/** The radius of this circle */
double radius = 1.0; Data field
Circle() {
}
Circle(double newRadius) {
radius = newRadius;
}
Constructors, cont.
• A constructor with no parameters is referred to as a no-
arg constructor.
• Constructors must have the same name as the class itself.
• Constructors do not have a return type—not even void.
• Constructors are invoked using the new operator when an
object is created. Constructors play the role of initializing
objects.
Creating Objects Using Constructors
new ClassName();
• Example:
new Circle();
new Circle(5.0);
Default Constructor
• A class may be declared without constructors. In this case,
a no-arg constructor with an empty body is implicitly
declared in the class. This constructor, called a default
constructor, is provided automatically only if no
constructors are explicitly declared in the class.
Declaring Object Reference Variables
• To reference an object, assign the object to a reference variable.
• To declare a reference variable, use the syntax:
ClassName objectRefVar;
• Example:
Circle myCircle;
Declaring/Creating Objects in a Single Step
ClassName objectRefVar = new ClassName();
Create an object
• Example:
Assign object reference
• The system allocates memory for class variables the first time
it encounters the class at class load time.
ClassName.variableName;
Car.gears;
Class vs. Instance Variables
Class variable Instance variable
Class variable is declared by using Instance variable is declared
static keyword. without static keyword.
int a = 4;
static int a = 4;
All objects share the single copy of All objects have their own copy of
static variable. instance variable.
Static variable does not depend on Instance variable depends on the
the single object because it belongs object for which it is available.
to a class.
ClassName.variable
ObjectName.variable
Class Methods(static methods)
• The class (static) methods is not referenced through a
particular instance of a class but through the class itself.
All objects share the single copy of All objects have their own copy of
static method. instance method.
Static method does not depend on Instance method depends on the
the single object because it belongs object for which it is available.
to a class.
ClassName.method( ); ObjectName.method( );
Static methods cannot call non- Non-static methods can call static
static methods. methods.
Static methods cannot refer to this Instance methods can refer to this
or super. and super.
Get and Set methods
• We were able to create objects of the class Account and initialize the variables
of the object using the constructor. But after that, we can no longer change the
values the variables hold. The following statement would result in an error
since the variables were declared to be private(private String name;).
myAccount.name=“Abebe”
• The solution lies in providing public methods through which new values can be
assigned to these variables and the values they hold can be accessed. These
are commonly known as get and set methods.
• Get methods provide access to the value a variable holds while set methods
assign values to the variables of the objects.
Set methods
• In order to set a variable to a new value, the method needs arguments similar
to how the parameterized constructors required details such as name,…. These
required arguments are specified in the method header within the parentheses
by specifying the variable name and a data type for that variable name.
• Example:- A set method for name looks as shown below:
public void setName ( String n ) {
name = n;
}
• It is a convention to name the setter methods starting with the word 'set'
followed by the variable name that is going to be set.
• If we wish to set the name for the first Account object a1 which we have
created, we use the following statement:
a1.setName(“Abebe");
Get methods
• Now, we shall provide a get method for the variable name. As we have earlier
said, methods are capable of returning values. These values can be of any data
type. Here, we will be returning a String. The data type that is going to be
returned in specified in the method header instead of the word void. We have
used the word void till now to indicate that a method does not return any value.
• Example: The get method for name would be the following:
public String getName() {
return name;
}
• In the following statements, the value returned by getName is stored in the
variable aName and displayed.
String aName = a2.getName();
System.out.println ("Name of a1: "+ a2.getName() );
Access Modifiers
• Access modifiers define various levels of access between
class members and the outside world (other objects).
• They allow us to define the encapsulation characteristics of
an object.
• There are four access modifiers in java:
1. Private
2. Protected
3. Public
4. default.
private
• The private access modifier is the most restrictive; it specifies that
class members are accessible only by the class in which they are
defined. This means that no other class has access to private class
members, even subclasses.
private int a = 4;
Package 1 Package 2
Package 1 Package 2
Package 1 Package 2
Package 1 Package 2
}
• To put Rectangle the class inside graphics package, the
following code appears inside the Rectangle.java file.
}
• if you want Circle class to available outside the package.
You must use the class fully qualified name as follows:
import graphics.*;
-End of Chapter Three-