• 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

Interface

 
Ranch Hand
Posts: 107
MyEclipse IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Q. From Interface can I use only one method in sub-class and I don't want to declare subclass as a abstract.. Is it possible if yes then how ? please help me out...


 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you want to declare it abstract?

The only reason not to declare it abstract is if you want to be able to instantiate it. But in that case you need to implement every method in the interface - what happens if someone calls that method, and there's no body? So no, it's not possible.

If you have a genuine case where it makes sense to implement one method and not the other, then that means your interface is badly designed, and it should be split so the methods are in more than one interface. That way you can implement only the ones that make sense.
 
Prakash Rai
Ranch Hand
Posts: 107
MyEclipse IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matthew Brown]Why don't you want to declare it abstract?

The only reason not to declare it abstract is if you want to be able to instantiate it. But in that case you need to implement every method in the interface - what happens if someone calls that method, and there's no body? So no, it's not possible.


If you have a genuine case where it makes sense to implement one method and not the other, then that means your interface is badly designed, and it should be split so the methods are in more than one interface. That way you can implement only the ones that make sense.





I did't understand ...
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prakash Rai wrote:I did't understand ...


What didn't you understand?

Better still, back up and explain why you only want to implement one method.

Winston
 
Prakash Rai
Ranch Hand
Posts: 107
MyEclipse IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have any such type of requirement but its has been asked in interview... I have asked the same question..
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prakash Rai wrote:I don't have any such type of requirement but its has been asked in interview... I have asked the same question..


In which case the answer is 'yes'.

Simply implement all 3 methods, and have the 2 that you don't want either:
(a) do nothing (not recommended).
(b) throw UnsupportedOperationException if they're called.

Not a very good question, mind you - although a lot of interview questions are kinda stupid if you ask me.

Winston
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prakash Rai wrote:[
Q. From Interface can I use only one method in sub-class and I don't want to declare subclass as a abstract.. Is it possible if yes then how ? please help me out...



Note that using the interface's methods has nothing to do with it. It's perfectly fine for no class to ever use any of the methods. What matters is whether you implement the methods. Using a method is completely different from implementing it. Make sure you understand the difference.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:Not a very good question, mind you - although a lot of interview questions are kinda stupid if you ask me.


I actually think it's pretty good. The perceptive interviewer could learn a lot about the candidate's maturity as a developer from a question like this.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prakash Rai wrote:


If you have a genuine case where it makes sense to implement one method and not the other, then that means your interface is badly designed, and it should be split so the methods are in more than one interface. That way you can implement only the ones that make sense.


I did't understand ...



Well, I was trying to understand why you might want to do this. In cases where you might want to, it might well be a problem with your design. Here's a very artificial example to try and explain what I meant.

Let's say you were modelling cars, and you had an interface to define some of the behaviour.

So you've got a few concrete classes like this:

Now let's say we wanted to introduce an electric car. We've got a CarLike interface - great, let's use that! Oh, but the fillWithPetrol method doesn't make sense. So we want to implement CarLike without implementing that method.

You could use Winston's suggestion, and have fillWithPetrol throw an exception. But that doesn't stop people trying to fill it with petrol. It just stops them succeeding. A better design is to segregate the interface.

And the problem is solved! By changing our interface design we get the effect we want without having to worry about not implementing abstract methods.
 
Prakash Rai
Ranch Hand
Posts: 107
MyEclipse IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Matthew Brown Its really nice Example ....I was thinking my Question is very good like one of ranchers said. But after seeing your example its really nice example .. Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic