• 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

no argument constructors

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i learned that if a class and its parent class both have no constructors, the compiler is supposed to complain.
when i compiled Test4, i got no errors.
why did it give no errors?
i read this article:http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read that article again. You'll see down near the bottom it says:

Oracle Docs wrote:If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor

 
Abigail Decan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, but it also says

If the parent has no constructor (Object does have one), the compiler will reject the program.


in my code, the parent of MySub is Test3, and Test3 doesn't have a constructor.
so i thought my compiler would reject the program.

but did Test3's parent class, Object's constructor get called?
in that case, how can it happen that "a parent has no constructor"?
every class is a subclass of Object, so the "call parent's constructor if class doesn't have one" will eventually reach Object.

or is it the case that if a class has a constructor with parameters, they won't have a no-argument constructor?
so if Test3 has a constructor constructor with arguments, i WILL get an error?

UPDATE

i tried this, and got


Exception in thread "main" java.lang.NoSuchMethodError: Test3: method <init>()V
not found
at MySub.<init>(MySub.java:1)
at Test4.main(Test4.java:4)


is this telling me that it can't find the constructor?
 
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If there is no constructor default constructor will be provided by the compiler. You can see that by using "javap filename" in prompt. So every class has atleast 1 constructor, compiler makes sure of it.
Object has a blank constructor, it does not do anything. It's just made to preserve the rule that every base class constructor calls it's parent class constructor before executing itself.
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

in my code, the parent of MySub is Test3, and Test3 doesn't have a constructor.



That's not possible. There is atleast 1 (default) constructor in every normal class.

but did Test3's parent class, Object's constructor get called?



Yes. but you can't see it, it does nothing.

in that case, how can it happen that "a parent has no constructor"?



It's not possible. What is possible is that parent has no matching constructor wrt the parameters.

is it the case that if a class has a constructor with parameters, they won't have a no-argument constructor?



yes, if we declare any constructor then default will not be provided by the compiler.

if Test3 has a constructor constructor with arguments, i WILL get an error?


Only if child does not have a matching constructor. However it may have additional constructors. Atleast 1 no-argument constructor is necessary for inheritance else error will be generated.

TIP : DO NOT provide parameterized cons. in parent class unless needed, if you must then always make another no-argument constructor in parent.

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shubham Semwal wrote: . . .
Object has a blank constructor, it does not do anything. It's just made to preserve the rule that every base class constructor calls it's parent class constructor before executing itself.

No, it isn't It is there to permit instantiation of Object objects. You don't use Object directly very often, but you do need it occasionally.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shubham Semwal wrote:That's not possible...


Shubham,

Please DontWriteLongLines. They make threads very hard to read. I've broken your up this time, but please read the link.

Thanks

Winston
reply
    Bookmark Topic Watch Topic
  • New Topic