Introduction to Objects Flashcards
In which memory does the object is allocated on JVM?
- In the memory managed by the JVM behind the scenes, object references and the objects assigned to them are managed by Stacks and heap.
- Heap is a large memory where all the objects are going to live, whereas Stack is a short term memory where we have reference variables and so on.
- Stacks are a list of references defined within a given block of code(also known as scope).
- Heap is where all objects are actually stored in memory.
- References in different stacks can point to the same objects in Heap.
What is Object-Oriented Programming?
- Object-oriented programming represents a way of creating code.
- The created code mimics real-world entities or “objects”.
- Objects are software bundles of data and related procedures.
- Objects contain state and/or behavior.
- Java refers to behavior as Methods, they are called function, procedures, or subroutines in other languages.
- For example, a bank account would contain data such as its balance, account number, and owner information.
- Refer to more details in the image below:
What are the advantages of OOP?
Objects vs Primitives?
Provide examples of creating objects in Java.
- The technical term for creating an object is called Instantiation.
How is an object created, explain in detail?
What happens when an object is Instantiated more than once?
How are methods in an object are invoked?
What are constructors and its uses?
What happens behind the scene when we use ‘new’ for invoking a constructor?
- “new” dynamically allocates space on the heap
- The object is created and its instance variables are initialized with default values. For string objects ‘null’ is the default value.
- Explicit initialization is executed with the values assigned during object instantiation.
- The final step is to call the constructor, thereby initializing the instance variables based on what the constructor indicates.
What are the default values for all the variables in Java?
Is it valid to use an instance variable inside a class to use before initialized?
What is a default constructor?
The default constructor is a no-argument constructor that is provided by Java if you define a class without explicitly defining any constructors.
Can we instantiate an object with the default constructor when it is not defined in the class?
- To fix the code below add a no-argument constructor inside the class Car. Car() { }
What is an initialization block?
It is a block of code that is run every time an object is instantiated.