Site Search:

Describe inheritance and its benefits


Back OCAJP


public class subClass extends superClass {}

Java use extends keyword to establish inheritance relationship between superclass (parent class) to subclass (child class). All super class's public and protected methods and variables are accessible to the subclasses. The benefits of inheritance is code reusability. All classes in java are subclass of java.lang.Object (object oriented by design).

When extending a class, watch for compilation errors.

The first line of every constructor is to implicitly or explicitly call the constructor of another constructor using this() or a call to to super class's constructor using super(). Any other code between means compilation error.

If the super class contains a default or no argument constructor, call super() can be omitted; if the super class have non default constructors, the subclass have to call it with super(arguments...), omitting it means compilation error.



OCAJP>cat test.java 
import java.util.*;
import java.util.function.*;
class test {
  public static void main(String...args) {
    subClass sub = new subClass();
    System.out.println("sub.var1="+sub.var1);
    System.out.println("sub.var2="+sub.var2);
    //System.out.println("sub.var3="+sub.var3);  //error: var3 has private access in subClass
    //System.out.println("sub.var4="+sub.var4);  //error: var4 has private access in subClass
    System.out.println("sub.getVar1="+sub.getVar1()); 
    System.out.println("sub.getVar2="+sub.getVar2());
    System.out.println("sub.getVar3="+sub.getVar3());
    System.out.println("sub.getVar4="+sub.getVar4());
    System.out.println("sub.getSuperVar4="+sub.getSuperVar4());
    subClass4 sub4 = new subClass4();
    System.out.println("sub4.var1="+sub4.var1);
    System.out.println("sub4.var2="+sub4.var2);
  }
}
class superClass {
  public int var1 = 0;
  protected int var2 = 1;
  private int var3 = 2;
  public int var4 = 3;
  public int getVar3() {return var3;}
  public int getVar1() {return var1;}
  int getVar2() {return var2;}  //subClass can no access outside package
}

class subClass extends superClass {
  public int var1 = 10;  //override
  protected String var2 = "type change";
  private int var4 = 20;
  public subClass() {
    /*this(0,0);  //error: call to super must be first statement in constructor
    System.out.println();  //error: call to super must be first statement in constructor 
    super();
    */
    this(10, 20);  //Note: call super must be the first statement in constructor, otherwise call this must be the first statement in constructor
  }
  public subClass(int var1, int var3) {
    super();
    var3 = 2000;  //no effect, this.var3
  }
  //private int getVar1() { return var1;}  //attempting to assign weaker access privileges; was public
  //public int getVar3() { return var3;}  //error: var3 has private access in superClass
  public int getVar4() { return var4;}
  public int getSuperVar4() { return super.var4;}
}

class subClass4 extends subClass1{
  public subClass4() {super(111, 222);}
}

/*
class subClass3 extends subClass1{
  //super(111,222);  //error: illegal start of type
  //public subClass3() {}  //error: constructor subClass1 in class subClass1 cannot be applied to given types;
}
*/
//class subClass2 extends subClass1 {}  //error: constructor subClass1 in class subClass1 cannot be applied to given types;

class subClass1 extends subClass {
  subClass1(int a, int b) {}
}

OCAJP>javac test.java 
OCAJP>java test
sub.var1=10
sub.var2=type change
sub.getVar1=0
sub.getVar2=1
sub.getVar3=2
sub.getVar4=20
sub.getSuperVar4=3
sub4.var1=10
sub4.var2=type change


Back OCAJP