Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content


class Parent { // superclass
private int a;
public Parent(final int a) {
this.a = a;
class Child extends Parent { // subclass
private int b;
public Child(final int a, final int b) {
super(a); // call to constructor of class Parent
this.b = b;
public class InheritanceExample {
public static void main(final String[] args) {
// Define an object of class Parent
final Parent a = new Parent(3);
// Define an object of class Child
final Child b = new Child(1, 3);
// Polymorphism: a Parent object refers to a Child object
final Parent c = b;
final Child d;
if (c instanceof Child) { // check if c points to class Child
// Cast the object referred to by c to type Child
d = (Child) c;
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content