Object Oriented Programming 2
Object Oriented Programming 2
in Java
Problems in Structured
Programming
Reference Variables.
Constructor Function
Constructor Overloading.
Multiple Objects of a Class.
Multiple objects can be created for a class.
Each object will be completely distinct
from other objects.
A separate copy of the data members is
created for each object.
An object cannot access the data
members of another object.
Class demo
Class Example {
{ Public static void main
int x; (String as[ ])
{
Void set(int a) Example ob1=new
{ Example();
Example ob2=new
x=a; Example();
} Ob1.set(10);
Ob2.set(30);
Void get() Ob1.get();
{ Ob2.get);
System.out.println(“ }
x=“ + x); }
Output:
} 10
} 30
Accessing object through
Multiple
Reference variables are the variable of
class data type.
A reference variable of a class can hold
the reference to the object of that class.
We can access an object through
multiple reference variables.
Class demo Ob=ob1; // assign the
{ reference of object 1
Public static void Ob.get() // ob will access
main(String as[]) the members of ob1
{ Ob=ob2; // assign the
Example ob1=new reference of object 2
Example(); Ob.get(); // assign the
Example ob2=new reference of object 2
Example();
}}
Example ob;
Output:
Ob1.set(10);
10
Ob2.set(30);
Ob1.get(); 30
Ob2.get); 10
30
Constructor Function