• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructors

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Kathy Siera's book, second chapter, page 135, it says:-


One last point on the whole default constructor thing (and it's probably very obvious, but we have to say it or we'll feel guilty for years), constructors are never inherited. They aren't methods. They can't be overridden (because they aren't methods and only instance methods can be overridden). So the type of constructor(s) your superclass has in no way determines the type of default constructor you'll get. Some folks mistakenly believe that the default constructor somehow matches the super constructor, either by the arguments the default constructor will have (remember, the default constructor is always a no-arg), or by the arguments used in the compiler-supplied call to super().



Call me stupid. But I don't understand this point at all.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If -- and only if -- you do not write any constructors for your class, then the the compiler will provide a default constructor. A default constructor will always be a no-args constructor with an implicit call to no-args super(). This is true regardless of what constructors the superclass might have.

For example, suppose your superclass has a single constructor that takes a String, and you do not provide a constructor for your subclass...

In this situation, you will have a problem. Why? Because the subclass will get a default no-args constructor, and this default constructor will try to call super() with no args, but no such constructor exists for the superclass.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which points don't you understand. please be specific.

default contructor is called implicitly when no other constructor is contructed. you can define you own contructor which can have no-args or have args.

example 1
class Bake{
Bake(){}// if you dont define this it will not be called implicitly by JVM
Bake(int x){}// this is defined constructor made explicitly.
Bake(int...x){}// this is defined constructor made explicitly.

}

example 2
class Cook{
Cook(){}// this contructor is called every time, wheather you see or you dont see it and on condition that you dont define you our contructor with args.
}
 
Raef Kandeel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, so all they are saying that the implicit call to "super()" is without arguments and so if you don't define your own no-arg constructor and define a new "arg-constructor" (in which case there would be no default constructor), it would give a compiler error if super() was called. Isn't that redundant? I thought they were making a new point.

. Thank you for clearing that up.
 
reply
    Bookmark Topic Watch Topic
  • New Topic