If interface has got four methods,and I like to implement only two methods, how this could be achieved?

Questions by Roshini

Showing Answers 1 - 31 of 31 Answers

jenny

  • Aug 11th, 2006
 

I have an idea. But I am not sure if it is the right answer. Can I divide these 4 methods into 2 interface? Then I can only implement the wanted 2 methods.

  Was this answer useful?  Yes

ayman

  • Aug 13th, 2006
 

I can achiev this bu creating adapter class that implements all interface methods. like MouseLisetner interface has a MouseAdapter class

  Was this answer useful?  Yes

ravikiran

  • Aug 14th, 2006
 

hey,

  those are perfectly wrong answers,

 u do that implement class becomes abstract class.

  Was this answer useful?  Yes

Farrukh Ijaz

  • Aug 14th, 2006
 

Create an abstract class which should implement the specified interface. Provide implementation for only desired methods.

msarada

  • Aug 24th, 2006
 

u can implement only two methods and for the rest of the methods just open and close curly braces followed by the method name , for ex

public <return-type> method-name(args) { }

by specifying like this it is mean that u have implemented the method

  Was this answer useful?  Yes

kishor

  • Sep 11th, 2006
 

By implementing Adapter design pattern we can implement only two method's.

Same techinque used by SUN in servlet API.In servlet interface we r having 5 method's.But ,by implementing HTTPServlet or GenericServlet we r overriding only required method's in the servlet interface.

Thanks&Regards

kishor

  

  Was this answer useful?  Yes

jay

  • Sep 23rd, 2006
 

>hey,> those are perfectly wrong answers,> u do that implement class becomes abstract class.so is this answer - interface is an interface, abstract class is an abstract class, interface never becomes abstract class because it is a PURELY abstract class itself.Anyways, the idea of an interface is that it assures that ALL of it's functions WILL BE implemented in the class implementing it.Creating an interface of 4 methods just to implement only 2 of them is pointless (and creation of an adapter do not solve your problem because you have to EXTEND an adapter (you cant implement it because if you could, it would have to be an interface also and this way it cant possibly be an adapter) and this way you automatically cant extend any other class (as in java you can only extend one class)).For example, if you had an interface : interface A { void a1(); void a2(); void a3(); void a4();}and wanted to implement only a1 and a2 but not a3 and a4 (if it was possible) in a class B implements A, and later when you deploy your B.class and someone who didnt know implementation of B would like to use it (throught interface A - as is a common practice) HOW would he possibly know that a3 and a4 are not implemented in it ?Interface in this case says : "I wont tell you about B class implementation, but I can tell you that since it implements A - it has methods a1, a2, a3 and a4 and you can use any of them".That's at the core OO filosophy.Using Adapter class/pattern is not by any means close to Interface, cause they have quite different roles. Anyways, if you need to use your interface, why not implement all of the functions, and in those you don't want to use , do something like this (back to my example) : public class B implements A { void a1() { //do something } void a2() { //do something } /* unimplemented */ void a3() { throw new Exception("O-o! This method isnt implemented!"); } /* unimplemented */ void a4() { //do something throw new Exception("O-o! This method isnt implemented!"); }}and when you deploy your class, you use javadoc or any documentation of the class to state that a3 and a4 arent yet implemented.this is a widely supported way of doing what you want to do.

  Was this answer useful?  Yes

sairam

  • Jan 27th, 2007
 

it is not possible.try once.the every interfaces methods must implemented in the class where it is implements.it must includes all interfaces we have defined.

  Was this answer useful?  Yes

Rizwan

  • Feb 3rd, 2007
 

What Ayman and Kishore have told are perfectly valid answers. Rest sorry u dont know the concept of adapter class. If you guyz wanted to make sure please go and c adapter class. You will get benefited.This is a just suggestion not an advice guyz. Because for other persons who view this comments get confused . So no need to confuse and What AYMAN and KISHORE told are perfectly valid answers no doubt in it

  Was this answer useful?  Yes


      Actually, Interfaces having  Abstract Methods only , they wont support any concrete methods. So, Interfaces having all the methods must be implemented.

      So, if u want to do that similar action mean, u can do that with the help of abstract class.......

  Was this answer useful?  Yes

Here there are two approaches.....
(1) implement only those 2 methods in ur class & declare this as abstract
(2) those who are saying abt adapter classes, adapter class is themselve are abstract!!!

  Was this answer useful?  Yes

public interface MyInterface1 {

public void A();

public
void B();public void C();
public void D();
}

package

com.venkata.ust;

public class MyClass1 implements MyInterface1 { 

@Override
public
void A() {

// TODO Auto-generated method stub
System.out.println("Hi");}

@Override
public
void B() {

// TODO Auto-generated method stub
System.out.println("Hi");

}
@Override

public void C() {

// TODO Auto-generated method stub
}

public void D() {

// TODO Auto-generated method stub
}
}

public class MainClass {


/**
* @param args
*/

public static void main(String[] args) {

// TODO Auto-generated method stub
MyClass1 my = new MyClass1();
my.A();
my.B();
}
}

  Was this answer useful?  Yes

Rest of the two method to open it and close the braces.

EX:

interface in1{

                public void A();
                public void B();
                public void C();
                public void D();
}

Class MyClass implements in1
{
                public void A(){ dosome thing}
                public void B(){ dosome thing}
                public void C(){ }              
                 public void D(){}

}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions