3. Classes and Objects
3. Classes and Objects
Undergraduate Program
Course Code: SE132
Course Title: Object Oriented
Programming
Classes & Objects
Class Declaration & Object Instantiation
– When you call the System.out.println() method, for example, the system
actually executes several statements in order to display a message on the
console.
Creating a method
The syntax for creating a method is
[modifier] returnType nameOfMethod ([Parameter List])
{ // method body }
– modifier − It defines the access type of the method and it is optional to use.
– returnType − Method may return a value.
– nameOfMethod − This is the method name. The method signature consists of the
method name and the parameter list.
– Parameter List − The list of parameters, it is the type, order, and number of
parameters of a method. These are optional, method may contain zero parameters.
– method body − The method body defines what the method does with the
statements.
Example
Rectangle
– Properties
• originXCoordinate
• originYCoordinate
• length
• width
– Methods
• calculateArea
• calculatePerimeter
• moveOrigin
Passing parameter
Java passes arguments by value
It is different from overriding. In overriding, a method has the same method name, type,
number of parameters, etc.
Creating Objects
In java, you create an object by creating an instance of a class or, in other words,
instantiating a class
The syntax for creating object is
– Class name reference_variable = new Class Name();
– e.g. Rectangle r = new Rectangle();
The object creation code above does three actions at a time. Declaration, instantiation
and initialization
Cont…
Declaring an object
– Declaring a variable to hold an object is just like declaring a variable to hold
a value of primitive type.
type name
Rectangle r;
Instantiating an object
– The new operator instantiate a class by allocating memory for a new object
of that type
new Rectangle();
Initializing an object
Rectangle r = new Rectangle();
– An object can have multiple references
Cont…
Referencing an object fields
– Inside a class fields are accessed using their name
– Outside of a class fields are accessed by using the object reference, the
dot(.) operator and the name of the field
//constructor
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
Cont...
The this key word is also used to call constructors within the same class
E.g. public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 1, 1);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
Access Modifiers
As the name suggests access modifiers in Java helps to restrict the scope of a class, constructor,
variable, method, or data member.
There are four access modifiers
• Default – if no explicit access modifier assigned to a class, method or data
member, the default access modifier is used
– A class, method or property with default access modifier is accessible within the
package it is defined
• Private – it is used to enforce data hiding principle of the object oriented
paradigm
– A property or method declared with private visibility is not accessible outside of a
class
– It is not applicable to class
– It requires setters and getters to set or read its values
Cont…
• Protected - the methods or data members declared as
protected are accessible within the same package or sub-
classes in different packages.
– It is not applied to class
• Public - the public access modifier has the widest scope among all other
access modifiers
– Classes, methods, or data members that are declared as public are
accessible from everywhere in the program. There is no restriction on the
scope of public data members.
Instance and Static Members
Instance Members
– Objects created from a class will have its own copy of the properties
defined in the class. The properties of an object are called instance
variables
– But in some cases it is necessary to define a member that is common to all the
objects and accessed without using a particular object.
– That is, the member belongs to the class as a whole rather than the objects
created form the class. Such members can be created by preceding them
with the keyword static.
Cont...
Example
– static int cal;
– static float min=1;
– static void display (int x) { …}
// four constructors
public Rectangle() {
origin = new Point(0, 0);
}
public Rectangle(Point p) {
origin = p;
}
public Rectangle(int w, int h) {
origin = new Point(0, 0);
width = w;
height = h;
}
Cont...
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}