What is inheritance?

  1.  The process of “obtaining one object properties into another object” is called inheritance.
  2. It supports reusability.
  3. We can create objects only for child classes.

                                    Or
                      Inheritance is the process of “acquiring the properties of super/base/parent class into sub/derived/child class


Example:

class parent                                                                    
{
            int x;
public void readx(int a)
{
X=a;
            }
}

class child extends parent
{
            int y;
            public void ready(int b)
{
            Y=b;
            }
public int getSum()
            {
                        Return x+y;
            }
            public static void main(Sting a[])
{
            child c=new child();
            c.readx(7);
            c.ready(9);
int sum=c.getSum();
                        System.out.println(“sum is”+sum);
            }
}

No comments:

Post a Comment