top of page
Search
Writer's pictureAbhinaw Tripathi

Relationship Between Objects Java


Relationship Between Objects

It is possible to create objects for different classes and establish relationship between them.When the objects are related,it is possible to access and use members of one object in another object.When an object should start processing data where where another object has left.It is helpful to pass data from one object to another object in chained form.

There are three ways to relate objects in Java:

  1. Using References

  2. Using inner class concept

  3. Using inheritance

Relating Objects using References

Let us create two classes,One and Two . Suppose we want to access the members of class two in class one,we should relate their objects.we should relate their objects .for this just declare the reference variable of class two as an instance variable in class One.

Program:

/**

*

*/

package com.myobjectrelation;

/**

* @author Abhinaw.Tripathi

*

*/

class One

{

int x;

Two t;

public One(Two t)

{

this.t=t;

x=10;

}

public void display()

{

System.out.println("One x= " + x);

t.display();

System.out.println("Two var="+t.y);

}

}

class Two

{

int y;

public Two(int y)

{

this.y=y;

}

void display()

{

System.out.println("Two y= " +y);

}

}

public class ObjectRelationUsingref

{

public static void main(String[] args)

{

Two obj2=new Two(22);

One obj1=new One(obj2);

obj1.display();

}

}

Result:

One x= 10

Two y= 22

Two var=22

Another Sample Program:

/**

*

*/

package com.myobjectrelation;

/**

* @author Abhinaw.Tripathi

*

*/

class OneA

{

TwoB obj2;

public OneA(TwoB obj2)

{

this.obj2=obj2;

}

double cube(double x)

{

double result=x*obj2.square(x);

return result;

}

}

class TwoB

{

Three obj3;

public TwoB(Three obj3)

{

this.obj3=obj3;

}

double square(double x)

{

double result=x*obj3.get(x);

return result;

}

}

class Three

{

public double get(double x)

{

return x;

}

}

public class Relate

{

public static void main(String[] args)

{

Three obj3=new Three();

TwoB obj2=new TwoB(obj3);

OneA obj1=new OneA(obj2);

double result=obj1.cube(5);

System.out.println("cube of 5 ="+result);

double result2=obj2.square(5);

System.out.println("square of 5 ="+result2);

}

}

Result:

cube of 5 =125.0

square of 5 =25.0

What is Object Graph?

Ans: Object graph is a graph showing relationship between different objects in memory.

Inner Class

Inner class is a class written within another class.Inner class is a basically a safety mechanism,since it is hidden from other classes in its outer class.

To make instance variables not available outside the class,we use private access specifier before the variables.this is how we provide the security mechanism to variables.Similarly,in some cases,we want to provide security for the entire class.in this case we use private access specifier before class.?.the problem is if we use private access specifier before a class ,the class is not available to the java compiler or JVM .So it is illegal to use private before a class name in java.

But private is allowed before an inner class and thus it is useful to provide security for the entire inner class because it is not available to other classes.This means an object to inner class can not be created in any other class.

Program:

/**

*

*/

package com.myobjectrelation;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* @author Abhinaw.Tripathi

*

*/

class BankAcct

{

private double bal;

public BankAcct(double b)

{

this.bal=b;

}

void contact(double r) throws IOException

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter Password: ");

String password=br.readLine();

if(password.equals("abhinaw"))

{

Interest in=new Interest(r);

in.calculateInterest();

}

else

{

System.out.println("Sorry,you are not an authorized person");

return;

}

}

private class Interest

{

private double rate;

public Interest(double r)

{

this.rate=r;

}

void calculateInterest()

{

double interest=bal*rate/100;

bal+=interest;

System.out.println("updated Balance="+bal);

}

}

}

public class BankAccount

{

public static void main(String[] args) throws IOException

{

BankAcct account=new BankAcct(100000);

account.contact(9.5);

}

}

Result:

Enter Password:

abhinaw

updated Balance=109500.0

Note:

The inner class object will contain an additional field by the name this$0 which stores the reference number of the outer class object in memory.this is this$o is invisible field because reference of outer class object is available to inner class object,now inner class is able to refer to all the members of the outer class.

  1. An inner class is a safety mechanism .

  2. Inner class is hidden from other classes in its outer classes.

  3. An object to inner class can not be created into other classes.

  4. An object to inner class can be created only in its outer class.

  5. Inner class members can access the members of the outer class directly.

  6. Inner Class and Outer Class objects are created in separate memory locations.

  7. When same names are used, we can refer to outer class members in the inner class.

  8. Inner class decrease readability of a program.This is against the design principal of Java to be a simple programming language.

Anonymous Inner Class

It is an inner class without a name and for which only a single object is created.

Anonymous inner classes are very useful in writing implementation classes for listener interfaces in graphics programming or Android interfaces.Lets understand it by a simple program.

Program:

/**

*

*/

package com.myobjectrelation;

import java.awt.Button;

import java.awt.Frame;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

/**

* @author Abhinaw.Tripathi

*

*/

class MyClass implements ActionListener

{

@Override

public void actionPerformed(ActionEvent e)

{

System.exit(0);

}

}

class But extends Frame

{

public But()

{

Button btn=new Button();

add(btn);

btn.addActionListener(new MyClass());

}

}

public class AnonymousListProg

{

public static void main(String[] args)

{

But obj=new But();

obj.setSize(400,300);

obj.setVisible(true);

}

}

Result: A window will pop up and when you will click on it ,it will be gone.

  • MyClass is inner class inside But class.

  • The name MyClass is not written in its outer class i.e. But class.

  • MyClass object is created only once.

  • MyClass code is directly copied into the method parameter.

What is Anonymous Class?

Ans: It is an inner class whose name is not written in the outer class and for which only one object is created.

Another Example:

/**

*

*/

package com.myobjectrelation;

import java.awt.Button;

import java.awt.Frame;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

/**

* @author Abhinaw.Tripathi

*

*/

public class AnotherAnonyExample extends Frame

{

public AnotherAnonyExample()

{

Button b=new Button();

add(b);

b.addActionListener(new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

System.exit(0);

}

});

}

public static void main(String[] args)

{

But obj=new But();

obj.setSize(400, 300);

obj.setVisible(true);

}

}

Result: A window will pop up and when you will click on it ,it will be gone.

Note: Finally,anonymous inner class is helpful when we want to pass entire class code to a method,where the class name is not written but an object is created to it.

Another way of relating objects is inheritance which we have already discussed.

4 views0 comments

Recent Posts

See All
bottom of page