• Post Reply Bookmark Topic Watch Topic
  • New Topic

partial Interface implemention  RSS feed

 
Pritam Chatterjee
Ranch Hand
Posts: 38
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one interface with three(more than one) method declaration.
In the subclass that implements it I want to define only one method not all three not even blank definition of them.Is there any keyword or method for that.
How to do it? Is it possible to do it?

In GUI we use adapter classes to achieve it. What for console application?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pritam Chatterjee wrote:...
In the subclass that implements it I want to define only one method not all three not even blank definition of them.Is there any keyword or method for that.
How to do it? Is it possible to do it?
In GUI we use adapter classes to achieve it. What for console application?

Adapter classed do provide implementation for the interface, albeit it is empty.

So the answer is no. You can either
1) Make your subclass an adapter
2) Declare it abstract
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say "class A implements B", you are basically signing a contract that says "I promise that my class A will have EVERY SINGLE METHOD DEFINED by the interface B".

So basically, you are saying "Can I promise the compiler I will do something, then not actually do it.".

The answer is no - the compiler will complain, and rightfully so.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pritam Chatterjee wrote:In the subclass that implements it I want to define only one method not all three not even blank definition of them.Is there any keyword or method for that.
How to do it? Is it possible to do it?

I'm not quite sure that "adapter" is the right term here. What you want sounds much more to me like a "skeleton implementation".

And there are tons of examples in the Java Collections Framework, most of which start with the prefix "Abstract" (eg, AbstractCollection, AbstractList).

HIH

Winston
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:I'm not quite sure that "adapter" is the right term here. What you want sounds much more to me like a "skeleton implementation".

Pritam got the term "adapter" from Java's GUI framework - it contains interfaces such as MouseListener, and abstract classes such as MouseAdapter that implements MouseListener with empty implementations (methods that do nothing). The idea is that if you are interested only in certain mouse events, you subclass MouseAdapter instead of implement MouseListener, and then you only have to supply your own implementation of the methods that you're actually interested in.

There's no special keyword for this pattern. If you want to do something similar, you'll have to create your own adapter class in a similar way.

It's however debatable if this is good design - you could argue that it defeats the purpose of interfaces. Maybe it's better to split up your interface into multiple interfaces, each with fewer methods, and then make your classes implement only those interfaces for which the class can provide an implementation.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!