0% found this document useful (0 votes)
0 views23 pages

3. Classes and Objects

The document provides an overview of Object Oriented Programming concepts in Java, focusing on classes, objects, methods, and access modifiers. It explains class declaration, object instantiation, method creation, parameter passing, and the use of the 'this' keyword. Additionally, it covers instance and static members, constructors, and constructor overloading, emphasizing their roles in defining and managing object behavior and properties.

Uploaded by

BIRUK GEBRE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views23 pages

3. Classes and Objects

The document provides an overview of Object Oriented Programming concepts in Java, focusing on classes, objects, methods, and access modifiers. It explains class declaration, object instantiation, method creation, parameter passing, and the use of the 'this' keyword. Additionally, it covers instance and static members, constructors, and constructor overloading, emphasizing their roles in defining and managing object behavior and properties.

Uploaded by

BIRUK GEBRE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

BITS College

Undergraduate Program
Course Code: SE132
Course Title: Object Oriented
Programming
Classes & Objects
Class Declaration & Object Instantiation

 A class is a blueprint from which an object is created (instantiated)


 A class is a means to create user defined data type
 A class represents the set of properties & methods that are common to all objects of the same type. e.g.
Car, Person, Student
 Class declaration has the following syntax
[access modifiers] class NameOfClass {
...
}
 Example
public class Rectangle{
...
}
Methods and Their Components
 A Java method is a collection of statements that are grouped together
to perform an operation.

– 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

 The arguments should be in the same order as their respective parameters in


the method specification.

 Parameters can be passed by value or by reference.


– Passing Parameters by Value means calling a method with a parameter. Through this,
the argument value is passed to the parameter.
Arbitrary Number of Arguments
 Variable length arguments can be passed to a method by using the varargs
construct.
– e.g. public void methodName(type... variable) {}
 The variable length parameter is treated as an array inside the method
 The method can be called either with an array or with a sequence of arguments
Method Overloading
 When a class has two or more methods by the same name but different
parameters, it is known as method overloading.

 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

 Calling an objects method


– Use an object reference, the dot(.) operator and the name of the method
 The garbage collector
– When is an object eligible for garbage collection
• If there is no reference to an object
• When the reference variable set to null explicitly
The this key word

 Within an instance method or a constructor, this is a reference to the current


object
 You can refer to any member of the current object from within an instance method or a
constructor by using this
E.g. public class Point {
public int x = 0;
public int y = 0;

//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

– The methods of an object define its behavior. These methods are


called instance methods. It is important to note that these methods pertain to
each object of the class.

– Instance variables and instance methods, which belong to objects, are


collectively called instance members.
Cont...
 Static Members
– Methods and variables defined inside the class are called an instance
methods and instance variables. That is, a separate copy of them is created
upon creation of each new object.

– 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) { …}

 When a member is declared static, it can be accessed before any


objects of its class are created, and without reference to any object.

 We can declare both method and variables to be static


Cont...
 The most common example of static member is Main(). main() is declared
as static because it must be called before any objects exist.
 variables declared as static are, essentially global variables.
 Restrictions to static
– They can only call other static methods
– They must only access static data
– They can’t refer to this or super in any way
Constructors
 Constructors are special methods that are called to create objects from
the class blueprint
 Constructors use the name of the class as their name and do not have
return type
 A constructor could be explicit or implicit
 A class can have multiple constructors including the default constructor
 E.g. for the rectangle class created above
public Rectangle() {…}
public Rectangle(int length) {…}
public Rectangle(int width) {...}
Constructor Overloading
public class Rectangle {

public int width = 0;


public int height = 0;
public Point origin;

// 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;
}

// a method for moving the rectangle


public void move(int x, int y) {
origin.x = x;
origin.y = y;
}
}

You might also like