0

Given two classes, one subclasses the other, both out of my scope, so I don't have any influence on them, eg.

public class Bla {
}

public class SubBla extends Bla {
}

How can I create a customized subclass of Bla and SubBla without repeating code or limit SubBla's special functionalities?

Edit: So I don't have to:

public class MyBla extends Bla {
    public void someOverriddenMethodOfBla()
    {
        super.someOverriddenMethodOfBla();
        // do something custom
    }
}

public class MySubBla extends SubBla {

    // repeated code: this should do the same as MyBla's customized method
    public void someOverriddenMethodOfBla()
    {
        super.someOverriddenMethodOfBla();
        // do the same custom stuff as in MyBla's method
    }

}

Edit 2, to clear things up a litte:

Let's say I need the android classes Activity and ListActivity, which is a subclass of Activity. Now I want to add the same customization in the onCreate method to both. How do I acomplish this without repeating code?

Thanks!

user973224
  • 203
  • 2
  • 13
  • The purpose of subclassing is reuse..so that you dont repeat code (however, you can override/overload) ...can you elaborate a bit more? – Vikram Apr 22 '13 at 20:19
  • you dont require `MyBla` class at all. If `B extends A` then if you subclass `B` by `C extends B`...`C **IS** A also!` you dont require `C extends A` and again `D extends B`. So in your example all you require is `MySubla extends Subbla` – Vikram Apr 22 '13 at 20:29
  • No, I also need MyBla directly. That's the problem ;) – user973224 Apr 22 '13 at 20:32
  • Can you provide a concrete example what you cannot do with MySubBla that you can only do with MyBlah? – Vikram Apr 22 '13 at 20:38
  • SubBla overrides other methods of Bla, which do a completely different thing. So I can't use MySubBla in cases where I need the special functionallities of MyBla and vice versa – user973224 Apr 22 '13 at 20:47

3 Answers3

1

If I understand you correctly, you're trying to bypass SubBla's implementation, and invoke Bla's implementation directly from a subclass of SubBla. What you're trying to do is essentially call super.super, which cannot be done in Java

There's a good explanation for this here.

Community
  • 1
  • 1
0

Actually the purpose of inheritance is that you reuse code. In java there only exists public inheritance which means that the accessibility of the methods will remain the same trough inheritance.

If you do MyBla extends Bla then all public methods from Bla will remain still visible in the MyBla class.

emd
  • 740
  • 4
  • 12
0

Considering your example for Android Activity and ListActivity, subclass ListActivity as CustomListActivity. In CustomListActivity add your version of onCreate(). Now instead of using Activity or ListActivity you use CustomListActivity in your application.

Vikram
  • 4,162
  • 8
  • 43
  • 65
  • But I also need to use the customized normal activity, how to do that? – user973224 Apr 22 '13 at 21:54
  • I am sorry I have no idea about Android API...I dont think I understand your comment.. Let me give a shot: So you have `Activity`, `ListActivity` and `CustomListActivity`. You instantiate the one that is appropriate for example: if you have a Class `A` that can do with `onCreate()` from `Activity`, instantiate `Activity activity = new Activity();` If you want Class `B` that requires to use your custom `onCreate()` you instantiate `Activity customListActivity = new CustomListActivity();` and use this instance. – Vikram Apr 22 '13 at 22:07
  • No, I need Activity and ListActivity, both with customized onCreate for example – user973224 Apr 22 '13 at 22:17
  • 1
    You probably might want to take a look at this: http://stackoverflow.com/questions/1419758/inject-new-methods-and-properties-into-classes-during-runtime – Vikram Apr 23 '13 at 14:18
  • IMHO you can do it but Java API(version 6) does not provide out-of-the-box solution for this. – Vikram Apr 23 '13 at 14:25
  • now you got me ;) thx, but that's too much of a hack. i'll stick to the repeated code thing until i find something neater – user973224 Apr 23 '13 at 20:26