• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Class implementation is not possible in java?  RSS feed

 
Vagdevi Malla
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why Class implementation is not possible in java....
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Coderanch, Vagdevi Malla.

Could you provide some more details like what you mean by Class implementation?
 
Vagdevi Malla
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean why class implements interface?
why can't class implements class?
 
Ihsan Cingisiz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, it is called "Inheritance". Implements is used to implement interfaces, while extends is used to
extend an existing class.

When you implement interfaces in a specific class, you are required to override the methods inside the
interface you have implemented in your existing class. The main reason why one uses interfaces has also
to do with multiple implementations while you only can extend just one class.
And, when you extend an existing class, you can use the methods inside of it and more (see link below).

Please take a look at: Oracle Inheritance

I hope that will make it a bit clear for you. you should read the Oracle Documentation on Java OO. These
are the basic things that you actually in my opinion have to know if you are a programmer.

- Ihsan.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vagdevi Malla wrote:
I mean why class implements interface?
why can't class implements class?


Simple answer - that is by design.

Since concrete classes have methods that are already implemented ( by implemented I mean all the methods already have the method body ), there is no need to 'implement' them.

So that means you can at best provide alternate implementations. For that classes can be sub-classed and methods can be overridden.

Remember Java does not support multiple inheritance. Do you know why?


 
Chan Ag
Rancher
Posts: 1090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ihsan Cingisiz, Welcome to Coderanch.
 
Ihsan Cingisiz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chan Ag wrote:Ihsan Cingisiz, Welcome to Coderanch.


Thank you, Chan!
 
Vagdevi Malla
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chan Ag wrote:

I mean why class implements interface?
why can't class implements class?


Simple answer - that is by design.


Since concrete classes have methods that are already implemented ( by implemented I mean all the methods already have the method body ), there is no need to 'implement' them.

So that means you can at best provide alternate implementations. For that classes can be sub-classed and methods can be overridden.

Remember Java does not support multiple inheritance. Do you know why?

ya i know the reason...thanks for valuable information
 
Joren Uitzetter
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ihsan Cingisiz wrote:Hi, it is called "Inheritance". Implements is used to implement interfaces, while extends is used to
extend an existing class.

When you implement interfaces in a specific class, you are required to override the methods inside the
interface you have implemented in your existing class. The main reason why one uses interfaces has also
to do with multiple implementations while you only can extend just one class.
And, when you extend an existing class, you can use the methods inside of it and more (see link below).

Please take a look at: Oracle Inheritance

I hope that will make it a bit clear for you. You should read the Oracle Documentation on Java OO. These
are the basic things that you actually in my opinion have to know if you are a programmer.

- Ihsan.


This information is correct, however, methods of implemented interfaces are not overridden since they have no body to start with, you're simply providing an implementation for these methods, not overridde them.

But I'm sure you know that he meant that.
 
Ihsan Cingisiz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for correcting, that is true indeed, my bad. Methods that are in the class that you are "extending" CAN be overridden
if you would like to though.

- Ihsan.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Vagdevi, Ihsan and Joren!

methods of implemented interfaces are not overridden since they have no body to start with, you're simply providing an implementation for these methods, not overridde them.

Note that the recently released Java 8 changes this, though. Interfaces can now have default methods that *do* have body. "Overriding" might not be the best word to use if that default method is replaced, though, since that is associated with inheritance. But it is something like that.

But this being the "Beginning Java" forum, you should probably forget I mentioned interface default methods. Your tutorial likely doesn't talk about those anyway
 
Surinder Mehra
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more reason i see why Java class cannot implement another class is Multiple inheritance. What if class has to implement multiple API's provided by different vendors. Only Interfaces can help here since a java class can implement multiple interfaces but cannot implement/extend multiple classes
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a java class can implement multiple interfaces but cannot implement/extend multiple classes

Extend is the right word to use. Implement is used with interfaces. One should not mix these in order to avoid confusion.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!