2. Difference between Composition and Aggrigation?


2. Difference between Composition and Aggrigation?

Composition:

         Without existing container object if there is no chance of existing contained object, then container and contained objects are strongly associated and this strong association is called Composition.

Example:

         An University consists of several departments without existing University there is no chance of existing department. Hence University and Department objects are strongly association and this strong association is nothing but Composition.

Aggrigation:

        Without existing container object if there is chance of existing contained object, then container and contained objects are weakly associated and this weakly association is called Aggrigation.

Example:

         Within the department there may be a chance of several professors. Without existing department object there may be a chance of existing professor object. Department and professor are weakly associated, and this weak association is nothing but aggregation.

1. What is Singleton class?


1. What is Singleton class?

         The singleton is one of the core design patterns.

        For any java class if we are allowed to create only one object such type of class is called Singleton class.

Example:

 public class MySingletonClass

{

private static MySingletonClass ins;

private MySingletonClass() {}

public static MySingletonClass

getInstance(){

if(ins==null){

ins = new MySingletonClass();

}

return ins;

}

…//our custom logic

}//class

 Example:

Runtime r1 = Runtime.getRuntime();

Runtime r2 = Runtime.getRuntime();

:

:

:

Runtime rn = Runtime.getRuntime();

      Instead of creating a simple object for every requirement we can create only one object and we can reuse that object for every similar requirement.

      So that performance will be improved and memory utilization will be improved, we can achieved this by using Singleton class.

1. Explain about toString() metho of Object class?


     public String toString()

    Returns a string representation of the object

      We can use toString() method to get String representation of an Object.

    Whenever we are trying to print any object reference then internally toString() method will be executed.

   In java.lang package Object class contains toString() method as non-final method.

  The toString() method of Object class returns ClassName@hashCodeInHexadecimalNotation.

   As Object class is super class for every method so it is inherited into every java class.

 The toString method is non-final method every class can override this method.

   Whenever we are displaying a ref on the monitor using println() or print() methods and if the reference is not pointing null then these methods internally call toString() on the ref and prints the result of toString().

   If reference is pointing null then NullPointerException is not generated only null is printed.

  If our class is overriding toString() then our class toString() return value is printed.

   If  our class is not overriding toString() then Object class toString() return value is printed i.e.,    ClassName@hashCodeInHexadecimalNotation.

Example: without toString() method

//StudentDemo.java

public class StudentDemo{

String name;

Int rollno;

Student(String name,  Int rollno){

this.name = name;

this.rollno = rollno;

}

public static void main(String[] args){

Student s1 = new Student(“durga”,101);

Student s2= new Student(“nag”,102);

System.out.println(s1);

System.out.println(s2);

//System.out.println(s1.toString());

}//main

}//class

javac StudentDemo.java

java StudentDemo

OUTPUT:

StudentDemo@3e25a5

StudentDemo@19821f

Example: with toString() method

//TestDemo.java

public class TestDemo{

public String toString(){

return “TestDemo object”

}

public static void main(String[] args){

String s = new String(“durga”);

Integer I = new Integer(10);

TestDemo t = new TestDemo();

System.out.println(s);

System.out.println(I);

System.out.println(t);

}//main

}//class

javac TestDemo.java

java TestDemo

OUTPUT:

Durga

10

TestDemo object

The ways to make an Object eligible for Garbage Collection (GC)?


          Even though programmer is not responsible for destruction of useless Objects.

           It is highly recommended to make an Object eligible for GC if it is no longer required.

            If an Object doesn’t contain any references then that Object eligible for GC.

             The following are various possible case to make an Object eligible for GC.

 1.Nullifying the reference variable

2.Reassigning the reference variable

3.Objects created inside a method

4.Island of Isolation

 

In How many ways we can create and get an Object in java?


  1. 1.By using new operator

         Test t = new Test();

    2.By using newInstance() method

         Object o = Class.forName(“Test”).newInstance();

    3.By using De-Serialization

   FileOutputStream fos = new FileOutputStream(“abc.txt”);

   ObjectOutputStream ois = new ObjectOutputStream(fos);

  Object o = oos.writeObject();

 NOTE: The factory method, clone method and any other listed would be taken as derived ways of creating the java object.