• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reference Variables and Objects

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q. Why does a reference to an Interface cannot invoke methods or instance variables not defined in the Interface but defined in the implementing class.The Same thing happens when we are dealing with Parent and Child Classes.

For Example :


----------------------------------------------------
// InterFace
public interface Data {

public static final String name="Name";

void getName();

}

------------------------------------------------------
// Implmenting Class
public class Bank implments Data{

private int age=55;

void getName(){
System.out.println("Name is :- " + Bank.name);
}

public void getAge(){
System.out.println("Age is :- " + age);
}

}

------------------------------------------------------
// Main Method Class
public class Process {

public static void main(String [] args) {

Data d = new Bank();
d.getName(); // This works fine
d.getAge(); // This generates an error ... ???
}

}
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vivek,

This is an essential part of Java and its object oriented nature
(polymorphism).

When you declare a reference variable of class A and create or
instantiate it with an object of subclass B, then two things
happen when you call a method m1() with that reference:

1.) at compilation time the compiler looks for m1 amongst the
(overloaded) methods in the class of the reference type A.
2.) at runtime the JVM looks in the class of the object type B
for any overridden version of the method or just takes the
inherited method.

Suppose that the compiler finds no method in step 1 then it
gives an error. The reason for this is that step 2 is a runtime
check and the compiler doesn't necessarily have to know what
the object will be at runtime due to polymorphism.

Cheers,

Gian Franco
[ June 04, 2004: Message edited by: Gian Franco Casula ]
 
Vivek Nanda
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Gian Franko !!

It would be great if anyone can suggest some reference links also.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic