Mufrah
Mufrah

Reputation: 534

How a class can extend with two different classes While one class is from a external library and other is built in

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 .

Upvotes: 0

Views: 573

Answers (4)

Mufrah
Mufrah

Reputation: 534

I have solved this issue by extending my class with SherlockFragmentActivity

public class AllAppsInfo extends SherlockFragmentActivity{}

Upvotes: 1

Azodious
Azodious

Reputation: 13882

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();
    }
}

Upvotes: 3

logoff
logoff

Reputation: 3443

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

Upvotes: 0

Vijay Shanker Dubey
Vijay Shanker Dubey

Reputation: 4418

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.

Upvotes: 1

Related Questions