Friday 23 August 2013

Can anybody please help me with this polymorphic reference variable explanation?

Can anybody please help me with this polymorphic reference variable
explanation?

Heres the code :
package com.java2.javaapplication2;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
/**
*
* @author Ajinkya
*/
class A{
int data1=10;
void show1(){
System.out.println("In A="+data1);
}
}
class B extends A{
int data2=20;
void show2(){
System.out.println("In B="+data2);
}
}
public class Overriding6 {
public static void main(String args[]){
A a=new B();
System.out.println("Data1="+a.data1);
System.out.println("Data2="+a.data2);
a.show1();
a.show2();
}
}
Please explain A a=new B(); and what variables and methods are accessible
by a ? this technique is called polymorphic reference right, but how does
it work under influence of inheritance ?

No comments:

Post a Comment