Saturday 23 June 2012

java program of Inheritence example(Dyanmic dispatch).

import java.io.*;
abstract class c1
{
    int i;
    c1()
    {
        i=10;
    }
    void myMehtod()
    {
        System.out.println("class c1");
    }
}



class c2 extends c1
{
    int i;
    c2()
    {
        i=20;
    }
    void myMehtod()
    {
        System.out.println(i);
        System.out.println("class 2");
    }
}


class InheritExample2
{
    public static void main(String args[])
    {      
        c2 b=new c2();       
         b.myMehtod();
        /*c=a;
        //call c1's method
        c.myMehtod();
        c1 x=new c2();
        //polymorphism  giveing reference of sub class to super class         object
        //call c2's method   
        x.myMehtod();*/
      }
}

No comments:

Post a Comment