-4

Assume there are two classes:

class A{
    public int a=0;
}
class B extents A{
    public int a=1;
}

I've tried this code:

A a=new B();
B a1=new B();

and a.a is 0, a1.a is 1.

Is there any method to access a1's super.a?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
keai4le
  • 623
  • 2
  • 7
  • 13

1 Answers1

3

Create a method in class B to fetch the value of superclass variable a through super.a

class A {  
    public int a=0;
}  

class B extends A {  
    public int a=1;

    public int superClassA() { 
        return super.a; 
    } 
}

Use a1.superClassA() to fetch the value for a from class A

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
arif abbas
  • 341
  • 2
  • 6