27

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
  • 4,011
  • 5
  • 27
  • 32
Jeyjey
  • 341
  • 1
  • 4
  • 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

4 Answers4

61

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
  • 3,355
  • 3
  • 21
  • 35
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
14

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

Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
2
class Human extends Monkey implements BasicAnimals, xyz

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

0

Yes, you can able to declare extends and implements together. But one condition you have to declare extends before implements keyword.

example

public class Dog extends Animal implements Interface1,Interface2
{
         //you should implement all methods here...
}