Results 1 to 4 of 4
  1. #1
    CyberFrog is offline Member
    Join Date
    Mar 2008
    Posts
    16
    Rep Power
    0

    Default Invoking a superclass version of a overridden method

    Hi all,
    I am currently going through an example on the above topic (see subject) and with this comes a snippet of code,

    XML Code:
    class Boxer extends Dog2 {
        public void printYourself () { 
         super.printYourself();  //Invoke the super class (Dog2)
                                //then do Boxer specific work here
         System.out.println("You have just printed your sub-method!!");
        }
    }
    
    
    public class Dog2{
        public void eat()  {}
        public void printYourself() {
         }
        public static void main(String [] args) {
            
        }
    
    }
    Now as far as I can see this example shows super being used to invoke an overridden method i.e. the Boxer method. I have added these print statements to emohasise what method is being used but it doesn't print, does anyone know how I can emphaises this example with these simple print statements? So that I print a statement when we use super

    Thanks

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    797
    Rep Power
    6

    Default

    Hi,
    put S.O.P inside printYourself meth of Dog2 class to make sure that super is called
    Ramya:cool:

  3. #3
    CyberFrog is offline Member
    Join Date
    Mar 2008
    Posts
    16
    Rep Power
    0

    Default

    Hi Ramya,

    I have just attempted this but it doesn't print?

    Java Code:
    class Boxer extends Dog2 {
        public void printYourself () { 
            
         super.printYourself();  //Invoke the super class (Dog2)
                                //then do Boxer specific work here
         
        }
    }
    
    
    public class Dog2{
        public void eat()  {}
        public void printYourself() {
                 System.out.println("You have just printed your supermethod!!");   
         }
        public static void main(String [] args) {
    
        }
    
    }
    Where in the printYpurself method does it go?

    Sorry as you cann tell I am a beginner : )

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    797
    Rep Power
    6

    Default

    Hi,
    Here subclass extends the functionality of super class .So create an instance of Boxer.

    So,inside the main method
    1. create an instance of Boxer which is a subclass which got the inherited functionalty.

    Boxer obj = new Boxer();

    2.Then call obj.printYourself();
    Ramya:cool:

Similar Threads

  1. Invoking Servlet from SWT application
    By wolve634 in forum SWT / JFace
    Replies: 1
    Last Post: 03-30-2009, 11:35 AM
  2. invoking javascript on in jsp
    By pankaj_salwan in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 12-12-2008, 04:54 PM
  3. Graphic settings being overridden?
    By sjchase in forum New To Java
    Replies: 0
    Last Post: 01-16-2008, 11:27 PM
  4. Invoking superclass methods... how?
    By rhobincu in forum New To Java
    Replies: 7
    Last Post: 08-09-2007, 03:10 PM
  5. invoking method and output questions
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:36 AM

Posting Permissions

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