Sunday, 28 September 2014

Java- Extends Eg



package basics;

class SuperClass
{
    int x, y, z;   
    SuperClass()
    {
        System.out.println("Im in main super class--Default Empty!!");
    }
    SuperClass(int ia, int ib)
    {
        x= ia;
        y= ib;       
    }
   
    public int area()
    {
        return x*y;
    }
}

class Super2 extends SuperClass
{
    Super2()
    {       
        super();
        System.out.println("Im in sub class- Super2");
    }
    Super2(int ip, int iq, int ir)
    {
        super(ip, iq);
        z=ir;
    }
    public double volume()
    {
        return x*y*z;
    }
}
public class ExtendsTest {
    public static void main(String[] args) {
        System.out.println("From Main method-Empty class testing..");
        Super2 s2= new Super2();
       
        System.out.println("Constructor having parameters test...");
        Super2 s3= new Super2(10, 20,30);
        System.out.println("Area is = "+ s3.area());
        System.out.println("Volume is = "+ s3.volume());
       
        int[] arr={1,2,3,4,5};
        System.out.println("Array Length is = "+ arr.length);
        System.out.println("Looping through array elements");
        for (int i=0; i<arr.length; i++)
        {
            System.out.println("array element is = "+ arr[i] + " at index= "+ i);
        }
        String s= "\n";
        System.out.println("Length of the string is : "+ s.trim().length());
               
    }
}

No comments:

Post a Comment