0

I am using SherlockActionBar library in my project.And my class is extending an abstract class SherlockActivity

public class AllAppsInfo extends SherlockActivity{}

In mean while i want to extend my AllAppsInfo with FragmentActivity as well.Which is not allowed due to negligence of multiple inheritance in java.

I have done some search, composition is the solution but how to use it in this Scenario? Neither i can make abstract Sherlock class a interface nor same be done with Fragment Activity class .

Mufrah
  • 534
  • 7
  • 22
  • Confused. You are trying to do multiple inheritance, but say you realize that Java doesn't do multiple inheritance. You can't do multiple inheritance in Java. Period. You can do encapsulation instead. – John B Oct 19 '12 at 11:17
  • You cannot do multiple inheritance of implementation in Java; you can only extend one class at a time. But you can do multiple implementation of interfaces. – duffymo Oct 19 '12 at 11:28

4 Answers4

3

Try Composition.

You can refer here too.

The funda is simple:

class A
{
    void aMethod() {}
}

class B
{
    void bMethod() {}
}

class C
{
    void cMethod() {}
}

if you want to call aMethod() and bMethod() from cMethod(); you can do following way:

class C extends A    // Inheritance
{
    B aBObject = null;    // Composition
    void cMethod()
    {
        aMethod();
        aBObject = new B();
        aBObject.bMethod();
    }
}
Community
  • 1
  • 1
Azodious
  • 13,752
  • 1
  • 36
  • 71
1

Yes, In Java we do not have multiple inheritance.

You have two options ahead of you.

  1. use composite pattern. described here
  2. Recreate your class hierarchy, by adding few more interfaces and base classes.
Vijay Shanker Dubey
  • 4,308
  • 6
  • 32
  • 49
1

I have solved this issue by extending my class with SherlockFragmentActivity

public class AllAppsInfo extends SherlockFragmentActivity{}
Mufrah
  • 534
  • 7
  • 22
0

Java has not full multiple inheritance, C++ has. You need workarounds to do it like composition

logoff
  • 3,347
  • 5
  • 41
  • 58