CLASS A
class A
{
int a=0; //initialise
A() //default constructor
{
a=10; //value of a in default constructor
}
A(int b) //parametrised constructor
{
a=50; //value of a in PARAMETRISED CONSTRUCTOR
}
void showA() //method call
{
System.out.println(a);
}
}
CLASS B
class B extends A //INHERITANCE OF CLASS A{
int b=0;
B()
{
b=20; // VALUE OF B IN DEFAULT CONSTRUCTOR
}
B(int a)
{
super(a); // super keyword is used to called the value of super class i.e here class A
b=a;
}
void showB() //METHOD CALL
{
System.out.println(b);
}
}
CLASS Object_Call
class Mock_Test_For_Object_Call//mock test application{
public static void main (String args[])//main class
{
B b = new B(); //object created as b
b.showA (); //method with default parameter via object of class b is called
b.showB (); // class B is called
}
}
CLASS Parametrised_Call
class Mock_Test_For_Parametrised_Call{
public static void main (String args[]) //main class here for A and B CLASS
{
B b = new B(30); //object b created with parameter 30
b.showA (); //method with PARAMETRISED CONSTRUCTOR IS EVOKED BY PASSING THE VALUE AS 30 OF CLASS A THOUGH OBJECT OF CLASS B
b.showB (); // method with PARAMETRISED CONSTRUCTOR IS EVOKED BY PASSING THE VALUE AS 30 OF CLASS B
}
}
/**
*@author Shashi Jaiswal
*@use of method overloading is done
*/
No comments:
Post a Comment