Thursday, 23 May 2013

INHERITANCE IN JAVA

This program illustrates the following concepts :-

  1. Inheritance
  2. Constructors (default and argumented constructors)
  3. Sequence of constructors called when inheritance is involved.
  4. Using the super() to access the immediate super-class.
  5. How to call argumented constructor of any class.



SOURCE CODE:-
/********************************************************************************/


class basic_inheritance {

public static void main(String[] args) {

derived_class obj =new derived_class(90,80,70,60);

System.out.println(obj.a+"  "+obj.b+"  "+obj.c+"  "+obj.d+"  ");
}
}



class base_class {
int a,b;
base_class()
{
System.out.println("YOU ARE IN THE DEFAULT CONSTRUCTOR OF BASE_CLASS");
}
base_class(int p,int q)
{
a=p;
b=q;
System.out.println("YOU ARE IN THE ARGUMENTED CONSTRUCTOR OF BASE_CLASS");
}
}



class derived_class extends base_class{
int c,d;
derived_class()
{
System.out.println("YOU ARE IN THE DEFAULT CONSTRUCTOR OF DERIVED_CLASS");
}

derived_class(int p,int q,int r, int s)
{
super(p,q); //the keyword super is used to refer the immediate super class

c=r;
d=s;
System.out.println("YOU ARE IN THE ARGUMENTED CONSTRUCTOR OF DERIVED_CLASS");
}
}
/*******************************************************************************/
Output:-

No comments:

Post a Comment