Results 1 to 9 of 9
  1. #1
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Question Adding to a function from a different class

    Is it possible, if so what is it called?

    I'd like to be able to 'edit' a function from another class. Eg

    Class a
    function hello
    {
    say hello.
    }

    Class b
    edit hello function from a and add
    name.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Adding to a function from a different class

    If Class B extends Class A you can use the super keyword to call the method in the parent class.

  3. #3
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Default Re: Adding to a function from a different class

    Super keyword?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Adding to a function from a different class

    Yeah!

    I know this might be a bit too much for me to expect but I assumed that if there was something you didn't understand that you would do some research. Google would be a good place to start or maybe your textbook.

  5. #5
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Default Re: Adding to a function from a different class

    Super/overriding is not what I'm after though. I'm after the ability to in effect 'remotely edit' a function. Eg

    class a
    function a
    {
    do this
    }

    class b
    function a
    {
    do whatever is already in the function
    then do this
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Adding to a function from a different class

    If you want a method in class B to call a method in class A then you will need an instance of class A in class B.

  7. #7
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Default Re: Adding to a function from a different class

    No. This is quite difficult to explain. I'll give you a real example.

    My class extends another class with various functions. I want to use one of those functions but add stuff to it. I can't call that function and then add some extra stuff outside the function or else it won't get called in the thread which is present in the extended class.

    Java Code:
    public class MyGame extends GameCanvas
    {
    	AppleEntity pear;
    	public MyGame()
    	{
    		pear = new AppleEntity("../resources/apple.png",100,100);
    	}
    	
    	@Override
    	public void Render()
    	{
    		run code from GameCanvas Render method
    		and then
    		... add some stuff here to execute too...
    	}
    }
    Hope this makes sense.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Adding to a function from a different class

    I have already given you the solution. USE SUPER!

  9. #9
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Default Re: Adding to a function from a different class

    Oh sorry! Missed the fact it was also inside the brackets of the overridden method. My apologies! Thanks

    Java Code:
     public void printMethod() { //overrides printMethod in Superclass
            super.printMethod();
            System.out.println("Printed in Subclass");
        }

Similar Threads

  1. Replies: 0
    Last Post: 10-23-2011, 10:49 PM
  2. Code for adding search function in an application
    By Avdhut in forum Threads and Synchronization
    Replies: 5
    Last Post: 03-03-2009, 10:15 AM
  3. problem calling function from class to class
    By alin_ms in forum New To Java
    Replies: 3
    Last Post: 12-19-2008, 07:35 PM
  4. Replies: 1
    Last Post: 11-17-2008, 09:32 AM
  5. Register a callback function in other class
    By barts2108 in forum New To Java
    Replies: 2
    Last Post: 11-10-2008, 04:24 PM

Posting Permissions

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