-4

Im developing Android Application and somehow I should call "super" without super keyword inside method. Can I call super with instance of class? For example:

@Override 
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
}

How can I call super.onCreate(savedInstanceState) without using super keyword?

UPDATE 1 I tried this but this is not working too.

@Override   
public void onCreate(Bundle savedInstanceState) { 
    try {
        getClass().getSuperclass().getMethod("onCreate",Bundle.class).invoke(this,savedInstanceState);
    } 
    catch (IllegalAccessException e) {
        e.printStackTrace();
    } 
    catch (InvocationTargetException e) {
        e.printStackTrace();
    } 
    catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
}

Note: Please understand what I'm talking about. Don't say 'call super()'.

Metehan Toksoy
  • 1,885
  • 3
  • 22
  • 39
  • You can't, if I understand your question correctly. Why do you say that "somehow [you] should" do this? – CommonsWare Jan 08 '17 at 00:25
  • Im developing very unique application for my company. This is "somehow" :D I need to call it from instance or with reflection. Also I updated question for reflection. – Metehan Toksoy Jan 08 '17 at 00:27
  • Why would 5 extra method calls inside a `try` block with 3 `catch` statements be anywhere near as convenient as just calling `super.onCreate()`‽ – Michael Dodd Jan 08 '17 at 00:34
  • Thats the situation @MichaelDodd . I know what youre talking about Im not a beginner or something else. Please if you know just answer what I asked. – Metehan Toksoy Jan 08 '17 at 00:40
  • 3
    Reflection isn't going to work. The invoke function is going to invoke the onCreate method of the instance, which is the onCreate of the child class. Just use super. That's the only anwer you'll get until you actually explain PRECISELY why you need it. If you do that, maybe we can give better advice. – Gabe Sechan Jan 08 '17 at 00:47
  • I know just calling super @GabeSechan . Think this. I got class names, I got method and fields and I want to create fragment in runtime. did you ever use javassist? – Metehan Toksoy Jan 08 '17 at 00:53
  • 2
    We are *trying* to understand what you want, which is difficult because you haven't actually said anything where answer "use `super`" wouldn't be the correct answer, so let me try to re-phrase what you want, and you can say whether I got it right: You have an object reference, and you want to call a method declared in a superclass, using reflection, but the method has been overridden in the actual class of the object (or some in-between class), and you don't want the override to be called, only the specify superclass method. In short, you want to bypass dynamic dispatch. Is that it? – Andreas Jan 08 '17 at 00:59
  • 2
    @Andreas from his response to me, it looks like he wants to create a new Fragment class at runtime (not an instance, a new class). That means he needs to create methods on the fly which call super. Which seems like an unmaintainable mess to me, I can't think of a single good reason to ever do it. – Gabe Sechan Jan 08 '17 at 01:03
  • No I want to call super other ways just this. I want to make super call without using super keyword because I exposed one class and I create dynamic class from this class with javaassist. For example I create Fragment dynamically with class reference and I want to create object from it but also I want to override "onCreate" method. Inside on create I need to call super() for life cycle events of android but I cannot call it because I just got only class reference not class. – Metehan Toksoy Jan 08 '17 at 01:04
  • @MetehanToksoy So, you just want to use javaassist to create the same bytecode the compiler would for a `super` call? What's preventing you from doing that? – Andreas Jan 08 '17 at 01:06
  • that because Im not using this byte codes exacly natively. Its too complicated and I cant give you the exact reason to why Im doing this, because its not mine. – Metehan Toksoy Jan 08 '17 at 01:08
  • 2
    If your entire question is about how to dynamically create a class at runtime using Javassist, where one of the dynamically created methods must perform action similar to `super` call, don't you think that would have been **extremely** relevant information to put in the question? ** I'm gonna vote to close question as "**unclear what you are asking**" until you can edit the question to correctly state what you're trying to do. – Andreas Jan 08 '17 at 01:09
  • Why are you still want why I need to do this? Im asking just simple question? How can I call super class function without super keyword? Thats it. Its a simple question? This is clear and basic question. I know you think why he is asking this? I know but question is clear and basic @Andreas – Metehan Toksoy Jan 08 '17 at 01:17
  • @MetehanToksoy Ok, then, here is the simple answer. You can't: 1) Only way from plain Java is hardcoding a `super` call. 2) You can't do it using reflection. 3) Since question, as currently worded, is *not* about bytecode generation / dynamic class generation, that's your full answer: **You can't.** Happy? --- Now, if your question *had* been about dynamic code generation, the answer might be entirely different, but then, that would also be an *entirely different question*, wouldn't it? --- So, actually, please don't edit the question, as previously requested. *Create a new one.* – Andreas Jan 08 '17 at 01:33
  • And before you do create a new question, please read: **[How do I ask a good question?](http://stackoverflow.com/help/how-to-ask)** – Andreas Jan 08 '17 at 01:38
  • @Andreas thank you! Thats it. Im so happy right now :) of course If my question was "in dynamic code generation" your answers will be different. I know in dynamic code generation I can do it because super has some byte codes. – Metehan Toksoy Jan 08 '17 at 01:39
  • @Andreas I know how to ask question. There is no problem in my question as you know. As I told you and as you answered its a simple question I can or I cant? We are just talking dont try to sting. – Metehan Toksoy Jan 08 '17 at 01:42
  • 2
    @MetehanToksoy *FYI:* One of the reasons we kept asking you why, is that we suspected an [XY Problem](http://meta.stackexchange.com/q/66377). – Andreas Jan 08 '17 at 01:42
  • @Andreas thank you, again. Its not an XY. – Metehan Toksoy Jan 08 '17 at 01:44

2 Answers2

3

Not calling super methods is generally a very bad idea, especially when it comes to the activity lifecycle. In fact, an activity will crash without fail if the super method of an activity lifestyle event is not called.

By using @Override on a method, you are amending its functionality to do tasks you require, i.e. declare views. However, the parent class may need to do other important things that are of no concern to app-level developers i.e. set states or flags. If a method was designed to be completely implemented by an app-level developer without any parent code executed, it would be declared abstract or part of an interface instead.

Further reading:

Community
  • 1
  • 1
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
  • I know some classes needs super calls especially activities or life-cycle based classes. Its already crashing and I try to figure out the way to call super without super keyword. Also android sets so much flags inside this functions. – Metehan Toksoy Jan 08 '17 at 00:36
  • But I'm struggling to understand _why_ you want to do this in such a long-winded way. – Michael Dodd Jan 08 '17 at 00:38
  • Because I can't as I told you. Im developing very unique framework. This is why i need this. I know making super is better than some workarounds. I know calling super is better than reflection or something else. – Metehan Toksoy Jan 08 '17 at 00:42
  • Well, the only other method I can think of off the top of my head would be using reflection. Cast `this` as an instance of the parent class then invoke `onCreate`. No idea if it'll work though. See http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful – Michael Dodd Jan 08 '17 at 00:48
  • 1
    @MichaelDodd won't work. Just because you've cast it to the parent doesn't change the type of the instance. It would still call the child version. In fact that's desired behavior, that's why you can call View.onDraw and it calls the correct onDraw function. – Gabe Sechan Jan 08 '17 at 00:49
  • Disregard that. Reflection won't work according to [Gabe Sechan](http://stackoverflow.com/users/1631193/gabe-sechan) (See comments above) – Michael Dodd Jan 08 '17 at 00:49
  • Also @MichaelDodd I tried to cast it to Fragment and try to cal this method in it. not worked because of what Gabe Sechan told. – Metehan Toksoy Jan 08 '17 at 00:56
  • Also not sure why this answer has been downvoted when it's been consistent with all other comments left on this question. – Michael Dodd Jan 08 '17 at 00:57
  • Because it not even an answer. I told you in my comment I know all this stuffs. life cycles, just using super etc. – Metehan Toksoy Jan 08 '17 at 01:06
  • As Andreas mentioned above, from the start you've not been very clear about _why_ you wanted to do this, or at the very least not clear enough to justify why `super` should not be used. Truth be told, I still can't think of any reasons why this would be a good idea. – Michael Dodd Jan 08 '17 at 01:11
  • @MichaelDodd as I told you. I just need this. Its a simple. Dont be nerd. I not asking for your opinion that "Should I call super or not" ? Please guys this is not your issue, this is mine. And Im asking you to how can I call super method without super keyword? It is just that simple. – Metehan Toksoy Jan 08 '17 at 01:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132618/discussion-between-michael-dodd-and-metehan-toksoy). – Michael Dodd Jan 08 '17 at 12:16
  • After one year, I checked the question again and I remembered that I forgot the give a feedback. The reason that I asked this question is a runtime class generation and use proxy on the javascript side using V8. Its a complicated question but I solved this in a different way. – Metehan Toksoy Apr 18 '18 at 04:05
-1
public class A {
    static void m1() {
        System.out.println("m1 P");
    }
}
class B extends A {
    static void m1() {
        System.out.println("m1 C");
    }
}
class Test {
    public static void main(String[] args) {
        A a = new B();
        A.m1();
    }
}
Metehan Toksoy
  • 1,885
  • 3
  • 22
  • 39