• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can Interface variables access derived class members which implements it? (Mala Gupta)

 
shweta patiljadhav
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

While studying the rules for inheritance with interfaces from
Mala Gupta
book , I came across with rules rules for Reference variable and object types ......

Can variable of interface access members of derived class which implements that derived class?

like,

while Reference variable and object types,
this is given in the book,

An object is referred to by a reference variable of an interface implemented
by a class, the reference variable can access only the variables and methods
defined in the interface, and not from derived class which implements the same interface.


At the same time during studying Inheritance with Interfaces, example code in book where interface variable can access derived class members which implements the same interface -



The output of the preceding code is as follows:
testing complete on real device
QA complete
code delivered with release notes

Please help to resolve the confusion....may be this is the dumbest question but I am really unable to get this contradiction right now....

Thanks in Advance!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shweta patiljadhav wrote:may be this is the dumbest question but I am really unable to get this contradiction right now....

There are no dumb questions!

shweta patiljadhav wrote:While studying the rules for inheritance with interfaces I came across contradictory rules with rules for Reference variable and object types ......

These rules are not contradictory.

shweta patiljadhav wrote:An object is referred to by a reference variable of an interface implemented by a class, the reference variable can access only the variables and methods defined in the interface, and not from derived class which implements the same interface.

This statement in the study guide is spot-on!

shweta patiljadhav wrote:At the same time during studying Inheritance with Interfaces, example code in book where interface variable can access derived class members which implements the same interface

In this code snippet, the code in the main method only accesses a method defined in the MobileAppExpert interface. And that's exactly what the aforementioned statement tries to explain. It doesn't access any members of the derived class (because the derived classes don't have any other members than the - required - implementation of the method defined in the interface which is implemented by the derived classes)

Let me give another exampleThe Movable interface defines just one method: move(). The Ball class implements the Movable interface. Besides the required implementation of the move() method, the Ball class also defines a bounce() method. Now have a look at the main method: first a new Ball instance is created and assigned to a Ball reference variable. You can invoke both methods without any problem. But when another new Ball instance is created and this time it's assigned to a Movable reference variable, you can only invoke the methods (and variables) defined in this interface. So you can only invoke the move() method. When you try to invoke the bounce() method (which is not defined in the Movable interface), you'll get a compiler error. And that's exactly what the statement tries to explain.

Hope it helps!
Kind regards,
Roel
 
shweta patiljadhav
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Roel!

as, Methods bind at runtime and variables at compile time.... I have one doubt here,

I can't write like this in your example, please confirm? is it because movable is an interface and not base class and interface won't have object of derived clas -

movable b= new ball();
b.move();
b.bounce();

so that out put will be

moving ball
bouncing ball.

according to
Mala Gupta





and out put will be

Employee
Employee
Employee
Programmer

because method bind at runtime ....

Thank you very much once again for solving doubt so specifically and in depth.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shweta patiljadhav wrote:I can't write like this in your example, please confirm? is it because movable is an interface and not base class and interface won't have object of derived clas -

movable b= new ball();
b.move();
b.bounce();

As you probably know already, Java is case sensitive. So you should put some effort when you post code to make sure you use the right case throughout your code snippet. The types movable and ball don't exist in my code snippet, it should be Movable and Ball. In Java uppercase or lowercase really matter! It can be the difference between "compilation fails" and "compiles successfully". So you should definitely be much more accurate!

Let's take this code snippet as an exampleWhy does line2 result in a compiler error? That's very easy! Let's start with a very, very, very important rule: The compiler doesn't execute any code! So every compiler error you get, is because the compiler knows something is wrong without executing any line of code. So the compiler doesn't know that reference variable m is refering to a Ball instance. The only thing the compiler knows is the type of reference variable m which is Movable. When the compiler does its job, it has to verify to see if you didn't invoke any methods which were not allowed. So because reference variable m is of type Movable, the compiler knows you can only invoke the move() method on a reference variable of type Movable (and that's why line1 compiles successfully). On line2 you try to invoke a bounce() method on a reference variable of type Movable. The compiler is unhappy with this invocation, because on a reference variable of type Movable you can only call the move() method. And if the compiler is unhappy, you'll get a compiler error

Hope it helps!
Kind regards,
Roel
 
shweta patiljadhav
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Roel! and really sorry for not being case sensitive, I know


will be able to invoke only method move();

I have doubt about


would this code execute? please enlighten me on this ....
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shweta patiljadhav wrote:would this code execute? please enlighten me on this ....

Are you kidding me? I just explained this code snippet in my previous post!
 
shweta patiljadhav
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies Roel, I think I am confused because of finding analogy between your Movable and Ball code and this code below-



and out put will be

Employee
Employee
Employee
Programmer

because method bind at runtime .... I am thinking of creating reference like above code does, which is I am unable to ,
I am thinking like, would this work? that is my confusion.... So sorry for troubling you....
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code snippet with Employee and Programmer can be summarized in 2 very simple (and hopefully easy to remember) but very, very, very important rules:
  • Which instance variables you can access is determined at compile time based on the reference variable type.
  • Which instance methods you can call/invoke is determined at compile time based on the reference variable type. Which instance method is actually executed is decided at runtime based on the type of the actual object (= polymorphism).


  • And it doesn't matter if your reference type is an interface (like in the Movable/Ball code snippet) or a class (like in the Employee/Programmer example).

    shweta patiljadhav wrote:I am thinking like, would this work? that is my confusion...

    Because Ball IS-A Movable, you can assign a Ball instance to a reference variable of type Movable. That will compile successfully (as explained in one of my previous posts). So it will definitely work (as in compiles successfully without compiler errors). Unfortunately I still don't understand what's your confusion about that line of code.
     
    shweta patiljadhav
    Ranch Hand
    Posts: 48
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    And it doesn't matter if your reference type is an interface (like in the Movable/Ball code snippet) or a class (like in the Employee/Programmer example).

    Because Ball IS-A Movable, you can assign a Ball instance to a reference variable of type Movable.


    Got it now, thanks a lot!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic