kensuke1984
kensuke1984

Reputation: 995

How to call super-version of a method that is overridden?

I know how to call a method in a super class from a subclass by super. even when the method is overriden. But how can I call the method in the super class?

Is it impossible?

public class Test extends Super{

    public static void main (String[] args){
        Test t = new Test();
        t.print();
    }

    void print1(){
        System.out.println("Hello");
    }
}


class Super{

    void print1(){
        System.out.println("hi");
    }

    void print(){
        print1();
        // What can I do when I want to print "hi"
    }
}

Upvotes: 1

Views: 132

Answers (8)

aliteralmind
aliteralmind

Reputation: 20163

Once you override a function, the super-version of that function is no longer accessible, neither to the super class or the sub-class. It's called being "hidden". The only exception is calling it from within the overridden function, via super:

public class Test extends Super{
    void print1(){
        System.out.println("Hello");
        super.print1();                   //Here
    }
}
class Super{
    void print1(){
        System.out.println("hi");
    }   
}

(This is exactly the reason that constructors must never directly call functions that are potentially overridable.) You could also make the Super.print1() private, which would mean that, even though there is a Test.print1(), it does not override anything, because, as far as it's concerned, the super-version does not exist (is not visible).

So this:

public class Test extends Super{
   public static void main (String[] args){
      Test t = new Test();
      t.print();
   }
   void print1(){
      System.out.println("Hello");
   }
}
class Super{
   private void print1(){
      System.out.println("hi");
   }
   void print(){
      print1();
      this.print1();
   }
}

Outputs this:

hi
hi

Upvotes: 1

OO7
OO7

Reputation: 2807

Just give your super class print1() method private visibility.

class Super{
    private void print1(){
       System.out.println("hi");
    }
    void print(){
        print1();
        // What can I do when I want to print "hi"
    }
}

Upvotes: 1

Awanish
Awanish

Reputation: 48

Just use: super.method_name() from your method.

Upvotes: 0

JCW
JCW

Reputation: 222

If you definitely want to call Super.print1() from Super.print then you can make it private. Test.print1() will then not override it.

Upvotes: 0

learner
learner

Reputation: 365

create the object of Super and call print method

 Super s = new Super();
 s.print();

output:

hi

Upvotes: 0

Stultuske
Stultuske

Reputation: 9437

actually, if you want to call the method from the parent class, you shouldn't have overridden it.

the fact that you did override it, means that for each instance of Test, you want it to print "hello".

if you want "hi" to print, you have to call the print method through an instance of the parent class.

Upvotes: 0

Pracede
Pracede

Reputation: 4361

When you override a method from a super class, it's mean you don't need to use the implementation of the super class. So you cannot do that. You could read more about Polymorphism. It could help.

Upvotes: 0

user3774329
user3774329

Reputation: 91

super.method_name(); 

That's how you call a method of the super class. If you want to do something different, please explain.

Upvotes: 0

Related Questions