-1

Can a constructer made of same class access private members of parameter ?

in a constructor " Queue(Queue ob) " , how is it possible to access 'ob's private members' ?

class Queue{
private char q[];
private int putloc, getloc;

Queue(int size){
    q = new char[size];
    putloc = getloc = 0;
}

Queue(Queue ob){
    putloc = ob.putloc;
    getloc = ob.getloc;
    q = new char[ob.q.length];

    for(int i = getloc; i < putloc; i++)
        q[i] = ob.q[i];
}

void put(char ch){
    if(putloc == q.length){
        System.out.println(" - Queue is full. ");
        return;
    }
    q[putloc++] = ch;
}

char get(){
    if(getloc == putloc){
        System.out.println(" - Queue is empty. ");
        return (char)0;
    }
    return q[getloc++];
}
}
public class QDemo2 {
    public static void main(String args[]){
        Queue q1 = new Queue(10);
        Queue q2 = new Queue(q1);
    }
}

1 Answers1

0

Well that's how java works. Queue can access its own private fields, even of another instance.

Sason Ohanian
  • 795
  • 5
  • 16