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 / April 2007

Tip: Looking for answers? Try searching our database.

Query regarding calling parent's class method which is overridden in child class???

Thread view: 
parkarumesh@gmail.com - 26 Apr 2007 10:29 GMT
Hi,

I have a superclass say

public class Parent {

         public String go() {
            .
            .
            .
         }
}

Assume i have written some code in the go method of parent class.

Also i have a child class say,

public class Child {
        public String go() {
           .
           .
           .
           super.go();
        }
}

I have overridden the go method of parent class in the child class. I
write some code in the go() of Child class and give a call to Parent's
go() using super.go() as I want to execute the functionality written
in the go() of Parent.

I want to know is it logical to call super.go(), when go is actually
overridden by child class.

Regards,
parkarumesh
Ingo R. Homann - 26 Apr 2007 15:52 GMT
Hi,

> ...
> I want to know is it logical to call super.go(), when go is actually
> overridden by child class.

Yes, it's a (more or less ;-) common practise. That's what the "super"
keyword is used for (in this context).

The only other possibilty is not very common, IMHO:

class Sub extends Parent {

  void go() {
    ...
  }

  void bar() {
    super.go();
    // here it is more common to call this.go()
    // but of course this also depends on your use-case
  }

}

Ciao,
Ingo
Lew - 26 Apr 2007 22:34 GMT
parkarumesh@gmail.com wrote:
>> I want to know is it logical to call super.go(), when go is actually
>> overridden by child class.

> Yes, it's a (more or less ;-) common practise. That's what the "super"
> keyword is used for (in this context).
[quoted text clipped - 14 lines]
>
> }

Ingo is absolutely right.  To provide a little more detail, let's say a parent
(possibly abstract) class provides part of the setup, but wants the child
class to do the rest.

<sscce source="Child.java">
abstract class Parent
{
  public void init()
  {
    System.out.println( "Parent.init()" );
    establishConnection();
    doMoreParentalGuidance();
  }
  private void establishConnection()
  {
    System.out.println( "Parent.establishConnection()" );
  }
  private void doMoreParentalGuidance()
  {
    System.out.println( "Parent.doMoreParentalGuidance()" );
  }
}

public class Child extends Parent
{
  public void init()
  {
    System.out.println( "Child.init()" );
    super.init();
    doStuffOnlyChildNeeds();
  }
  private void doStuffOnlyChildNeeds()
  {
    System.out.println( "Child.doStuffOnlyChildNeeds()" );
  }
  public static void main( String [] args )
  {
    Parent parent = new Child();
    parent.init();
  }
}
</sscce>

The call to parent.init() will polymorphically call the Child version of
init(), which in turn invokes the Parent version through the super call.

Signature

Lew



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



©2008 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.