Constructors And Instantiation Constructor Basics • Every class must have a constructor • Even abstract classes!! • No return types • Their names must exactly match the class names. Ex1: Given: public class A { void A() { System.out.println("Class A"); } public static void main(String[] args) { new A(); } } What is the result? A. Class A B. Compilation fails. C. An exception is thrown at line 2. D. An exception is thrown at line 6. E. The code executes with no output. Constructor chaining Object() LivingBeings LivingBeings () calls the constructor of Object Mammal Mammal() calls the constructor of LivingBeings Cat() calls the constructor of Mammal Cat Ex2: class Base { Base() { System.out.print("Base"); } } public class A extends Base { public static void main( String[] args ) { new A(); new Base(); } } What is the result? A. Base B. BaseBase C. Compilation fails. D. The code runs with no output. E. An exception is thrown at runtime. Rules for Constructors Access modifiers • • • • private public protected default Private constructors class TestSuper { private int a; private TestSuper(int i) { this.a=i; } public void test() { System.out.println("inside test method"); } public static void main(String args[]) { TestSuper s=new TestSuper(5); System.out.println(s.a); } } class Test { TestSuper t=new TestSuper(10); //error public void callMethod() { test(); //error } } Default constructor compiler- ALWAYS no args class A { A() { } } class B extends A { } Which two statements are true? (Choose two) A. Class B’s constructor is public. B. Class B’s constructor has no arguments. C. Class B’s constructor includes a call to this(). D. Class B’s constructor includes a call to super(). Contd.. Features of default constructors • Same access modifier as class • No arguments • No arg call to super constructor Ex3: public class Test { } What is the prototype of the default constructor? A. Test() B. Test(void) C. public Test() D. public Test(void) E. public void Test() Ex4: Which three statements are true? (Choose three) A. The default constructor initializes method variables. B. The default constructor has the same access as its class. C. The default constructor invokes the no-arg constructor of the superclass. D. If a class lacks a no-arg constructor, the compiler always creates a default constructor. E. The compiler creates a default constructor only when there are no other constructors for the class. Ex5: In which two cases does the compiler supply a default constructor for class A? (Choose two) A. class A { } B. class A { public A() {} } C. class A { public A(int x) {} } D. class Z {} class A extends Z { void A() {} } Ex6: which would generate compiler error??? A. class A { public A (int x) { } } B. class A { } class B extends A { B() { } } C. class A { A() {} } class B { public B() { } } class Z { public Z (int) { } } class A extends Z { } D. Ex7: First statement in a constructor can either super()- call to super class constructor class A1 { public A1() { System.out.println("hello from a"); } } class B extends A1 { public B () { System.out.println("hello from b"); Super(); } } public class A { public static void main(String args[]) { A1 a = new B(); } } What is the result when main is executed? A. Compilation fails. B. hello from a C. hello from b D. hello from b E. hello from a hello from a hello from b this()- call to overloaded constructor class E { int m_x; int m_y; public E(int x, int y) { m_x = x; m_y = y; System.out.println("inside parameterised constructor"); } public E() { this(0, 0); // Calls other constructor. System.out.println("inside no parameterised constructor"); } } public class Ex { public static void main(String args[]) { E e=new E(); } } Ex8: • Compiler will insert a no arg call to super() as the very first statement by default class TestSuper { TestSuper(int i) { } } class TestSub extends TestSuper{ } public class A{ public static void main (String [] args) { new TestSub(); } } Which is true? A. Compilation fails. B. The code runs without exception. C. An exception is thrown at line 7. D. An exception is thrown at line 2. Contd… • A call to super() may or may not contain arguments class Super { public int i = 0; public Super(String text) { i = 1; } } public class Sub extends Super { public Sub(String text) { super(text); i = 2; } public static void main(String args[]) { Sub sub = new Sub("Hello"); System.out.println(sub.i); } } What is the result? A. 0 B. 1 C. 2 D. Compilation fails. Contd.. • No args constructor does not necessarily mean a default constructor. public class Ex { Ex() { //this is not a default constructor } public static void main(String args[]) { Ex e=new Ex(); } } Contd… • Cannot call an instance method or variable until the super class constructor runs. • Only static variables and methods can be accessed as a part of the call to super class E{ int a; E(int i){ } } public class Ex extends E { Ex() {super(E.a); //this will generate an error, but if int a is declared //as static it will not generate an error } public static void main(String args[]){ Ex e=new Ex(); } } Contd… • when invoking a constructor explicitly from another constructor, only static methods can be invoked. No instance methods can be invoked. public class Ex { public static void main (String [] args) { E e1 = new E(); System.out.println(e1.val); E e2= new E(4); System.out.println(e2.val); } } class E { int val; E(int n) { this.val = n; } E() { this(getValue()); } static int getValue() { int i=10; return i; } } Contd.. • When concrete class is instantiated, abstract class constructors are called • Interfaces do not have constructors • Constructors can only be called from within a constructor not from within a method. Overloaded constructors public class Ex { int var; Ex(int var) { this("hello"); } Ex(String s) { this(); System.out.println(s); } Ex() { System.out.println("good-bye"); } public static void main(String[] args) { Ex t = new Ex(5); } } Benefits of constructor overloading • Flexible ways to instantiate objects • Avoid code duplication- one constructor calling another overloaded constructor Recursive call to constructors class A { A() { this("foo"); } A(String s) { this(); } } Compiler error Important points • Constructors are never inherited • They are not methods • They cannot be overridden • They can be overloaded contd… THANK YOU