• Post Reply Bookmark Topic Watch Topic
  • New Topic

interface and abstract class  RSS feed

 
tong yuxin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interface just like 100% of abstract class.
So, i just confused why abstract class cannot extends interface?

what i think:
when abstract extends interface, the abstract class got all abstract methods from interface, and abstract became the interface but without "intreface" keywords.
why ? abstract class cannot extend interface and why?
 
Baseet Ahmed
Ranch Hand
Posts: 225
Java Notepad Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interface just like 100% of abstract class.
True.
when abstract extends(actually implements) interface, the abstract class got all abstract methods from interface, and abstract became the interface but without "intreface" keywords. Agree.

abstract class cannot extend interface and why?
Who said,you cannot extend it ,you can extend with the help of "implements" keyword.

Regards
Baseet Ahmed
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can see many examples of this in the Java Language itself. For example, look at the AbstreactList class, its an Abstract class that implements the List interface (along with a couple of others). Then in turn, the ArrayList class extends (that is a subclass of) the AbstractList class. The Vector and AbstractSequentialList classes also extend AbstractList.

This is a very common design paradigm; and a very powerful one. You have an Interface that defines the interaction contract, an Abstract class that provides some default implementations, or common implementations of some of the methods defined in the interface. The abstract class may leave some of the methods defined in the interface unimplemented. Then one or more concrete classes that extend the abstract class and provide more specific behavior. Those concrete classes may use the "default" implementations defined in the Abstract class, or they may override them for something more specific. And they must also implement any methods left as abstract in the abstract class.

Of course, an abstract class can be created without implementing an interface.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Baseet Ahmed
Who said,you cannot extend it ,you can extend with the help of "implements" keyword.


No its not true, there is a vast difference between implements and extends, atleast from the OOP perspective.
Only an interface can extend another.This inheritance is not possible with a class whether abstract or concrete.

Hope this helps
 
Mandar Khire
Ranch Hand
Posts: 582
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try to give answer, if mistake then tell me.
First you should try to understand difference between Interface & abstract classes with reference to there features, Read following comparisons( as i am lazy to type full answer here)


Then you should try to understand comparison between keyword implements & extends

The keyword Extends is used in Inheritance concept.In simple words if Class B extends Class A then the class B inherits some methods and variables from Class A which it can use as its own. Then Class B is said to be sub-class of Class A. Class A is said to be super class of Class B.

The keyword Implements is used in Interfaces concept.If Class B implements Interface A then Class B should implement the methods which are declared in Interface A. In other words Class B declares itself to be having the properties of Interface A.
There big difference is that a class can carry implementation whereas an interface cannot, and that you can extend only one class but as many implement interfaces as you like.Further, from a typing perspective there's no difference between a class and an interface. You can declare variables of both.
So a class can inherit as many super types it likes but only one can carry implementation. That's the core of the Java inheritance model.
read following links
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from a typing perspective there's no difference between a class and an interface. You can declare variables of both.

There is a lot of difference and I dont understand what do you mean by typing perspective?
Can you have private or protected variables in an interface?
What about non final variables?
I forgot to mention InterfaceVsAbstractClass in my last post.


Hope this helps
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!