Chapter-7
Chapter-7
Object:-
An object in Java is the physical as well as a logical entity, An entity that has state and
behavior is known as an object e.g., chair, bike, marker, pen, table, car, etc. It can be
physical or logical .
An object has three characteristics:
For Example, Pen is an object. Its name is Reynolds; color is white, known as its state. It
is used to write, so writing is its behavior.
Class:-
A class is a group of objects which have common properties. It is a template or blueprint
from which objects are created. It is a logical entity. It can't be physical.
A class in Java can contain:
Fields
Methods
Constructors
Blocks
class <class_name>
{
field;
method;
}
class Student
{
int id;
String name;
}
class TestStudent3
{
public static void main(String args[])
{
//Creating objects
Student s1=new Student();
Student s2=new Student();
//Initializing objects
s1.id=101;
s1.name="Sonoo";
s2.id=102;
s2.name="Amit";
//Printing data
System.out.println(s1.id+" "+s1.name);
System.out.println(s2.id+" "+s2.name);
}
}
SeeSee
below figure
below is represented
figure graphically
is represented for how
graphically fortohow
memory occupyoccupy
to memory by obj1by
and obj2.
obj1 and obj2.
int a; int a;
Obj1 Obj2
int b; int b;
int c; int c:
Advantage of Method
Code Reusability
Code Optimization
The method visibility modifier goes first. Here in above example it is public.
Then method's return type goes second, which is void type in the code above.
After the method type, you need a space followed by the name of your method.
We've called the one above sum.
In between a pair of round brackets we have told Java that is parameter List. Here we have
used two variable which names are a and b respectively.
To separate this method from any other code, you need a pair of curly brackets. Your code
for the method goes between the curly brackets. Then here we have used those variables
which are passed in the parameter. And remember it here we have not return any value so
we do not wrought return statements.
Let’s now we will discuss about all four step see in above figure.
Step 1: create an Object. Here class name is DemoMethod and d1 is an Object name.
Step 2: this step is indication of how to call method using an Object. Here
sum is method name and values 10 and 20 are as parameters.
Example:
class Demo
{
int sum(int x, int y)
{
return(x+y);
}
int sub(intx, int y)
{
return(x-y);
}
int mul(int x, int y)
{
return(x*y);
}
double div(double x, double y)
{
return(x/y);
}
Class Main
{
public static void main(String [] args)
{
Demo d1=new Demo();
int ans1,ans2,ans3,ans4;
ans1=d1.sum(50,30);
ans2=d1.sub(50, 30);
ans3=d1.mul(50, 30);
}
}
Output:
Addition is=80
Subtraction is=20
Multiplication is=1500
Division is=2.0
}
}
Recursion in Java
Recursion in java is a process in which a method calls itself continuously. A method in
java that calls itself is called recursive method.
It makes the code compact but complex to understand.
Syntax:
returntype methodname()
{
//code to be executed
methodname();//calling same method
}
class RecursionExample1
{
void p()
{
System.out.println("hello");
p();
}
hello
hello
...
java.lang.StackOverflowError
class RecursionExample2
{
int count=0;
void p()
{
count++;
if(count<=5)
{
System.out.println("hello "+count);
Output:
hello 1
hello 2
hello 3
hello 4
hello 5
sum(10)
sum(9)
sum(8)
sum(7)
sum(6)
sum(5)
sum(4)
sum(3)
sum(2)
sum(1)
Sum(0)
return 0
return 0+1=1
return 1+2=3
return 3+3=6
return 6+4=10
return 10+5=15
return 15+6=21
return 21+7=28
return 28+8=36
return 36+9=45
return 45+10=55
}
public static void main(String[] args)
{
RecursionExample4 re=new RecursionExample4();
int fac;
fac=re.factorial(5);
System.out.println("Factorial of 5 is:"+fac);
}
}
Explaination
factorial(5)
factorial(4)
factorial(3)
factorial(2)
factorial(1)
return 1
return 2*1 = 2
return 3*2 = 6
return 4*6 = 24
return 5*24 = 120