Professional Documents
Culture Documents
1
Objects & Classes
2
Objects
*Object-Oriented programming or OOP revolves around the concept of
objects as the basic elements of programs.
*These objects are characterized by their properties (or attributes) and
behaviors.
*The objects in the physical world can easily be modelled as software
objects using the properties as data and the behaviors as methods.
*Each object is composed of a set of data (properties/attributes) which are
variables describing the essential characteristics of the object, and a set of
methods (behaviors) that describes how an object behaves-performing
some task.
*Thus, an object is a software bundle of variables and related methods. The
variables and methods in a Java object are formally known as instance
variables and instance methods to distinguish them from class variables
3
and class methods.
*Objects are instances of a class.
*An object is a building block which contains variables and methods.
*An object is a software bundle of related state and behavior.
Software objects are used to model the real-world objects in
everyday life.
*Objects interact with each other by passing messages.
*Methods and variables that constitute a class are called members of
the class.
*The data members or variables, defined with in a class are called
instance variables.
4
*A Class is a template, a prototype or a blueprint of an object.
*A class is a programmer defined type that serves as a blueprint for
instances of the class.
*It consists of two types of members which are called fields
(properties or attributes) and methods. Fields specify the data
types defined by the class, while methods specify the operations.
*Classes provide the benefit of reusability. Software programmers
can use a class over and over again to create many objects.
*A class is a template/ blueprint/prototype that defines the form of
an object.
*A class is a collection of data and methods that operate on that
data.
*A Class is a blueprint that defines the states and the behaviors
common to all objects of a certain5 kind.
*A class is created by using the keyword class.
General Form:
8
public class Addition {
int x,y;
public double add() {
return x+y;
}
public class AdditionTest {
public static void main(String args[]) {
Addition ad = new Addtion();
// Assign values to ad’s instance variables
ad.x=5;
ad.y=7;
sum= ad.add();
System.out.println(“The Sum of ”+ad.x+” and ”+ad.y+” is: ”+sum);
}
}
* Object reference variables act differently than you expect when an assignment
take place. For example:
Course c1 = new Course( );
Course c2 = c1;
* c1 and c2 will both refer to the same object. The assignment of c1 and c2 did not
allocate any memory or copy any part of the original object . It simply makes c2
refer to the same object as does c1.
* When an instance is assigned to a variable, that variable is said to hold a
reference or point to that object
Person g = new Person(“Abebe", 26);
Person h = new Person("Abebe", 26);
* g and h hold references to two different objects that happen to have identical state
* The object creation statement has three parts.
Course c = new Course();
10
Declaration Instantiation Initialization
* Declare variables of int type as: -
int a; //a is a variable which refers to any type of int data.
* Declaration: Associates a variable name with an object type.
* Classes in java are also types so declare class type variable as: -
Course c; //c is a variable which refers to any type of Course.[Used to refer
Course object]
* Declaring a object variable (Course c; )do not create any object until assigned
with a new keyword.
2. Instantiation
* The new operator instantiates a class by allocating a memory for a new object.
The new operator returns a reference to the object it created and this reference
is assigned to the appropriate variable.
Course c = new Course(); //Create object called c & reserve/allocate memory to that object
* Instantiating a class means creating an object/Instance of a class.
11
* The object is always initialized by calling a constructor.
* A constructor is a special method which has a same name as class and used to
initialize the variables of that object.
* Course class object is initialized by calling the constructor of Course class:
Course( );
20
* The type, name and arguments together is referred to as the signature of the method
* Method definition has four parts . They are , name of the method, type of object or
primitive type the method returns, a list of parameters and body of the method.
General form:
21
*A method has 4 parts: the return type, the name, the arguments,
and the body:
type name arguments
void Methods
A method of type void has a return statement without any specified value. i.e. return;
Any method declared void doesn't return a value.
It’s used to branch out of a control flow block and exit the method. return;
26
public void dispalyMsg() { System.out.println(“Hello Computer Science”); }
*The return statement is used in a method to output the result of
the methods computation.
*It has the form: return expression_value;
*The type of the expression_value must be the same as the
type of the method:
29
* Variables are areas allocated by the computer memory to hold data.
Class variable Instance variable
Class variable is declared by using Instance variable is declared without
static keyword. static keyword.
static int a = 4; int a = 4;
All objects share the single copy of All objects have their own copy of
static variable. instance variable.
Belong to the whole class-static
member 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.
Static variable can be accessed without Instance variable cannot be accessed
creating an object, by using class name. without creating an object.
ClassName.variable ObjectName.variable
30
s
Static Method Instance Method
Static methods are declared by using instance methods are declared without
static keyword. static keyword.
static type method( ); type method( );
All objects share the single copy of All objects have their own copy of
static method. instance method.
Static method does not depend on the Instance method depends on the object for
single object because it belongs to a class. which it is available.
Static methods cannot call non-static Non-static methods can call static
methods. methods.
Static methods cannot access not- Non-static methods can access static
static variables. variables.
31
Static methods cannot refer to this or Instance methods can refer to this and
super. super.
When to declare variable as static? When value of the variable remains the same for all class
instance created & being used in every instance. They’ll be loaded when a class loads.
Static tells compiler there’s exactly one copy of this variable in existence, no matter how
many times class has been instantiated.
Static method or variable is not attached with specific object, but to class as a whole. They are
allocated when the class is loaded.
Static methods are declared when the behavior of method doesn’t change for each object created
[i.e. behaviors of method remain the same for all instances created.]
// Demonstrate static variables, methods and blocks.
class UseStatic {
static int a = 3;
static int b;
Output
static void meth ( int x ) {
Static block Initialized
System.out.println ( “ x = “ + x );
x = 42
System.out.println ( “ a = “ + a );
a=3
System.out.println ( “ b = “ + b );
b = 12
System.out.println ( “ Static block initialized . “ );
b=a*4;
}
public static void main ( String args [ ] ) { meth ( 42 ); }
} 32