1. Example for Overloading :
class OverLoading
{
// Area for square
public double area(int a)
{
double sArea = a*a;
return sArea;
}
// Area for circle
public double area(double a)
{
double cArea = 3.14*a*a;
return cArea;
}
// Area for rectangle
public double area(double b, double h)
{
double rArea = b*h;
return rArea;
}
// Area for Eclipse
public double area(double b, int h)
{
double eArea = 0.5*b*h;
return eArea;
}
public static void main(String[] arg)
{
OverLoading oL = new OverLoading();
System.out.println(" Area for Square = "+oL.area(10));
System.out.println(" Area for Circle = "+oL.area(10.0));
System.out.println(" Area for Rectangle = "+oL.area(10.0,10.0));
System.out.println(" Area for Eclipse = "+oL.area(10.0,10));
}
}
2. Example for Overriding :
public class Overriding
{
public static void main(String arg[])
{
System.out.println(" ----------------------------------------");
System.out.println(" Animal a = new Animal(); ");
Animal a = new Animal();
System.out.println(" (non static mtd) a.eat() ");
a.eat();
System.out.println(" ( static mtd) a.showActualClassName() ");
a.showActualClassName();
System.out.println(" ----------------------------------------");
System.out.println(" ----------------------------------------");
System.out.println(" Tiger t = new Tiger(); ");
Tiger t = new Tiger();
System.out.println(" (non static mtd) t.eat() ");
t.eat();
System.out.println(" (static mtd) t.showActualClassName() ");
t.showActualClassName();
System.out.println(" ----------------------------------------");
System.out.println(" ----------------------------------------");
System.out.println(" Animal at = new Tiger(); ");
Animal at = new Tiger();
System.out.println(" (non static mtd) at.eat() ");
at.eat();
System.out.println(" ( static mtd) at.showActualClassName() ");
at.showActualClassName();
System.out.println(" ----------------------------------------");
}
}
class Animal
{
public Animal()
{
System.out.println("Animal constructor invoked");
}
public void eat()
{
System.out.println("Animal eat method called");
}
public static void showActualClassName()
{
System.out.println("This class name is : Animal");
}
}
class Tiger extends Animal
{
public Tiger()
{
System.out.println("Tiger constructor invoked");
}
public void eat()
{
System.out.println("Tiger eat method called");
}
public static void showActualClassName()
{
System.out.println("This class name is : Tiger");
}
}
No comments:
Post a Comment