aspose file tools*
The moose likes Java in General and the fly likes Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interface" Watch "Interface" New topic
Author

Interface

Prakash Rai
Ranch Hand

Joined: Jan 10, 2011
Posts: 106



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...


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Jan 10, 2011
Posts: 106

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 ...
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

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

Joined: Jan 10, 2011
Posts: 106

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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Jan 10, 2011
Posts: 106

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface