0

I'm somewhat new to Java and I was wondering if there was a way to change the function of a class somehow.

Class pager = new Pager();// everything is initialized
pager.dostuff() = function(){};

Is there a specific name for this and is it possible to do in Java? If not, is there a language that does allow it?

Thank you for your time.

Edit:

To clarify the question because originally it seemed like I wanted to initialize the new class with different functions. That is not the case. I want to change it after it's already been created. The reason for this is that I'm working with android and the class I'm getting is from the xml. Is it possible to change the class' function when I get the class like so?

Pager pager = (ViewPager) findViewById(R.id.pager)

I feel like I'm going to have to create a new class, which is ok but I wanted to see if I could do it this way.

8
  • What exactly you want to do ? Also this is not valid syntax in java Commented Mar 14, 2014 at 6:06
  • 1
    Javascript will let you do something like this. But what exactly is your aim here? Commented Mar 14, 2014 at 6:06
  • 2
    Think the question is about function pointers in java. Have a look at this stackoverflow.com/questions/1073358/function-pointers-in-java Commented Mar 14, 2014 at 6:10
  • 2
    You would be allowed to do something similar in Java 8, but for now you'll have to wait. Of course not "replacing" a function but doing something similar with a "functional interface" Commented Mar 14, 2014 at 6:13
  • 1
    "is there a language that does allow it?" -- answer-- Javascript allows this. Commented Mar 14, 2014 at 6:24

3 Answers 3

5

In java you can override method at runtime like -

Pager pager = new Pager(){
  @Override
  public void dostuff(){
     ....
  }
};

Runtime it will create subclass of Pager and override the doStuff method.

Sign up to request clarification or add additional context in comments.

2 Comments

@AbhinavKumar and SubhrajyotiMajumder have the same code... may I know why?
Why! coz that is prefferered way may be. one additional thing @Override annotation which justify method closely.
1

Of course ..It is possible through overriding of a class through an annonymous inner class while inheriting all the properties of the class being overriden.

Pager pager = new Pager(){
  public void dostuff(){
     ....
  }
}

PS:- Beware,Outside the anonymous inner class ,you may invoke only those methods present in your parent class through the instance of your annonymous inner class

2 Comments

AbhinavKumar and @SubhrajyotiMajumder have the same code... may I know why?
Thats how the code is written usually for anonmous inner classes-wheher it comes for either class flavour or interface flavour.The same format is used in SCJP certification by Kathy Sierra
0

Java does not offer a functionallity like this. Maybe it is possible for you to override the function in a sub-class.

What you Showed above can be done in Ruby (i think).

Override

Pager pager = new Pager() {
    @Override
     public void doStuff() {
        doSomethingElseFunct();
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.