In my program I've created two methods , one with integr type and other with float (line # 20 and 29) !!

class Stack{
int StackArrayI[] = new int[2]; //0,1,2,3,4,5,6,7,8,9
float StackArrayF[] = new float[2]; //0,1,2,3,4,5,6,7,8,9
int tos=-1;

public void push(int value){
    if (tos==1)
    System.out.print ("Stack already Full..!\n");
    else
    StackArrayI[++tos] = value; // tos=0
}

public void push(float fvalue){
    if (tos==2)
    System.out.print ("Stack already Full..!\n");
    else
    StackArrayF[++tos] = fvalue; // Storing values as Float
}

int pop(){
    if (tos==-1){
    System.out.print ("Stack is Empty");
    return 0;
    }
    else
    return StackArrayI[tos--]; 
}
    
float pop(){
    if (tos==-1){
    System.out.print ("Stack is Empty");
    return 0;
    }
    else
    return StackArrayF[tos--]; 
}
}

but when i'm running a program it is giving an error
Stack1.java:29: pop() is already defined in Stack
float pop(){
^
1 error

Why my program is not working ???

Overloading doesn't work on the basis of the return type. It only looks at the method signature which is composed of the method name and the parameters' list.

As far as Java is concerned you defined two identical methods.

commented: TY :) +0

i didn't understand ???
how can i fix it ??

I've also used two identical methods for push() and they are working

No, one is push(int) the other is push (float), unlike your pops which are both pop(). Check Serena5's post again, it's the answer you needed.

your methods push and pop are different:

Java will only look at the part of the message highlighted in red (the method signature):
public void push(int value)
public void push(float value)
int pop()
float pop()

As you can see pop are identical (they both have no arguments) whereas push have one argument but of differenttypes.

How to fix this:
In my opinion, the best way to deal with this would be to have two methods popInt and popFloat.
If you insist on having one method named pop, then use only float pop(), but then you need to cast values to integers.

hope this helps :)

I've understood :D thanks alot !!!
you said to use two methods popInt and popFloat , but our teacher told us to use Polymorphism technique in this program...!

if i use only float pop(), would it be considered as Polymorphism ???
here is the complete program!!

class Stack{
int StackArrayI[] = new int[2]; //0,1,2,3,4,5,6,7,8,9
float StackArrayF[] = new float[2]; //0,1,2,3,4,5,6,7,8,9
int tos=-1;

public void push(int value){
    if (tos==1)
    System.out.print ("Stack already Full..!\n");
    else
    StackArrayI[++tos] = value; // tos=0
}

public void push(float fvalue){
    if (tos==2)
    System.out.print ("Stack already Full..!\n");
    else
    StackArrayF[++tos] = fvalue; // Storing values as Float
}

int pop(){
    if (tos==-1){
    System.out.print ("Stack is Empty");
    return 0;
    }
    else
    return StackArrayI[tos--]; 
}
    
float pop(){
    if (tos==-1){
    System.out.print ("Stack is Empty");
    return 0;
    }
    else
    return StackArrayF[tos--]; 
}
}
 
class Stack1{
public static void main (String args[]){
    Stack first = new Stack();
    // Pushing values as integer in an Array...!
    for (int i=1;i<=2;i++){ 
        first.push(i);
        System.out.println(i +" pushed at tos= " + first.tos);
        }
    System.out.println("");
    // Popping values from Integer Array...!
    for (int i=1;i<=2;i++)
    System.out.println("Value at tos= " + first.tos +" is " + first.pop());
    System.out.println("");
    // Pushing values as Float in an Array...!
    for (float i=11;i<=12;i++){ 
        first.push(i);
        System.out.println(i +" pushed at tos= " + first.tos);
        }
    System.out.println("");
    }
}

solution 1: have two methods
int popInt() and float popFloat()
solution 2: only one method float pop()
but when assigning to an integer value, cast it as:
int x = (int)pop();

Hint:

abstract class Stack
class FloatStack extends Stack
class IntStack extends Stack

You have already used polymorphism for push().
If you've already covered inheritance, than go for JamesCherrill's suggestion

@Serena5, your suggestion is not working, i don't know why :'(

for (int i=1;i<=2;i++){ 
		first.push(i);
		System.out.println(i +" pushed at tos= " + first.tos);
		}
	System.out.println("");
	// Popping values from Integer Array...!

	for (int i=1;i<=2;i++){
			int b=(int)first.pop();
	System.out.println("Value at tos= " + first.tos +" is " + b);
	}

output:

1 pushed at tos= 0
2 pushed at tos= 1

Value at tos= 0 is 0
Value at tos= -1 is 0

Why the tos value becomes -1 ??
why 0 is popped instead of 1 and 2 ??

Problem solved , thanks alot !!!
i had to use the integer array in order to pop integer values!!! :D

Use the first suggestion, better and easier to implement.

By using your implementation of (float)pop():
values are entered into StackArrayI[] (first for loop)
but you are reading from StackArrayF[] which is empty at this point (second for loop)

cool no probs :)

i've done this,

for (int i=1;i<=2;i++){
	first.StackArrayF[first.tos]=first.StackArrayI[first.tos];
	System.out.println("Value at tos= " + first.tos +" is " + (int)first.pop());
	}

:)

the other way is to overload he pop() method, how can i overload the pop() method ???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.