StackOverflowed
StackOverflowed

Reputation: 5975

Java Abstract Class (this keyword)

I think I'm misunderstanding something, and if so, I need help..

Let's say I have the following two classes:

public abstract class Abstraction() {
   protected int number = 0;
   public void printNumber() {
       System.out.println(this.number);
       System.out.println(getNumber());
   }
   public int getNumber() {
       return this.number();
   }
}

public class Reality extends Abstraction {
   int number = 1;
   public Reality() {
       this.printNumber();
   }
}

// some other class
new Reality();

What should the output be? I'm getting 0, 0 (the code here is a bit more complicated but still the same issue). How can I get the output to be 1,1?

Upvotes: 0

Views: 1154

Answers (4)

Mohammod Hossain
Mohammod Hossain

Reputation: 4114

public class Reality extends Abstraction {
    int number = 1;
    public Reality() {
        this.printNumber();
    }
}

Above the code number is an instance variable Of class Reality and calling method of super class. In super class printNumber() method will print value of number 0 because it is initialized with 0.

If you want to get subclass variable value of number, you have to pass value as method argument as follows:

public Reality(int num) {
    this.printNumber();
}

Upvotes: -1

nhaarman
nhaarman

Reputation: 100438

The number in the Reality class doesn't overwrite the number in the Abstraction class. Therefore, the Abstraction class still sees his own number value as 0.

A solution would be:

public class Reality extends Abstraction {
   public Reality() {
       number = 1;
       this.printNumber();
   }
}

Because your number in Abstraction is protected, you can access it in your Reality class.


Another example is a parameter in a method:

private int number;

public void myMethod(int number){
    number = 2;
}

Your private int number field won't be set, instead, the parameter number will be set as 2.


Finally, a word on the this and super keywords, see an edit of your classes:

public abstract class Abstraction() {
   protected int number = 0;
   public void printNumber() {
       System.out.println(this.number);
       System.out.println(getNumber());
   }
   public int getNumber() {
       return this.number();
   }
}

public class Reality extends Abstraction {
   int number = 1;
   public Reality() {
       System.out.println(number); //Will print 1
       System.out.println(this.number); //Will print 1
       System.out.println(super.number); //Will print 0
   }
}

Upvotes: 9

Luke Taylor
Luke Taylor

Reputation: 9599

You could create setter methods in the abstract class:

public void setNumber(int value) {
this.number = value;
}

They way you are trying to achieve this won't work, since the variable doesn't overrite that of the super class.

I hope this helps!

Upvotes: 1

Adam Arold
Adam Arold

Reputation: 30548

You are creating a local variable in Reality but the printNumber() method refers to the the member variable which is still 0.

If you don't create a local varible but you use the field instead like this:

number = 1;

it will be OK.

If you have problems understanding fields you can always refer to the official documentation which can be found here: Oracle java docs

Upvotes: 1

Related Questions