0% found this document useful (0 votes)
2 views4 pages

Java Constructor Overloading Example

The document provides an example of constructor overloading in Java through a 'Student' class with multiple constructors for different initialization scenarios. It also explains stack operations such as push, pop, peek, and isEmpty, along with examples of using both Java's built-in Stack class and a custom array-based Stack implementation. The document includes code snippets demonstrating object creation and stack operations, as well as their outputs.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Java Constructor Overloading Example

The document provides an example of constructor overloading in Java through a 'Student' class with multiple constructors for different initialization scenarios. It also explains stack operations such as push, pop, peek, and isEmpty, along with examples of using both Java's built-in Stack class and a custom array-based Stack implementation. The document includes code snippets demonstrating object creation and stack operations, as well as their outputs.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

// 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() { ... }

You might also like