• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why Class implementation is not possible in java?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why Class implementation is not possible in java....
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean why class implements interface?
why can't class implements class?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ihsan Cingisiz, Welcome to Coderanch.
 
Ihsan Cingisiz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
My cellmate was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic