if we cast an object to an interface, won\'t this object be able to call its own methods? in the following example, myObj
will only be able to call MyInterface meth
Only the methods in the interface are visible, but all methods in the object may still be invoked, as long as they're made accessible otherwise. For example:
public interface MyInterface {
String getName();
}
public class Obj implements MyInterface {
private String getFirstName() {
return "John";
}
private String getLastName() {
return "Doe";
}
@Override
public String getName() {
return getFirstName() + " " + getLastName();
}
}
public class Sec implements MyInterface {
private String getDate() {
return new Date().toString();
}
@Override
public String getName() {
return getDate();
}
}
In the above cases, both Obj
and Sec
can call their private members, even though they will not be visible in other classes. So when you say...
MyInterface myObj = new Obj();
MyInterface mySec = new Sec();
...as you've asked in the question, while it is true that in myObj
and mySec
the only method visible is getName()
, their underlying implementation can be different.
A simple analogy:
Consider a Chef(Interface). And there are Chinese chef(classes) and American chef(classes). Now, there is a Hotel looking out to recruit a chef based on some eligibility criteria such as
Here, the eligibility is the context within which the Chef type person is evaluated(Types functionality). Now, there may be cases where Chinese chef can also cook sea food , Italian dishes etc. so could be the case with American counterpart. All these may seem irrelevant to Hotel manager as he is only concerned with required criteria.
Chefs work in a hotel is analogous to Chef type object utilised in code. The obvious work would be to cook vegeterian and non-vegeterian food(criteria or interface methods). That does not mean that the Hotel cannot utilize the Chinese chef's sea food making skills.
It's just a matter of specifying what skills(functionality) the Hotel is looking for.
Hope you get the idea.
MyInterface mi = new Obj();
mi.getInterfaceMethods(); (methods in the MyInterface)
if(mi instanceof Obj) {
mi.getObjMethods(); (methods in the Obj)
}
In the code you showed you never perform any casts.
A typical use of an interface is to define some methods which will be available, without caring about the concrete implementation.
A nice example of this are the listeners in the standard JDK. For example the PropertyChangeListener
. This is an interface which defines a method which can be called when 'a property is changed'. A typical use of this interface is to attach it to a class which has properties, and this class will warn those listeners when one of its properties has changed.
The class does not care what those listeners will do. It only relies on the fact this propertyChange
method will be present, and calls that method.
The class can only call this method on the listener, since it only knows about the method defined in the interface. If those listeners have other methods, they can call those methods themself, but that is only because they know they are more then the interface.
MyInterface myObj = new Obj();
MyInterface mySec = new Sec();
For this to be legal, both Obj
and Sec
will have to be implementers of MyInterface
. The difference between these two objects would be how they provide that implementation. Obj
and Sec
could do two very different or very similar things, but their commonality is that they would adhere to a contract that you could rely upon. Consider you have a method
public void doSomethingWith(MyInterface thing) {
thing.frob();
}
Each object, myObj
and mySec
, could be passed into this method, and this method could then use that object's frob
method (assuming frob is part of the interface declaration). This is liberating. This allows you to do very powerful things, by programming to interfaces and not to implementations. For example, you can extend functionality of classes and not change a line of code in those classes, you simply pass a different implementation of a dependency. You are not tied to, or coupled with, any one implentation inside the method doSomethingWith
.
but i also read that if we declare the object myObj as MyInterface, myObj won't be able to use its own methods (from the class Obj), is that correct
Internally, instances of Obj
will continue to have full access to the Obj
API. myObj
is still an Obj
, it will always be able to use its own implementation details.
public interface MyInterface {
void frob();
}
public class Obj implements MyInterface {
public void frob() {
doFrobbing();
}
private void doFrobbing() {
System.out.println("frobbing");
}
public static void main(String[] args) {
MyInterface myObj = new Obj();
myObj.frob(); // still internally calls doFrobbing()
((Obj)myObj).doFrobbing(); // visible only via class reference
}
}
Instances of Obj
will still be instances of Obj
, and those instances will still be able to use doFrobbing
. Externally, persons using those instances via the interface reference will only be able to access the interface methods.
MyInterface myObj = new Obj();
MyInterface mySec = new Sec();
You declare an instance of a MyInterface named myObj. You initialize it with new Obj(); which is authorized by the compiler if Obj implements MyInterface. myObj can only call MyInterface methods obviously. Same for the second line.
Here is a sample code to try:
public class Test {
public static void main(String args[]){
A a = new A();
a.say();
a.a();
B b = new B();
b.say();
b.b();
I ia = new A();
ia.say();
ia.a(); // fail
}
}
interface i {
public void say();
}
class A implements i {
public void say(){
System.out.println("class A");
}
public void a(){
System.out.println("method a");
}
}
class B implements i {
public void say(){
System.out.println("class B");
}
public void b(){
System.out.println("method b");
}
}