21

Can i use the implements and the extends at the same time? Because I need to inherit something on the other class while i used implements on the same class.

public class DetailActivity extends AppCompatActivity
implementsView.OnClickListener "extends ListActivity" 

How can it be like that?

Raktim Biswas
  • 3,891
  • 5
  • 25
  • 31
Jeyjey
  • 281
  • 1
  • 2
  • 10
  • Yes. you can happily do it. – Mehraj Malik Jul 11 '16 at 09:02
  • Can I inherit multiple classes? @MehrajMalik – Jeyjey Jul 11 '16 at 09:07
  • Read this http://www.programmerinterview.com/index.php/java-questions/multiple-inheritance/ – Raghavendra Jul 11 '16 at 09:10
  • @Jeyjey Multiple inheritance is not allowed in java. read this question : http://stackoverflow.com/questions/6587621/can-one-class-extend-two-classes – Mehraj Malik Jul 11 '16 at 09:11
  • This is not a duplicate of the linked question. This isn't asking what they are defined as, it's specifically asking if you can do **both at the same time**. Which was my question and the linked answer did not answer that or give an example. Below Bathsheba does. – Chris Farr May 12 '18 at 20:01

3 Answers3

48

Yes, you can. But you need to declare extends before implements:

public class DetailActivity extends AppCompatActivity implements Interface1, Interface2 {
 // ...
}

Any number of interfaces can be implemented, if more than one then each needs to be separated with a comma.

emrcftci
  • 2,954
  • 3
  • 17
  • 34
Bathsheba
  • 227,678
  • 33
  • 352
  • 470
13

You can only extend one class but you implements multiple interfaces as your need.

Sohail Zahid
  • 8,098
  • 2
  • 23
  • 39
1
class Human extends Monkey implements BasicAnimals, xyz

in this way you can extend a class as well as implement interfaces.