Results 1 to 3 of 3
  1. #1
    Randron is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default How to have the subclass method be called instead of the superclass method

    I have a situation that is basically like this:

    Class A {
    ...

    void method1() {
    if (... && method2()) {
    method3();
    for (...) {
    ...
    }
    } else {
    ...
    method4();
    }
    }
    }

    I want to have many more classes which extend A, but which have different implementations of method2(), method3(), and method4(). However, all of these classes will have the same loop structure found in method1(). I want to make it so that when method1() is called on an instance of one of the subclasses, the subclass version of methods 2 3 and 4 will be called, not the superclass version, without have to override method1() by redundantly pasting that code into every subclass. Is there any way around having this redundant code? Thanks!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,388
    Rep Power
    5

    Default Re: How to have the subclass method be called instead of the superclass method

    If all of your subclasses provide their own implementation of methods 2,3,and 4. Then method1 will automatically call them even if method1 is not overridden in the subclass. Of course, Class A needs to have those methods declared somewhere, perhaps as part of an abstract class, interface implementation, or just some dummy methods. Otherwise, the method1 in class A won't know about them.

    Is this what you want?

    Java Code:
    public class SubClassing {
    
       public static void main(String[] args) {
          BA ba = new BA();
          ba.method1();
          CA ca = new CA();
          ca.method1();
       }
    }
    
    class A {
       public void method1() {
          System.out.println("Method 1 -- Original definition in Class A");
          method2();
          method3();
       }
       
       public void method2() {};
       public void method3() {};
    }
    
    class BA extends A {
       public void method2() {
          System.out.println("Method 2 Overridden in subClass " + getClass().getSimpleName());
       }
       public void method3() {
          System.out.println("Method 3 Overridden in subClass " + getClass().getSimpleName());
       }
    }
    class CA extends A {
       public void method2() {
          System.out.println("Method 2 Overridden in subClass " + getClass().getSimpleName());
       }
       public void method3() {
          System.out.println("Method 3 Overridden in subClass " + getClass().getSimpleName());
       }
    }

    Regards,
    Jim
    Last edited by jim829; 07-10-2013 at 03:06 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Randron is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default Re: How to have the subclass method be called instead of the superclass method

    Quote Originally Posted by jim829 View Post
    If all of your subclasses provide their own implementation of methods 2,3,and 4. Then method1 will automatically call them even if method1 is not overridden in the subclass. Of course, Class A needs to have those methods declared somewhere, perhaps as part of an abstract class, interface implementation, or just some dummy methods. Otherwise, the method1 in class A won't know about them.

    Is this what you want?

    Java Code:
    public class SubClassing {
    
       public static void main(String[] args) {
          BA ba = new BA();
          ba.method1();
          CA ca = new CA();
          ca.method1();
       }
    }
    
    class A {
       public void method1() {
          System.out.println("Method 1 -- Original definition in Class A");
          method2();
          method3();
       }
       
       public void method2() {};
       public void method3() {};
    }
    
    class BA extends A {
       public void method2() {
          System.out.println("Method 2 Overridden in subClass " + getClass().getSimpleName());
       }
       public void method3() {
          System.out.println("Method 3 Overridden in subClass " + getClass().getSimpleName());
       }
    }
    class CA extends A {
       public void method2() {
          System.out.println("Method 2 Overridden in subClass " + getClass().getSimpleName());
       }
       public void method3() {
          System.out.println("Method 3 Overridden in subClass " + getClass().getSimpleName());
       }
    }

    Regards,
    Jim
    Thank you for clarifying this!

Similar Threads

  1. Subclass and Abstract method implementation
    By konman795 in forum New To Java
    Replies: 2
    Last Post: 02-24-2012, 09:44 AM
  2. Can not find subclass method
    By samanesh in forum New To Java
    Replies: 4
    Last Post: 11-24-2011, 05:26 PM
  3. subclass calling superclass method
    By stackptr89 in forum New To Java
    Replies: 10
    Last Post: 11-22-2010, 07:40 PM
  4. Using superclass fields in subclass method
    By lonegreyride in forum New To Java
    Replies: 12
    Last Post: 11-17-2010, 01:21 PM
  5. Invoking a superclass version of a overridden method
    By CyberFrog in forum New To Java
    Replies: 3
    Last Post: 05-25-2009, 01:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •