Some Programs related to -Static Key Word,over loading vs overriding
=============================================================
A Program to demonstrate over loading vs overriding.
Description:
Class A is having method display().
It is overloaded.
Two version of display() method are created.
One with out argument and with argument.
Class B is extending class A.And have the same method.
So method display in class B overried display method of Class A.Which version to call is decided at runtime.
Class A is having method display().
It is overloaded.
Two version of display() method are created.
One with out argument and with argument.
Class B is extending class A.And have the same method.
So method display in class B overried display method of Class A.Which version to call is decided at runtime.
Source code:
class A
{
void display()
{
System.out.println("Inside A's display method with out argument");
}
void display(int i)
{
System.out.println("Inside A's display method "+ i);
}
}
class B extends A
{
// override display()
void display()
{
System.out.println("Inside B's display method");
}
}
class OverLoad_VS_OverrideDemo
{
public static void main(String args[])
{
A a = new A(); // object of type A
B b = new B(); // object of type B
System.out.println("");
A r; // obtain a reference of type A
r = a; // r refers to an A object
r.display(); // calls A's version of callme
r.display(10); // calls A's version of callme
r = b; // r refers to a B object
r.display(); // calls B's version of callme
}
}
{
void display()
{
System.out.println("Inside A's display method with out argument");
}
void display(int i)
{
System.out.println("Inside A's display method "+ i);
}
}
class B extends A
{
// override display()
void display()
{
System.out.println("Inside B's display method");
}
}
class OverLoad_VS_OverrideDemo
{
public static void main(String args[])
{
A a = new A(); // object of type A
B b = new B(); // object of type B
System.out.println("");
A r; // obtain a reference of type A
r = a; // r refers to an A object
r.display(); // calls A's version of callme
r.display(10); // calls A's version of callme
r = b; // r refers to a B object
r.display(); // calls B's version of callme
}
}
No comments:
Post a Comment