1

i have the following code :

class a {
    void show() {
        System.out.println("in a");
    }
}

class b extends a {
    void show() {
        System.out.println("in b");
    }
}

class c extends b {
    void show() {
        // can i do here
        ((a)super).show();
    }
}

kindly update if we can cast super variable to the superclass type.

b. why variables do not show the polymorphism phenomenon that overridden methods show.

c. i read that super.super.methodname() does not work removed to avoid violation of encapsulation.can anybody clarify on this how this is .

thanks

jmj
  • 237,923
  • 42
  • 401
  • 438
user2779311
  • 1,688
  • 4
  • 23
  • 31
  • First of all super is not a variable. It's a keyword. – Vivin Jun 20 '14 at 17:21
  • 1
    possible duplicate of [Why is super.super.method(); not allowed in Java?](http://stackoverflow.com/questions/586363/why-is-super-super-method-not-allowed-in-java) – ajb Jun 20 '14 at 17:25

1 Answers1

1

b. why variables do not show the polymorphism phenomenon that overridden methods show.

because field variables aren't polimorphic in java

c. i read that super.super.methodname() does not workis removed to avoid violation of encapsulation.can anybody clarify on this how this is .

because it isn't supported in java

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • but i wanted to know how this violates encapsulation.kindly update on this. – user2779311 Jun 20 '14 at 17:23
  • @user Jon Skeet has given very good explanation on that post, summarizing it, you shouldn't be allowed to control directly on your super's super bypassing it – jmj Jun 20 '14 at 17:25
  • can super be cast to any class type – user2779311 Jun 20 '14 at 17:35
  • super refers to reference of parent type, for example in case of Animal and Human human's super would refer to Animal so you can cast super to any valid reference it can point to – jmj Jun 20 '14 at 17:36
  • why we are not able to cast super to class a .is it that java does not allow calling grandparent class methods from grandchildren class – user2779311 Jun 20 '14 at 17:42
  • _why we are not able to cast super to class a_ elaborate , answer to _a_ is already given – jmj Jun 20 '14 at 17:43