
package joosimpleinheritance;

/**
 *
 * A simple example of Inheritance
 * 
 */

//Creating a SUPER Class
class A {
    //If i and j are declared with a private scope then they would not be
    //available for use by class B
    int i, j;
    
    void showij() {
        System.out.println("i and j: " + i + ", " + j);
    }
}

//Creating a SubClass that Extends (Inherits) from class A
class B extends A {
    int k;
    
    void showk() {
        System.out.println("k: " + k);
    }
    
    void sum() {
        System.out.println("i + j + k: " + (i+j+k));
    }
}

public class JOOSimpleInheritance {

    public static void main(String[] args) {
        A superObj = new A();
        B subObj = new B();
        
        //The super class can be used separately
        superObj.i = 10;
        superObj.j = 20;
        System.out.println("Contents of superObj: ");
        superObj.showij();
        System.out.println();
        
        //The sub class has access to all the public members
        //of its super class
        subObj.i = 7;
        subObj.j = 8;
        subObj.k = 9;
        System.out.println("Contents of subObj: ");
        subObj.showij();
        subObj.showk();
        System.out.println();
        
        System.out.println("Sum of i, j and k in subObj: ");
        subObj.sum();
    }
    
}
