-
Using new keyword → constructor get called Employee emp1 = new Employee();
-
Using newInstance() method of Class → constructor get called Employee emp2 = (Employee) Class.forName("org.programming.mitra.exercises.Employee").newInstance(); It can also be written as Employee emp2 = Employee.class.newInstance();
-
Using newInstance() method of Constructor → constructor get called Constructor constructor = Employee.class.getConstructor(); Employee emp3 = constructor.newInstance();
-
Using clone() method → no constructor call Employee emp4 = (Employee) emp3.clone();
-
Using deserialization → no constructor call ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj")); Employee emp5 = (Employee) in.readObject();
Three types - local, instance and static
Each and every object is created dynamically during runtime in the heap space. Stack contains the ocal primitive variable as well as the reference to objects in the heap memory.
Abstract class is a class whcih has abstract methods with no definition. We cannot create object in a abstract class it is automatically created during runtime. When a class is extending the abstract class, it needs to redefine the abstract methods also. This is also called Object to object inheritance. It can have instance variables.