2

Can you have a class which implements an interface, and choose whether to use the methods in the interface during instantiation of this class? Therefore having object A which uses the interface and object B which does not use it.

Thanks

Updated:

Assuming you have a Professor class and this class implements an interface called Employer, which has employ(rAssist x) abstract method.

Now I want to instantiated 2 objects from the Professor class implementing this interface Object A - Professor can employ a research assistant and Object B - Professor cannot employ research assistants.

2
  • I think You need to clarify your question? What Do you want to say? Commented Jul 30, 2012 at 5:22
  • The question doesn't make sense. The interface methods are only declarations. You can't 'use' them in any way. Commented Jul 30, 2012 at 22:26

5 Answers 5

4

Can you have a class which implements an interface, and choose whether to use the methods in the interface during instantiation of this class?

No, if class C implements the interface, then all instances of C will provide the methods declared in the interface.

What you can do is something like

class MyClass implements MyInterface { @Override void interfaceMethod() { System.out.println("Interface method"); } } 

and then do

MyClass x = new MyClass(); MyClass y = new MyClass() { @Override void interfaceMethod() { throw new UnsupportedOperationException(); } }; 

In effect, x supports the use of interfaceMethod while y does not. Note however that...

  • The usage of y.interfaceMethod is not prevented at compile-time, i.e. it will not be enforced by the type system.

  • With this solution, you are in fact creating an (anonymous) subclass of MyClass and assigning an instance of it to y.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok I understand. Now from a coder's point of view is this a neat solution? I am asking cause I need to understand this entirely.
Naah, you would typically want to program in a way such that errors are caught at compile-time, not at run-time.
1

Do you mean you want class A and Class B to implement a common Interface but you dont want to implement all methods in Class B?

An Interface in simple terms means it is sort of a contract and all the classes which implement it should follow that contract.So if you want Class B to implement the interface , Class B should also follow the same contract. But if you dont want to implement any methos you can always do this.

class ISampleInterface { void sampleMethod(); void optionalMethod(); } Class A implements ISampleInterface { void sampleMethod() { //Your Implementation } void optionalMethod() { //Your Implementation } } class B implements ISampleInterface { void sampleMethod() { //Your Implementation } void optionalMethod() { throw new UnsupportedMethodException(); } } 

Comments

0

No, that's not the point of an Interface.

An Interface is contract that guarantees that implementations WILL implement it's signature

4 Comments

I believe that doesn't address the question.
@Habib Hay, I'm half a sleep, so I'd appreciate if you could help and explain what I missed :P
see the answer of aioobe, What you are saying is perfectly right with respect to interfaces, but OP is asking about objects of a same class using methods from interface based on some condition
@Habib Ah, okay, obviously need more sleep, thanks for taking the time to point it out, much appreciated :) - Yes, I thought aioode's answer was much better as well
0

The idea of interface is to establish a obligation for the class that implements the interface. If your's is a requirement, you can use the java.lang.reflect.Method reflection class to change the visibility of the method at runtime. However, this is not a clean way.

Comments

0

1. Interfaces were introduced in Java because Multiple Inheritance was not allowed in Java.

2. But as far as Design Pattern are concerned, following are the uses..

- To implement certain Roles.

Consider Dog a Super class, but then Pet dog and Wild dog can be interfaces, which can be implemented by the Sub Classes of Dog class.

- Used when Behaviors keeps changing.

Consider you have a Class Drawing, and paint method() in it, now paint can be stroking, shading, etc... You must Encapsulate such behaviors in an Interface or an Abstract class.

1 Comment

Thanks for the examples...helpful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.