Skip to content

Instantly share code, notes, and snippets.

@viveknaskar
Last active September 12, 2020 06:21
Show Gist options
  • Save viveknaskar/9b4ef0dc14a8e652e5cef16c1659fb5e to your computer and use it in GitHub Desktop.
Save viveknaskar/9b4ef0dc14a8e652e5cef16c1659fb5e to your computer and use it in GitHub Desktop.
Added bunch of questions with their answers

How many ways you can create an object in java?

  1. Using new keyword → constructor get called Employee emp1 = new Employee();

  2. 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();

  3. Using newInstance() method of Constructor → constructor get called Constructor constructor = Employee.class.getConstructor(); Employee emp3 = constructor.newInstance();

  4. Using clone() method → no constructor call Employee emp4 = (Employee) emp3.clone();

  5. Using deserialization → no constructor call ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj")); Employee emp5 = (Employee) in.readObject();

What is your role in your current project?

How many types of variables are there in java?

Three types - local, instance and static

Stack vs Heap

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.

What are Abstract class?

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment