Explore Connect Documentation
Snippets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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;
    }
  }
}
Press desired key combination and then press ENTER.