-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
  • 125,519
  • 44
  • 317
  • 399
keai4le
  • 399
  • 1
  • 3
  • 11

1 Answers1

2

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
  • 90,369
  • 161
  • 124
  • 175
arif abbas
  • 331
  • 2
  • 6