Joy
Joy

Reputation: 171

Why can't method of a class reference on Interface type

Why we cannot call class method on Interface type which implements that interface..? What's a problem ?

Interface {
    void Iaminterfacemethod();
}

Class implements Interface {

    public void Iaminterfacemethod(){
        System.out.println("I'm class implements Interface");
    } 

    void classmethod() {
        System.out.println("I'm class Method");
    }

    public static void main(String[] args) {
        Interface O = new Class();
        O.classmethod(); --------->Why Not ?
    }
 }

If Interface type can reference to class object, than why can't he access class methods ? Because at the end, that call is going to be worked for class's object only..No..?

Upvotes: 2

Views: 1342

Answers (4)

user8885515
user8885515

Reputation:

Methods declared in your sub-class or implementing class are not part of your super class/interface, thus you cannot invoke those methods which are declared in sub-class with super class/interface reference type. The compiler checks if the reference variable o has a method classmethod() or not, it doesn't.

Upvotes: 0

Ravi
Ravi

Reputation: 31397

There are two things happens when you write your code and compile and run.

  1. At compile-time compiler checks whether you have correct syntax and method/variable accessibility check.
  2. Now, at run-time object would be created and would perform casting and/or boxing(wherever required).

When you write

<Interface> O = new <Class>();
O.classmethod();  <-- will be checked at compile time

So, basically compiler will check the reference whether classmethod exist for O, which is your <Interface>.

Now, assuming <Interface> has been implemented by multiple classes and classmethod method was overriden with different behaviors. Then, depending on object of <Class> (which implements <Interface>), the method would be dynamically bind at run-time.

Upvotes: 4

Suvam Roy
Suvam Roy

Reputation: 953

Try

Class O = new Class(); or `((Class)O).classmethod(); If you are using Interface O = new Class();` 

You can access all the methods.

     Interface O = new Class(); 

this is called Dynamic Dispatch. By this you can only access those methods which are in the Interface. Not the other methods which exclusive property of the Class that implements the methods of the Interface.

In your example your Interface has method : void Iaminterfacemethod(); The class that implements it has another method which is not in the interface i.e. void classmethod() by Interface O = new Class(); O.Iaminterfacemethod(); is possible but O.classmethod(); is not. For that you need Class O = new Class(); now both O.classmethod(); and O.Iaminterfacemethod(); is possible.

Upvotes: 3

jeet427
jeet427

Reputation: 596

It is how inheritance works.

Interface O = new Class();

Here O is reference of Interface hence O knows only about the method which are defined in Interface.

You can only refer the method which a reference knows about.

Upvotes: 0

Related Questions