Figure 2.1: Two Steps of Creating an Object in Java (Student s1 = new Student();)
Figure 2.1: Two Steps of Creating an Object in Java (Student s1 = new Student();)
1. Declaration
Student s1; (Creates reference on Stack)→
2. Instantiation (new)Allocates memory for object on Heap
→
3. InitializationCalls Constructor
Student() to set fieldsExplanation
- Declaring a reference gives the program a name for an object reference.
- The new expression creates and initialises the object, then the assignment stores its reference.
Figure 2.2: Three Types of Constructors in Java
Figure 2.2: Three Types of Constructors in Java
1. Default (No-Arg) Constructor
Takes no arguments; initializes fields with default or fixed starting values
2. Parameterized Constructor
Accepts arguments to initialize each object with custom user-supplied values
3. Copy Constructor
Accepts another object of the SAME class as a parameter and copies its fields
Explanation
- A no-argument constructor can choose initial values and a parameterised one accepts them.
- A copy-style constructor is written by the programmer, while a compiler-supplied default constructor appears only if no constructor is declared.
Figure 2.3: How Stack and Heap Memory Work Together in Java
Figure 2.3: How Stack and Heap Memory Work Together in Java
Stack Memory (Fast, LIFO)
- Stores method call frames & local primitive variables (e.g.,
int x = 10;) - Stores object reference variables (e.g.,
s1holding address0x4A2) - Automatically freed as soon as the method finishes execution
Heap Memory (Large, Dynamic)
- Stores ALL Objects created with
new(e.g.,new Student()at0x4A2) - Stores instance variables and Arrays
- Cleaned up automatically by the Garbage Collector (GC)
Explanation
- Method calls create frames containing local values and references.
- A reference can lead to an object whose lifetime extends beyond that call.
Figure 2.4: Converting Between Primitives and Wrapper Objects
Figure 2.4: Converting Between Primitives and Wrapper Objects
Primitive Value
int a = 25;→ Autoboxing →
Wrapper Object
Integer obj = a;→ Unboxing →
Primitive Value
int b = obj;Explanation
- Boxing wraps a primitive value in an object of its wrapper type.
- Unboxing obtains the primitive again and requires a non-null wrapper reference.
Figure 2.5: Types of Inheritance Supported by Classes in Java
Figure 2.5: Types of Inheritance Supported by Classes in Java
1. Single Inheritance
Class B extends Class A (One Parent → One Child)
2. Multilevel Inheritance
Class C extends B, and Class B extends A (Grandparent → Parent → Child)
3. Hierarchical Inheritance
Classes B, C, and D all extend the same Parent Class A
Explanation
- A class can extend one direct superclass and a chain can span several levels.
- Several subclasses can share the same parent, forming a hierarchy.
Figure 2.6: Multiple Inheritance in Java Using Interfaces
Figure 2.6: Multiple Inheritance in Java Using Interfaces
interface Printable (
print()) + interface Showable (show())
class Report implements Printable, Showable
Safely inherits from BOTH interfaces without the Diamond Problem!
Safely inherits from BOTH interfaces without the Diamond Problem!
Explanation
- A class can implement more than one interface.
- A caller can use the same object through any interface it implements.