1

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"
    }
}
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
kensuke1984
  • 949
  • 1
  • 11
  • 21
  • @TheLostMind No, the type of the reference doesn't affect how methods are dynamically dispatched. – awksp Jul 08 '14 at 09:30

8 Answers8

1

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"
    }
}
OO7
  • 2,785
  • 1
  • 21
  • 33
1

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
Community
  • 1
  • 1
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
0
super.method_name(); 

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

user3774329
  • 91
  • 1
  • 11
0

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.

Pracede
  • 4,226
  • 16
  • 65
  • 110
0

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.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
0

create the object of Super and call print method

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

output:

hi
learner
  • 365
  • 1
  • 3
  • 16
0

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

user3757014
  • 222
  • 1
  • 3
0

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

Awanish
  • 48
  • 1
  • 9