RE: Keyword super in Java



I don’t undestand the following example code :

Class A
{      void f()
{   System.out.println(“call to method f() of class A”) ;

Class B extends A
{      void f()
{ System.out.println(“call to methode f()  of class B”);

public void test()
{   A a = super ;
super.f() ;

Public class Super
{   public static void main(String args[])

{ B b = new B();

Result :

call to method f() of class B // Why  a.f() display the call to f()  of class B ?
call to method f() of class A  // Ok  for me, super is a reference to an object of class A.
call to methode f() of class B // Ok, “this” is a reference to the object that call test()

Thank you.


braheim Default Asked on September 21, 2014 in Selenium WebDriver.
Add Comment
3 Answers
Best answer

Hi Braheim,

Two things
first A a = super ; statement should give you compiler error. At least of jre 7.

However, the call should go to the Super class not the sub class. Can you try again and let us know. Put debug breakpoints this time.


Professor Answered on September 23, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.