←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Diagrams

Diagrams

Visual representations for Unit 2

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. DeclarationStudent s1; (Creates reference on Stack)
→
2. Instantiation (new)Allocates memory for object on Heap
→
3. InitializationCalls Constructor Student() to set fields
Explanation
  • 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.
Detailed Notes →Quick Revision →

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.
Detailed Notes →Quick Revision →

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., s1 holding address 0x4A2)
  • Automatically freed as soon as the method finishes execution
Heap Memory (Large, Dynamic)
  • Stores ALL Objects created with new (e.g., new Student() at 0x4A2)
  • 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.
Detailed Notes →Quick Revision →

Figure 2.4: Converting Between Primitives and Wrapper Objects

Figure 2.4: Converting Between Primitives and Wrapper Objects
Primitive Valueint a = 25;
→ Autoboxing →
Wrapper ObjectInteger obj = a;
→ Unboxing →
Primitive Valueint 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.
Detailed Notes →Quick Revision →

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.
Detailed Notes →Quick Revision →

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!
Explanation
  • A class can implement more than one interface.
  • A caller can use the same object through any interface it implements.
Detailed Notes →Quick Revision →