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.

ibpetr
  • 41
  • 3
  • What exactly you want to do ? Also this is not valid syntax in java – Kick Mar 14 '14 at 06:06
  • 1
    Javascript will let you do something like this. But what exactly is your aim here? – Rahul Mar 14 '14 at 06:06
  • I want to replace the function of a class while the program is running with any other function I choose. @Youngistan is there a valid syntax for this kind of thing? edit: I also want to do this without having to extend the original class. – ibpetr Mar 14 '14 at 06:08
  • 2
    Think the question is about function pointers in java. Have a look at this http://stackoverflow.com/questions/1073358/function-pointers-in-java – Kishore Mar 14 '14 at 06:10
  • No this is not a valid syntax at all and you cant assign one function to another function like this. – Kick Mar 14 '14 at 06:12
  • 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" – morgano Mar 14 '14 at 06:13
  • 1
    "is there a language that does allow it?" -- answer-- Javascript allows this. – Manu Viswam Mar 14 '14 at 06:24
  • @NandakishoreK It is somewhat if you take into account what can be done with C. That's a very useful link tho. – ibpetr Mar 14 '14 at 06:30

3 Answers3

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.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
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

Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
  • AbhinavKumar and @SubhrajyotiMajumder have the same code... may I know why? – morgano Mar 14 '14 at 06:15
  • 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 – Kumar Abhinav Mar 14 '14 at 06:21
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();
    }
};
NeoP5
  • 611
  • 7
  • 19