Social Icons

Wednesday, 4 September 2013

Dynamic Dispatch With abstract Function

abstract class A
{
int d1;
int d2;

abstract double area();

 A(int a, int b)
{
d1=a;
d2=b;

}

}


class B extends A
{
  B(int a,int b)
  {
  super(a,b);
  }

  double area()
  {
   return (d1*d2);
  }


}




class C extends A
{
 C(int a, int b)
 {
  super(a,b);
 }

 double area()
{
return d1*d2;
}

}

class over
{
public static void main(String args[])
{
C c=new C(5,3);
B b=new B(4,5);
A r;
r=c;
System.out.println(r.area());
r=b;
System.out.println(r.area());

}
}

Dynamic Method dispatch

class A
{


void show()
{
System.out.println("\n it is of a class");
}

}


class B extends A
{
void show()
{

System.out.println("\n it is of B class");
}


}




class C extends B
{
void show()
{

System.out.println("\n it is of C class");
}

}

class over
{
public static void main(String args[])
{
C c=new C();
A a =new A();
B b=new B();
A r;
r=a;
a.show();
r=b;
b.show();
r=c;
r.show();
}
}

Function overriding In java

class A
{


void show()
{
System.out.println("\n it is of a class");
}

}


class B extends A
{
void show()
{
super.show();
System.out.println("\n it is of B class");
}


}




class C extends B
{
void show()
{
super.show();
System.out.println("\n it is of C class");
}

}

class over
{
public static void main(String args[])
{
C c=new C();
c.show();
}
}

use of Super Keyword

class Box
{
double width;
double height;
double depth;

 Box(Box ob)
 {
  width=ob.width;
  height=ob.width;
  depth=ob.depth;
 }

 Box(double w, double h,double d)
 {
  width=w;
  height=h;
  depth=d;
 }
  Box(double d)
  {
  width=d;
  height=d;
  depth=d;

  }

 void volume()
 {

 System.out.println("\n the volume is " +width*height*depth);
 }
}

class B extends Box
{
double weight;
 B(double a,double b,double c, double d)
 {
 super(a,b,c);
 weight=d;
 }
  void show()
  {
 System.out.println("\n Weight is "+weight);
  }


}

class C
{
public static void main(String args[])
{
B ob=new B(5,6,10,4);
 ob.volume();
 ob.show();
}

}
 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management