• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can an interface extend a class?  RSS feed

 
Pritam Chatterjee
Ranch Hand
Posts: 38
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can an interface extend a class?

When I am running the following code it's showing some errors..I have commented them.Please help.

class A
{
public void methodA()
{
System.out.println("Class A methodA");
}
}
interface B extends A //interface expected here
{
public void methodA();
}
interface C extends A ////interface expected here
{
public void methodA();
}
public class D implements B, C
{
public void methodC()
{
System.out.println("Class D methodD");
}
public void methodA()
{
System.out.println("Class D methodD");
}
public static void main(String args[])
{
D obj1= new D();
obj1.methodC();
obj1.methodA();
}
}
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You appear to have answered your own question. An interface cannot extend a class.
 
Pritam Chatterjee
Ranch Hand
Posts: 38
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tim, So there is no way to extend a class to an interface..
And the error is showing that..?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there is no way to do that.

Why are you trying to? What would it mean to have an Interface extend a Class?
 
Pritam Chatterjee
Ranch Hand
Posts: 38
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I saw this code in a website,I can't remember the name. Then when I tried to run this code I got the errors.So I wanted clear the doubt..
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The website seems to have made a mistake then.

You can actually achieve something similar in Java 8. If Class A was an Interface and methodA() was a default method then that would work.

 
Ihsan Cingisiz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Extending a class from an interface is not possible. What you actually can do is implementing other interfaces
within your interface. So the class that implements the interface that implements another interface will require
you to implement the methods from both the interfaces.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ihsan Cingisiz wrote:What you actually can do is implementing other interfaces within your interface.

No you can't. An interface can extend another interface, but it can't implement one. Only classes can implement interfaces.
I know this is probably what you meant, but attention to detail is important to avoid confusing people who are new to Java.
 
Ihsan Cingisiz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Ihsan Cingisiz wrote:What you actually can do is implementing other interfaces within your interface.

No you can't. An interface can extend another interface, but it can't implement one. Only classes can implement interfaces.
I know this is probably what you meant, but attention to detail is important to avoid confusing people who are new to Java.


That was indeed what I meant, I am sorry if I started a confusion.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pritam, the code you posted looks like what is online at this Web site. Which means that Mike is correct: the Web site has a mistake.

It's kind of a big one, too, since you can't just change A from being a class to an interface (unless you go on to make methodA() a default method, which wasn't even an option when that page was first created.





The Web page says, "The above code would work without any issues and that’s how we implemented hybrid inheritance in java using interfaces." But that's incorrect. It won't even compile. With as few changes as I can make, and only under Java 8 (or later, for you programmers reading this in the future), it will run in this form:



Output is:

Class D methodD
Class D methodD


Even that output is shockingly misleading, as there is no "methodD," and you could easily misunderstand that to mean the same method had been invoked twice, when that's not what happened.

I hate to dismiss someone's work so quickly, but I'd suggest you find a different Web site to learn from. That one needs some editing.
 
mark mcpadden
Greenhorn
Posts: 2
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiya new to the forum.

So I thought I would give my 50 cents worth. A handy way I think to remember is I for interface and I for implements.....
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mark mcpadden wrote:So I thought I would give my 50 cents worth. A handy way I think to remember is I for interface and I for implements.....

Very nice. And you know what? In 12 years of using Java, it's never occurred to me.

It therefore gets a cow ... providing you never recommend Hungarian notation.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!