Learn Java for Web Development

(Tina Meador) #1

392 APPENDIX A: Introduction to Java


Listing A-17. Constructor Chaining


package apress.appendix_A;


public class ClassA {


public ClassA() {


System.out.println("Class A no-arg constructor");


}


public ClassA(String title) {
System.out.println("Class A constructor");


}


}


package apress.appendix_A;


public class ClassB extends ClassA {


public ClassB(String title){
System.out.println("Class B constructor ");


}


}


package apress.appendix_A;


public class Test {


/**



  • @param args
    */
    public static void main(String[] args) {
    ClassB var1 = new ClassB("classB");


}


}


Here is the output:


Class A no-arg constructor


Class B constructor

Free download pdf