Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Ambiguous method calls in case of inheritance

Thread view: 
Peter Meier - 14 Nov 2005 09:59 GMT
I have a simple problem. I have a class A and a class B which extends
class A. Both classes have a protected method (e.g. 'print') with the
same signature (so B's method override A's method). If I have an
instance of class B which calls some public method of class A in which
the 'print' method is called then the JVM automatically calls B's
'print' method. How can I achieve that A's 'print' method is called? I
already tried it with the following:
'this.print();' or '((A)this).print();' but neither works. I never see
the print out 'Base class'. Could anyone help me (see code snippet
below). Thanks!

Peter

public class A {
   public A() {
   }

   protected void print() {
       System.out.println("Base class");
   }

   public void doSomething() {
       print();
   }

   public static void main(String[] args) {
       B b = new B();
       b.doSomething();
   }
}

class B extends A {
   public B() {
   }

   protected void print() {
       System.out.println("Sub class");
   }
}
Roedy Green - 14 Nov 2005 10:29 GMT
>How can I achieve that A's 'print' method is called? I
>already tried it with the following:

you could do it like this by adding a method in to B to give access to
A's print.

 void oldPrint()
  {
  super.print();
  }
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Bjorn Abelli - 14 Nov 2005 12:08 GMT
"Peter Meier" wrote...

> I have a simple problem.

I don't see it as that "simple"... ;-)

> I have a class A and a class B which extends
> class A. Both classes have a protected method
> (e.g. 'print') with the same signature (so B's
> method override A's method).

Here you've got some decisions to make.

Do you *want* it to be "overridable" at all?

As you seem to want this particular method to be invoked when doing
something, you might want to make it "final"...

public class A {
...
   final void print() {
       System.out.println("Base class");
   }
...
}

This way you prevent it from being overridden, and if you want a "similar"
method in a subclass, you're forced to name it differently.

class B extends A {
...
   protected void printB() {
       System.out.println("Sub class");
   }
}

Another option is to *hide* it from the subclasses, by simply making it
private.

public class A {
...
   private void print() {
       System.out.println("Base class");
   }
...
}

In this case, the call will be on "the level of A", and doSomething will
invoke A.print, even if you have a print-method in the subclasses, until you
decide to override doSomething! ;-)

> How can I achieve that A's 'print' method is called?

> I already tried it with the following:
> 'this.print();' or '((A)this).print();'
> but neither works. I never see
> the print out 'Base class'.

You need to define for yourself *why* you need this behaviour, and then you
possibly can see whether it fits into one of the categories i sketched
above.

// Bjorn A
HalcyonWild - 14 Nov 2005 14:09 GMT
> I have a simple problem. I have a class A and a class B which extends
> class A. Both classes have a protected method (e.g. 'print') with the
[quoted text clipped - 35 lines]
>     }
> }

If you can program the situation in which A.print() should be called,
do that.

print() //class B
{
   if (some condition is true) { super.print(); }
   else  print();
}

If not, and still want control on which print to call, have another
method in B to call A.print(), something like

otherprint() // or overload(not override) as print(int k)
{
   super.print();
}

But frankly, I do not understand the need for doing all this. What is
the problem for which you want to use both print methods.
charles_n_may@yahoo.com - 14 Nov 2005 15:25 GMT
>From a modeling perspective, should B really be a specialized type of
A? Consider whether B and A could be sibling classes with an abstract
parent class, P. Implement P.print() and A.print(), but let B inherit
print() from P.

> I have a simple problem. I have a class A and a class B which extends
> class A. Both classes have a protected method (e.g. 'print') with the
[quoted text clipped - 35 lines]
>     }
> }
Andrew McDonagh - 14 Nov 2005 22:14 GMT
> I have a simple problem. I have a class A and a class B which extends
> class A. Both classes have a protected method (e.g. 'print') with the
[quoted text clipped - 35 lines]
>     }
> }

If you need the 'print' method to have the same name and parameters list
(even an empty one as above), then you can't have a different
implementation in B and expect the A.print() to be used.

All over ridden methods in Java are Virtual - (i.e polymorphic).

However, as Charles says, your issue is how you are modeling the
relationship between A & B.

You can do as Charles recommends:

public abstract class Printer {
   public void print();
}

public class A extends Printer {

    public void print() {
        System.out.println("A class called");
    }

    public void doSomething() {
        print();
    }

    public static void main(String[] args) {

        Printer printer = null;

// Just showing how we can reassign the ref to point to
// the different subclasses of Printer

        printer = new A();
        a.doSomething();

        printer = new B();
        b.doSomething();
    }
}

class B extends Printer  {
    public B() {
    }

    public void print() {
        System.out.println("B class called");
    }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.