// Example of Constructor Overloading in Java
class Student {
String name;
int age;
String course;
// Default constructor
Student() {
name = "Unknown";
age = 0;
course = "Not Assigned";
// Constructor with one parameter
Student(String n) {
name = n;
age = 18; // default age
course = "General Studies";
// Constructor with two parameters
Student(String n, int a) {
name = n;
age = a;
course = "Computer Science";
// Constructor with three parameters
Student(String n, int a, String c) {
name = n;
age = a;
course = c;
void display() {
[Link]("Name: " + name + ", Age: " + age + ", Course: " + course);
public static void main(String[] args) {
// Creating objects using different constructors
Student s1 = new Student();
Student s2 = new Student("Alice");
Student s3 = new Student("Bob", 20);
Student s4 = new Student("Charlie", 22, "Mechanical Engineering");
// Displaying the student details
[Link]();
[Link]();
[Link]();
[Link]();
Name: Unknown, Age: 0, Course: Not Assigned
Name: Alice, Age: 18, Course: General Studies
Name: Bob, Age: 20, Course: Computer Science
Name: Charlie, Age: 22, Course: Mechanical Engineering
stack explanation
Operation Description Example
push(x) Adds (inserts) an element x on top of the stack push(10)
Operation Description Example
pop() Removes the element from the top of the stack Removes 10
peek() / top() Shows the top element without removing it Returns 10
isEmpty() Checks if the stack is empty true / false
display() (Optional) Shows all elements in the stack [10, 20, 30]
Operation Stack (Top → Bottom)
push(10) 10
push(20) 20, 10
push(30) 30, 20, 10
pop() 20, 10
peek() Shows 20
USING JAVA BUILT-IN STACK
import [Link];
//this line imports Stack class from [Link] package in java standard library .
Stack<Integer> stack = new Stack<>();
//* Meaning
Stack The class name (from [Link] package)
<Integer>
A generic type — it tells Java that this stack will store Integer objects (not
Strings, not Doubles, etc.)
stack The variable name (you can choose any name)
= Assignment operator — assigns the newly created object to the variable
new
Stack<>() Creates a new instance (object) of the Stack class*/
[Link](10);
[Link](20);
[Link]();
[Link]([Link]());
USING CUSTOM STACK [ARRAY BASED]
class Stack {
int[] arr;
int top, size;
Stack(int size) {
arr = new int[size];
top = -1;
void push(int x) { ... }
int pop() { ... }