-1

I am very new in programming, while learning Java i got into a doubt, my situation is, I am trying to call a implemented method of a class TestingImpl1111 from interface ITesting1111 in a test class by creating a object of a abstract parent class using child class constructor, but i am not able to call the method m1() which was as implemented in class TestingImpl1111 as out put. it always brings the overridden method m1 in class TestingImpl2222

this is my code below :-

package PolymorphismAndAbstraction.Example1.Example2;

interface ITesting1111
{
    void m1();
    void m2();
    void m3();
}
abstract class TestingImpl1111 implements ITesting1111
{
    public void m1()
    {
        System.out.println("testing method from interface in class 1111");
    }
}
abstract class TestingImpl2222 extends TestingImpl1111
{
    public void m1() 
    {
        System.out.println("testing method m1 from interface in class 2222 which was implemented in class 1111 as well.");      
    }   
    public void m2() 
    {
        System.out.println("testing method from interface in class 2222");      
    }   
}
 class TestingImpl3333 extends TestingImpl2222
{
        public void m2() 
        {
            System.out.println("testing method m2 from interface in class 3333 which was implemented in class 2222 as well.");      
        }
    public void m3()
    {
        System.out.println("testing method from interface in class 3333");      
    }
}

public class Test1234 
{
    public static void main(String[] args) 
    {   
        TestingImpl1111 obj = new TestingImpl3333();        
        
        ((TestingImpl1111)obj).m1();
        obj.m1();
        ((TestingImpl2222)obj).m2();
        obj.m2();       
        obj.m3();       
    }

}

and below is output i am geting :-

testing method m1 from interface in class 2222 which was implemented in class 1111 as well.
testing method m1 from interface in class 2222 which was implemented in class 1111 as well.
testing method m2 from interface in class 3333 which was implemented in class 2222 as well.
testing method m2 from interface in class 3333 which was implemented in class 2222 as well.
testing method from interface in class 3333

But output i am expecting is :-

testing method m1 from interface in class 2222 which was implemented in class 1111 as well.
testing method from interface in class 1111
testing method m2 from interface in class 3333 which was implemented in class 2222 as well.
testing method from interface in class 2222
testing method from interface in class 3333

Please explain me what I am doing wrong here how do i get my desiered output ??

  • done sorry typing mistake! :) I have corrected it now :) – Siddharth Mone Mar 28 '23 at 19:24
  • @Progman no it doesn't answer my question as I have created an abstract class I need to call a method of the parent class I am expecting an output as mentioned in my question. I don't understand how I get that output! – Siddharth Mone Mar 28 '23 at 19:32
  • 1
    There are a lot of ways you *could* get to your desired output, but fundamentally, you need to understand that your `obj` is a pointer to a `TestingImpl3333` object that only contains the `m1` implementation from your `TestImpl2222` class. Casting your `obj` to a type of `TestingImpl1111` doesn't swap out the implementation of your `m1` function. Generally casting objects is more for appeasing the compiler or for enforcing certain "rules" in your code, rather than changing functional behavior of your code (as opposed to casting primitive types, like double to int, which can change behavior). – Jake Mar 28 '23 at 19:36
  • 2
    The accepted answer for [How to call the overridden method of a superclass?](https://stackoverflow.com/questions/15668032/how-to-call-the-overridden-method-of-a-superclass) has the correct answer: _You cannot do what you want. The way polymorphism works is by doing what you are seeing._ And this will never change because that is the way Java is designed to do it. – Thomas Kläger Mar 28 '23 at 19:37

1 Answers1

0

Ok, casting an object to its parent wont make it a parent object.

In your case, obj is an instance of TestingImpl3333.

So whatever method you call on it, it will first check whether TestingImpl3333 is implementing it or not. If implemented, it uses that.

If not, it will look into its parent.

Thats how polymorphism works

Renjith
  • 3,274
  • 19
  • 39