Single Level Inheritance
class Base { int a; Base() { a = 0; } Base(int x) { a = x; } void display() { System.out.println("\na = " + a); } }
class Derived extends Base { int b; Derived() { super(); // Calling Base class Default Constuctor b = 0; } Derived(int x, int y) { super(x); // Calling Base class Parameterised Constructor b = y; } void display() { super.display(); // Calling Base class Display method System.out.println("b = " + b); } }
class SingleLevelInheritance { public static void main(String[] args) { Derived d, d1; // Declaring Variables d = new Derived(); d.display(); d1 = new Derived(10, 20); d1.display(); } }
OUTPUT::
a = 0 b = 0 a = 10 b = 20