Friday, November 20, 2015

Program to demostrate final keyword.

class Base
{
final void show()
{
System.out.println("Base class Method");
}
}
class Derived extends Base
{
void show()
{
System.out.println("Derived class Method");
}
}

class FinalMethod
{
public static void main(String args[])
{
Derived obj=new Derived();
obj.show();
}
}
Output:
 error: show() in Derived cannot override show() in Base

Thursday, November 19, 2015

Program to demostrate the use of super keyword.

class Reclangle
{
double length, width,area;
Reclangle(double l, double w)
{
length=l;
width=w;
area=length*width;
System.out.println("Area of Rectangle is : "+area);
}
}
class Cuboid extends Reclangle
{
double height, volume;
Cuboid(double l,double w,double h)
{
super(l,w);
height=h;
volume=length*width*height;
System.out.println("VoIume of cuboid is : "+volume);
}
}
class Demosuper
{
public static void main(String args[])
{
Cuboid obj=new Cuboid(10,15,20);
}
}
Output:


Wednesday, November 18, 2015

Program to implement multilevel inheritance.

class Base
{
void display()
{
System.out.println("Base class Method");
}
}
class Derived1 extends Base
{
void show()
{
System.out.println("Intermediate class method");
}
}
class Derived2 extends Derived1
{
void dis()
{
System.out.println("DeIived class Method");
}
}
class Inherit_Multilevel
{
public static void main(String args[])
{
Derived2 obj=new Derived2();
obj.display();
obj.show();
obj.dis();
}
}
Output:

Program to implement single inheritance.

class Base
{
void display()
{
System.out.println("Base class Method");
}
}
class Derived extends Base
{
void show()
{
System.out.println("DeIived class Method");
}
}
class Inherit_Single
{
public static void main(String args[])
{
Derived obj=new Derived();
obj.display();
obj.show();
}
}
Output:

Program to implement Constructor Overloading.

class Volume
{
double cuboid, sphere;
Volume(double l,double w, double h)
{
cuboid=l*w*h;
System.out.println("Volume of Cuboid = "+cuboid);
}
Volume(double r)
{
sphere=(4/3.0)*3.14*r*r*r;
System.out.println("Volume of Sphere = "+sphere);
}
}
class Multiple_const
{
public static void main(String ar[])
{
Volume cuboid=new Volume(10,20,30);
Volume sphere=new Volume(2.0);
}
}
Output:

Saturday, November 7, 2015

Program of Parameterized Constructor

class Add
{
    int a,b,c;
    public Add(int x,int y)
    {   
        a=x;
        b=y;
        c=a+b;
    }
    void display()
    {
        System.out.println("Sum is = "+c);
    }
}
class ParaConst
{
    public static void main(String args[])
    {
        Add obj = new Add(10,12);
        obj.display();
    }
}

Copy Constructor

class Demo_Copy
{
    int a,b;
    public Demo_Copy(int x,int y)
    {
        a=x;
        b=y;
    }
    public Demo_Copy(Demo_Copy obj)
    {
        a=obj.a;
        b=obj.b;
    }
    void display()
    {
        System.out.println("a = "+a);
        System.out.println("b = "+b);
    }
}
class Copy_Constr
{
    public static void main(String args[])
    {
        Demo_Copy obj1 = new Demo_Copy(10,20);
        Demo_Copy obj2 = new Demo_Copy(obj1);
        obj2.display();
    }
}