-6

I am new to Java, need some help

I have an abstract superclass that has 2 protected attributes

public abstract class Superclass {
  protected int a = 0;
  protected int b = 0;
  ...
  }

then I have a subclass that extends the superclass, and i wish to access a and b, but I dont know how. I searched the web and didnt find anything.

public class Subclass extends Superclass {
  public boolean someMethod(){
  .....
  // at the end i need to do
  a += 1;
  return true
  }
}

I get the compilation error: "unreachable statemen"

thanks for your help.

Alessandroempire
  • 1,640
  • 4
  • 31
  • 54

6 Answers6

2

An interface can't define protected members. You should see a compiler error.

protected members of a superclass are available to subclasses: you need only reference them in any class that extends the superclass (abstract superclass or otherwise).

Refer to the access modifier table here.

pb2q
  • 58,613
  • 19
  • 146
  • 147
2

If you have this

abstract class TestABC {
    protected int a = 0;
    protected int b = 0;
}

and then this:

public class TestABCD extends TestABC{
    public void increaseA() {
      try {
        //do method including return
      } finally {
         a++;
      }
    }

    public String printIt() {
      System.err.println(t);
      increaseA();
      System.err.println(t);
    }

    public static void main(String[] args){ 
        TestABCD t = new TestABCD();
        t.printIt();
    }
}

You can see that I can directly access t.a in the println because my main method is within the subclass. If the variable was private instead of protected, you would not be able to access it.

jcern
  • 7,798
  • 4
  • 39
  • 47
  • ok ok i know that. but i get a compilation error: unreachable statement. And i know its protected. I dont understand why its giving me this error. – Alessandroempire Oct 05 '12 at 23:57
  • can you post your method code? I can't say for sure, but it sounds like the method encounters a return statement before it reaches that line. – jcern Oct 05 '12 at 23:59
  • added the code. its quiet long so i am just asking for the specif part that doesnt work – Alessandroempire Oct 06 '12 at 00:04
  • You need to access the variable within a method. I updated my above example to show you how that would work. – jcern Oct 06 '12 at 00:06
  • yeah thats the problem, i am accessing it inside a method and it keeps throwing unreachable statement – Alessandroempire Oct 06 '12 at 00:07
  • Is there a return statement before you increase the variable? – jcern Oct 06 '12 at 00:11
  • Well, what you have posted basically compiles fine. You can try moving the increment to a finally block. But essentially the error you are getting is because that code can never be executed because the logic of what your method can never get there. We'd need to see more of the code for the method to figure out where the error is. A method can have multiple return statements in it. – jcern Oct 06 '12 at 00:20
1

"Unreachable statement" means that there is no path in your code that can get you to this line in your program. It has nothing to do with protected fields. Check, if there is a return statement before your a+=1 that always exits your method before this line is ever reached. Or, if your a+=1 is in an if-block that is never executed, because the condition you specified always evaluates to false.

Markus A.
  • 12,349
  • 8
  • 52
  • 116
0

the class should have access to a & b just like that but if you declare an a or b that "hides" the class extends class if you want the a & b from the super class you just use them like that

gets a little to be a house of mirrors but it is handy sometimes when things get involved

0

If you have a subclass that extends a superclass with protected methods such as

public abstract class SuperClass{

protected int a = 0;
protected int b = 0;

}

All you have to do is call those variables directly in the subclass as follows:

public class SubClass extends SuperClass{

    public Subclass(){
       System.out.println("int a value:" + a);
       System.out.println("int b value:" + b);
    }
}
Elbek
  • 3,434
  • 6
  • 37
  • 49
josh-cain
  • 4,997
  • 7
  • 35
  • 55
0

Your problem has nothing to do with protected at all. It has to do with you're trying to place code statements out naked in the class where they don't belong. Instead place that line of offending code in a method or constructor.

public class Subclass extends Superclass {
  a += 1; // **** this line ****
}

the commented line is not valid. The only code that can belong there naked in the class are variable declarations or declarations with initialization, not statements that don't involve declaration of variables. Again, put that line in a constructor or method:

public class Subclass extends Superclass {
  public Subclass() {
    a += 1; // **** this line ****
  }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373