class
fruit {
public void popo1()
{
System.out.println("this is a popo1 of the fruit class");
}
}
interface squeezable
{
}
class citrus extends fruit
{
public void popo()
{
System.out.println("this is a popo method of the citrus class");
}
}
class lemon extends citrus
{
}
class grapefruit extends citrus
{
public void nitin()
{
System.out.println("what r u trying to do");
}
}
class tangelo extends citrus
{
}
class popo
{
}
class lapore
{
public static void main(
String[]args)
{
grapefruit g[]=new grapefruit[50];
for(int i=0;i<g.length;i++)>
{
g[i]=new grapefruit();
}
citrus[] s=g;
for(int i=0;i<s.length;i++)>
{
s[i].nitin();
}
/*grapefruit[] a=(grapefruit[])s;
for(int i=0;i<a.length;i++)>
{
a[i].nitin();
}*/
}
}
In the above example i have assigned the reference of the subclass to the base class.Then from the base class(citrus)array variable i tried to invoke the
method nitin(),I expected an error but it's showing an output.The reason why i expected an error is that we can invoke a subclass method through the reference varaible of the superclass only if superclass contain's the method with the same name.That is when overriding come's into picture.
Please throw some light on it.
------------------