1 / 27

Constructors And Instantiation

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" ); }

eilis
Télécharger la présentation

Constructors And Instantiation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Constructors And Instantiation

  2. Constructor Basics • Every class must have a constructor • Even abstract classes!! • No return types • Their names must exactly match the class names.

  3. Ex1: • Given: • publicclass A { • void A() { • System.out.println("Class A"); • } • publicstaticvoid 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.

  4. Constructor chaining LivingBeings Mammal Cat

  5. Ex2: • class Base { • Base() { System.out.print("Base"); } • } • publicclass A extends Base { • publicstaticvoid 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.

  6. Rules for Constructors • Access modifiers • private • public • protected • default

  7. Private constructors • class TestSuper { • privateinta; • private TestSuper(int i) • { • this.a=i; • } • publicvoid test() • { • System.out.println("inside test method"); • } • publicstaticvoid main(String args[]) • { • TestSuper s=new TestSuper(5); • System.out.println(s.a); • } • } • class Test • { • TestSuper t=new TestSuper(10); //error • publicvoid callMethod() • { • test(); //error • } • }

  8. 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().

  9. Contd.. • Features of default constructors • Same access modifier as class • No arguments • No arg call to super constructor

  10. 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()

  11. 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.

  12. 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() {} • }

  13. 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() { } • } • D. class Z { • public Z (int) { } • } • class A extends Z { • }

  14. 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(); • } • } • publicclass A { • publicstaticvoid 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

  15. this()- call to overloaded constructor • class E { • intm_x; • intm_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"); • } • } • publicclass Ex • { • publicstaticvoid main(String args[]) • { • E e=new E(); • } • }

  16. Ex8: • Compiler will insert a no arg call to super() as the very first statement by default • class TestSuper { • TestSuper(int i) { } • } • class TestSubextends TestSuper{ • } • publicclass A{ • publicstaticvoid 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.

  17. Contd… • A call to super() may or may not contain arguments • class Super { • publicinti = 0; • public Super(String text) { • i = 1; • } • } • publicclass Sub extends Super { • public Sub(String text) { • super(text); • i = 2; • } • publicstaticvoid 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.

  18. Contd.. • No args constructor does not necessarily mean a default constructor. • publicclass Ex • { • Ex() //this is not a default constructor • { • } • publicstaticvoid main(String args[]) • { • Ex e=new Ex(); • } • }

  19. 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){ } • } • publicclass 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 • } • publicstaticvoid main(String args[]){ • Ex e=new Ex(); • } • }

  20. Contd… • when invoking a constructor explicitly from another constructor, only static methods can be invoked. No instance methods can be invoked. • publicclass Ex { • publicstaticvoid main (String [] args) { • E e1 = new E(); • System.out.println(e1.val); • E e2= new E(4); • System.out.println(e2.val); • } • } • class E { • intval; • E(int n) { • this.val = n; • } • E() { • this(getValue()); • } • staticint getValue() { • int i=10; • return i; • } • }

  21. 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.

  22. Overloaded constructors • publicclass Ex { • intvar; • Ex(int var) { • this("hello"); • } • Ex(String s) { • this(); • System.out.println(s); • } • Ex() { • System.out.println("good-bye"); • } • publicstaticvoid main(String[] args) { • Ex t = new Ex(5); • } • }

  23. Benefits of constructor overloading • Flexible ways to instantiate objects • Avoid code duplication- one constructor calling another overloaded constructor

  24. Recursive call to constructors • class A { • A() { • this("foo"); • } • A(String s) { • this(); • } • } • Compiler error

  25. Important points • Constructors are never inherited • They are not methods • They cannot be overridden • They can be overloaded

  26. contd…

  27. THANK YOU

More Related