0

I am trying to add https://github.com/mdg-iitr/Swipper to my video player

So I added

public class VideoPlayerActivity extends AppCompatActivity, Swipper {

//Code

I get an error since java cannot extend with multiple classes, So what is the solution?

Since as per Swipper documentation the only way to implement Swipper is by public class MainActivity extends Swipper{}

However, I need to import AppCompatActivity as well.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Stacker
  • 23
  • 7
  • 2
    Does this answer your question? [Since a class in Java cannot extend multiple classes. How would I be able to get by this?](https://stackoverflow.com/questions/12518972/since-a-class-in-java-cannot-extend-multiple-classes-how-would-i-be-able-to-get) – Akash Jain May 25 '20 at 15:53

1 Answers1

1

Extending classes is a tricky question in java. One way to do it, it will improve code segregation, is to create another class B that extends Swipper. There, you will create the public methods you need to interact with it. In VideoPlayerActivity class you create an instance of B, or pass it as an argument. The VideoPlayerActivity only interacts with class B , so there is no need for VideoPlayerActivity inheriting from Swipper. That way VideoPlayerActivity can extend AppCompatActivity


class B extends Swipper{
 int a,b;

 //override swipper functions
 @Override
 public void play(){

 }
}

class VideoPlayerActivity extends AppCompatActivity{
 B instanceB = new B();

 public void play(){
  instanceB.play();
 }

 //instead of calling directly, since this class does not extends swipper, you call that function of class B, that extenda Swipper
 public void inheritedFunction(){
  instanceB.inheritedFunction()
 }

}