Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

super.super

 
alec stewart stewart
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all

suppose we have a class a which extends b and b in turn extends c

all three classes have a method tell();

now how do i call the method tell of c from a

can i do like this super.super

and also what will be the super class of a will it be b or c
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because a extends b, b is the superclass

You cannot access the methods in c if b overrides them.
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

because a extends b, b is the superclass
You cannot access the methods in c if b overrides them.


Steven please can you explain your statement in reference to the following code below,


In the code above, engine() is a method present in LuxaryCar, Buick and BuickLeSabre but I am still accessing method engine() of LuxaryCar. I think that was the question that Alec had.

suppose we have a class a which extends b and b in turn extends c
all three classes have a method tell();
now how do i call the method tell of c from a


Also Steven please can you explain this particular statement with reference to what doubt Alec had,

You cannot access the methods in c if b overrides them.


I think you forgot to mention Where you cannot access the methods and Why ?, I think after that explaination your answer would be complete.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Alec was awfully clear:


now how do i call the method tell of c from a


He wants to call the version of "tell" inherited from "c" directly from "a". Steven's answer is concise and correct -- it can't be done. No need for any examples: both Alec and Steven understand one another.
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a security issue. A may not know all the reasons B overrode C's method. To allow A to bypass B and directly access the method in C could cause problems.

Suppose C was a generic elevator control object. The original designers used Imperial units to control the maximum velocity of the elevator. B comes along and overrides C's move method so it now takes velocities in metric units. Finally A comes along and wants to change the acceleration profile so it moves smoothly but doesn't realize B.move was actually a wrapper function that changed the units. A bypasses B completely and calls super.super.move() with Metric velocity units.

Elevator crashes. People die.

Java prevents direct access to "grandparent" methods to prevent this from happening. Also note this is true for methods AND constructors for the same reason.

Cheers,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic