2

Can I call a parent class overridden method with the child class object in java?

I tried below example

class First1
 {
   void show()
    {
    String msg="You are in first class";
    System.out.println(msg);
    }
 }   
 class second extends First1  
 {  
   void show()  
   {  
   String msg="You are in second class";  
   System.out.println(msg);          }  
   }  
 }
 class CallingMethod extends second  
 {  
   void show()  
   {  
    String msg="You are in the third class";  
    System.out.println(msg);  
   }  
    public static void main(String[] args)  
    {  
    CallingMethod cm=new CallingMethod();  
    cm.show();  
    }  

}

Now tell me if it is possible to print "I am in second class." by using the object of CallingMethod class that is cm here in example and without using super keyword anywhere.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
P3M
  • 149
  • 3
  • 10
  • 3
    Did you try an example? If so post it and let us know where you are having issues. – kosa Aug 08 '12 at 14:51
  • 6
    Why would you not want to use the super keyword? What's the object of this game you're having us play? – Hovercraft Full Of Eels Aug 08 '12 at 14:53
  • Yes I did, I have three classes called First, Second and Third, Second extends First, Third extends Second. And each class have a method void show(), I made a object t of class third and I want to call and print the show() method of the second class with the object t. How can I do it @thinksteep – P3M Aug 08 '12 at 14:55
  • @HovercraftFullOfEels My teacher asked me and told me to do so without using super, That's why. – P3M Aug 08 '12 at 14:59
  • According to the answers people are taking wild guesses on what you are actually asking for. Could you edit your question and add your code? One answer claims **Nope** and one claims **Yes**. – maba Aug 08 '12 at 14:59
  • 1
    It's almost like asking "if I break a puppy's legs, can I still teach him to play fetch?". My question would be: "why break his legs?" – Hovercraft Full Of Eels Aug 08 '12 at 15:06
  • @HovercraftFullOfEels That's true.. I wud luv to ask my teacher the same question.. See the code, I have posted and tell me if u can do something about it. – P3M Aug 08 '12 at 15:31
  • @thinksteep check the code plz – P3M Aug 08 '12 at 15:40
  • http://stackoverflow.com/questions/5411434/how-to-call-a-superclass-method-using-java-reflection is potentially of interest. – Jesse Glick Aug 08 '14 at 20:26

4 Answers4

4

I assume you mean to call the method from outside of the subclass.

Then no, not possible in java, since an overriden method means changed behaviour that makes sense for the new class.

Inside of the class, you would use the super keyword in any case.

NOTE: using Reflection you can do things with objects, that the language itself does not allow.

NOTE: I tested this using Reflection, it does NOT work. But when you use C with JNI you might be able to do that...

//does not work
class YourClass
{
    public static void main(String[] args) throws SecurityException,
            NoSuchMethodException, IllegalArgumentException,
            IllegalAccessException, InvocationTargetException
    {
        CallingMethod cm = new CallingMethod();
        First1 f = new First1();
        // Method m = First1.class.getDeclaredMethod("show");
        Method m = First1.class.getMethod("show");
        m.invoke(f);
                    //output: You are in first class
        m.invoke(cm);
                    //output: You are in the third class
    }

}
John Smith
  • 2,282
  • 1
  • 14
  • 22
  • Ah, so *that's* what he's talking about! +1 – erickson Aug 08 '12 at 15:00
  • Ok.. And how can u say that with that much confidence? – P3M Aug 08 '12 at 15:34
  • @P3M That's a hard question. Here is the docu for [super](http://docs.oracle.com/javase/tutorial/java/IandI/super.html). That said I'm in the position, that I know (from reading) what the language can do (not just super), but I can never be perfectly sure that it cannot do the things that are not documented. Maybe your teacher wants to hear about Reflection. Reflection allows you to access private methods and properties and it might allow you to do what your teacher wants as well.From that point of view, you can tell him, that if you use C, you can do virtually anything with your java classes. – John Smith Aug 08 '12 at 16:02
  • @P3M see my other answer for an explanation why java does not allow this. – John Smith Aug 08 '12 at 16:55
0

Yes, here there is an example overriding methods:

http://www.cs.umd.edu/~clin/MoreJava/Objects/overriding.html

Lo Juego
  • 1,305
  • 11
  • 12
0

you can use super.overriddenMethod() as long as you are calling it inside the child class itself.

Byter
  • 1,132
  • 1
  • 7
  • 12
  • Yeah I know, but the problem is I am not allowed to use the super keyword, now what? – P3M Aug 08 '12 at 15:33
0

Maybe you want something like this:

class A
    method()
class B extends A
    method()
class C extends B
    method()
    {
        //call A.method()
    }

Which is impossible in Java as well. You can only call methods of your direct super class. And you allways need to use

super

EDIT: And this is why:

class A
{
  private int positionA;
  void move()
  {
    positionA++;
  }
  int getPosition()
  {
    return positionA;
  }
}
class B
{
  private int positionB;

  void move()
  {
    positionB++;
  }
  int getPosition()
  {
    return positionB;
  }
}
A a = new A()
B b = new B()

If you run

b.move()

then positionB is incremented. You would get what you would expect from a call to getPosition().

If you COULD run

A.move() 

on b

it would increment positionA. Hence a call to b.getPosition() would not return the correct position.

If you had

Class C extends B

you would bypass B's move() if you could call

A.move()

on

this. 

It's the same problem as outside of the class. Your classes would behave strangely and this is why the Java developers disallowed it.

John Smith
  • 2,282
  • 1
  • 14
  • 22