0

Please read full question, Its different from this type of question Difference between interface and class objects

Example: 1

Class implementing Interface

public interface a{
  void foo();
}
public class b implements a{
  @Override
  void foo(){}
  void bar(){}
}

Working behaviour

a asd=new b(); can call only foo()
b asf=new b(); can call both foo() and bar()

I hope upto here its clear.

Example 2:

Now there is one class CheckingPhase and IdentifyCheckingPhase

class IdentifyCheckingPhase {

    private static a getPhase() {

        return a;
    }

    private static void matchPhase(){


 (CheckingPhase)IdentifyCheckingPhase.getPhase().bar();

 }

}

class CheckingPhase implements a {

    @Override
    void foo() {
    }

    void bar(){   
 }

}

In Example 1. Interface instance only able to call its own implemented method in class and class instance able to all methods (class itself and Interface too). If that's a case, am sure something different being maintanied in compiler side that's why its able to differentiate.

Doubt First, Its correct to say that Interface and Class ref always points to different types instances of same class ? I guess yes, that's they are able to call their own methods. (Interface only its own methods but class ref can call all)

If not, Then In second example, a returned from getPhase(), should not be allowed to replace with CheckingPhase in matchPhase() and call its class instance method. Because a allowed to call only foo and CheckingPhase can call foo and bar both.

Doubt 2, I'm wondering, Is it syntactically correct using CheckingPhase instead of a while coming from method getPhase() to matchPhase() ?

I hope its clear what am trying to ask. Please let me know if may qyestion is not clear. (Its more about how java is using Syntax for above use case)

Maria
  • 452
  • 1
  • 6
  • 20
  • I don't understand what you are trying to ask. You have used some very non-standard terminology. It would be great if you can explain what you mean by "type instance" of a class, and what it means to "come from `getPhase` to `matchPhase`". In addition, there are two non-trivial compiler errors in your code. First, you can't just `return a;` as the variable `a` does not exist. Second, you can't call `bar` in `matchPhase`. – Sweeper Apr 06 '20 at 09:07
  • @Sweeper I think Maria's `matchPhase` method was supposed to read `((CheckingPhase) IdentifyCheckingPhase.getPhase()).bar();` - and she's asking about the legitimacy of casting the result of a method call. Her use of `a` as both a type name and an expression of that type is ultra-confusing though. – Dawood ibn Kareem Apr 06 '20 at 09:08
  • Why its correct to use CheckingPhase instead of 'a' returned from getPhase() ? Becasue both type of instances (Interface and Class) has different scope of methods to be called. If both type of instances has different scope of methods then there must be difference in instances created by Java. If there is difference then how its correct to use CheckingPhase instead of 'a' returned from getPhase() ? – Maria Apr 06 '20 at 09:19
  • I hope its clear now Guys – Maria Apr 06 '20 at 09:20
  • If by "use instead of" you mean "cast to", then the answer is: you can attempt to cast to another type if there's a *chance* (according to the type system) that at runtime the instance is actually of the cast-to type. If it is, the cast will succeed, if it isn't, it will throw a `ClassCastException`. The object pointed to doesn't change just because the reference type is different. – Joachim Sauer Apr 06 '20 at 09:27
  • And as a side-note: don't name your classes `a`. Classes should start with an upper-case letter. Even (or especially) in sample code, so that it's obvious what you're refering to. Call it `MyInterface` or anything like that. – Joachim Sauer Apr 06 '20 at 09:28
  • @Joachim Sauer, you got the point, thanks. But if it’s not changing object it’s pointed to, then using different ref is correct...? – Maria Apr 06 '20 at 09:32
  • What do you mean by "correct"? Does it compile? Ask your compiler! Does it run? Execute it and see. – Joachim Sauer Apr 06 '20 at 09:34
  • Yes it’s running, I mean to say Is it correct to change ref in above scenario...? Seems to be wrong – Maria Apr 06 '20 at 09:36
  • @Maria: what do you mean by **wrong**? If it compiles and runs then it isn't wrong, is it? Do you mean "morally wrong"? "A bad idea"? "Likely to be confusing to readers"? It's okay with the language spec and the compiler. – Joachim Sauer Apr 06 '20 at 11:49

0 Answers0