0

I want to extend 2 classes but I don't know how, I know it is very silly question but I need it.

I want to do so, but it is impossible. In which way I can do this ?

Public class Mclass extends Fragment extends extends ListActivity
{

}

Can anyone help me ? Thanks

Bosko Mijin
  • 3,287
  • 3
  • 32
  • 45
  • Check this out: http://stackoverflow.com/a/5836735/3377857 – Klemens Morbe Mar 10 '14 at 15:49
  • 2
    In Java, you can just extend one class. But you can implement as many interfaces as you want. Why you would like to extend 2 classes? Please, provide some context. – Christian Tapia Mar 10 '14 at 15:50
  • yeah I read it before But I can't understand what it means Could you explain it simply way? please – user3371107 Mar 10 '14 at 15:52
  • android I have 2 tabs and each tabs extends fragments and I also want in one tab make listView for it I extends ListAvtivity but it is not possible cause I extends aldready this class to Fragment – user3371107 Mar 10 '14 at 15:54

4 Answers4

6

Simple answer is that you cannot. Java doesn't support multiple inheritance of implementation.

You can however create a class which implements the interfaces of both your two classes (or inherits one and implement the interface of the other one) and delegate your calls to an instance you keep a reference to.

Fredrik
  • 5,759
  • 2
  • 26
  • 32
0

Java doesn't support multiple inheritance but multilevel inheritance. You can extend Class A in Class B and you can extend Class B in Class C. This can be the way to inherit both Class A and Class B in Class C. However, you can implement multiple interfaces.

Naddy
  • 2,664
  • 6
  • 25
  • 38
0

You can't extend from two classes, but you can create a class called "FragmentListActivity", that class would have the content of the ListActivity class and just change the extends from Activity to FragmentActivity, this would give you a fragment manager, the support library if you need to support older versions and the ListActivity functionality.

This is the content of the ListActivity: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/ListActivity.java

And when you create your activities, they will extend from this new FragmentListActivity.

rastadrian
  • 1,025
  • 1
  • 10
  • 17
0

Depending on what you need really there could be a simple solution. You cannot extend from more than one class in java, but if you needn't your class have both types(the types of both classes) and you just need their functionality, the best way to get it is to create a variable that instance one of the two types and extends the other one.

public class MyClass extends FirstClass{

SecondClass variable;

public MyClass(){

variable = new SecondClass();
}

If you need to use both classes as if you where inside of them because you want to access to private variables or methods, create another class that only extends from one of them and create other functions that allow you to use that private functionality. Then you can instanciate that class instead of the class you wanted to extend from.

Tony_craft
  • 213
  • 1
  • 10